242 lines
10 KiB
C#
242 lines
10 KiB
C#
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Jobs;
|
|
using Unity.Mathematics;
|
|
|
|
namespace SepCore.Simulation
|
|
{
|
|
internal static class PickupMovementSystem
|
|
{
|
|
// 读写集:R/W PickupTransform / PickupState; R PickupIdentity / PickupRadius
|
|
// W CollectedPickups
|
|
public static JobHandle Schedule(PickupStore pickups, NativeList<int> collectedPickups,
|
|
float deltaTime, float3 playerPosition, float absorbRange, float absorbSpeed, float collectDistance,
|
|
float recoilDistance, float recoilDuration, float dashAccelerationDuration,
|
|
float dashMaxSpeedMultiplier, float turnSpeed, JobHandle dependency)
|
|
{
|
|
int pickupCount = pickups.Count;
|
|
PickupMovementJob job = new PickupMovementJob
|
|
{
|
|
Transforms = pickups.Transform.Data.AsArray(),
|
|
States = pickups.State.Data.AsArray(),
|
|
Identities = pickups.Identity.Data.AsArray(),
|
|
Radii = pickups.Radius.Data.AsArray(),
|
|
CollectedPickups = collectedPickups.AsParallelWriter(),
|
|
PlayerPosition = playerPosition,
|
|
AbsorbRange = absorbRange,
|
|
AbsorbSpeed = absorbSpeed,
|
|
CollectDistance = collectDistance,
|
|
RecoilDistance = recoilDistance,
|
|
RecoilDuration = recoilDuration,
|
|
DashAccelerationDuration = dashAccelerationDuration,
|
|
DashMaxSpeedMultiplier = dashMaxSpeedMultiplier,
|
|
TurnSpeedRadians = math.radians(turnSpeed),
|
|
DeltaTime = deltaTime
|
|
};
|
|
return job.Schedule(pickupCount, 64, dependency);
|
|
}
|
|
}
|
|
|
|
[BurstCompile]
|
|
internal struct PickupMovementJob : IJobParallelFor
|
|
{
|
|
private const float IdleFloatAmplitude = 0.08f;
|
|
private const float IdleFloatAngularFrequency = 3.14159265f;
|
|
private const float IdleRotationSpeedRadians = 0.610865238f;
|
|
|
|
public NativeArray<PickupTransformSim> Transforms;
|
|
public NativeArray<PickupStateSim> States;
|
|
[ReadOnly] public NativeArray<PickupIdentitySim> Identities;
|
|
[ReadOnly] public NativeArray<PickupRadiusSim> Radii;
|
|
public NativeList<int>.ParallelWriter CollectedPickups;
|
|
public float3 PlayerPosition;
|
|
public float AbsorbRange;
|
|
public float AbsorbSpeed;
|
|
public float CollectDistance;
|
|
public float RecoilDistance;
|
|
public float RecoilDuration;
|
|
public float DashAccelerationDuration;
|
|
public float DashMaxSpeedMultiplier;
|
|
public float TurnSpeedRadians;
|
|
public float DeltaTime;
|
|
|
|
public void Execute(int index)
|
|
{
|
|
PickupTransformSim transform = Transforms[index];
|
|
PickupStateSim state = States[index];
|
|
if (state.State == PickupStateSim.StateCollected)
|
|
{
|
|
return;
|
|
}
|
|
|
|
PickupIdentitySim identity = Identities[index];
|
|
PickupRadiusSim radius = Radii[index];
|
|
float effectiveCollectDistance = math.max(CollectDistance, radius.Radius);
|
|
float deltaTime = DeltaTime;
|
|
|
|
if (state.State == PickupStateSim.StateIdle)
|
|
{
|
|
state.IdleElapsed = math.fmod(state.IdleElapsed + deltaTime,
|
|
2f * math.PI / IdleFloatAngularFrequency);
|
|
transform.Position.y = state.FloatBaseHeight +
|
|
math.sin(state.IdleElapsed * IdleFloatAngularFrequency) * IdleFloatAmplitude;
|
|
RotateWhileIdle(ref transform, deltaTime);
|
|
|
|
float3 toPlayer = PlayerPosition - transform.Position;
|
|
toPlayer.y = 0f;
|
|
if (AbsorbRange <= 0f || math.lengthsq(toPlayer) > AbsorbRange * AbsorbRange || AbsorbSpeed <= 0f)
|
|
{
|
|
Transforms[index] = transform;
|
|
States[index] = state;
|
|
return;
|
|
}
|
|
|
|
BeginRecoil(ref transform, ref state, PlayerPosition);
|
|
}
|
|
else if (state.State == PickupStateSim.StateAbsorbRequested)
|
|
{
|
|
BeginRecoil(ref transform, ref state, PlayerPosition);
|
|
}
|
|
|
|
float remainingDeltaTime = deltaTime;
|
|
if (state.State == PickupStateSim.StateRecoil && remainingDeltaTime > 0f)
|
|
{
|
|
if (RecoilDuration <= 0f)
|
|
{
|
|
transform.Position = state.RecoilStartPosition + state.RecoilDirection * RecoilDistance;
|
|
state.State = PickupStateSim.StateDash;
|
|
state.PhaseElapsed = 0f;
|
|
}
|
|
else
|
|
{
|
|
float phaseRemaining = RecoilDuration - state.PhaseElapsed;
|
|
float stepTime = math.min(remainingDeltaTime, phaseRemaining);
|
|
state.PhaseElapsed += stepTime;
|
|
float recoilProgress = math.saturate(state.PhaseElapsed / RecoilDuration);
|
|
transform.Position = state.RecoilStartPosition +
|
|
state.RecoilDirection * (RecoilDistance * recoilProgress);
|
|
remainingDeltaTime -= stepTime;
|
|
|
|
if (state.PhaseElapsed >= RecoilDuration)
|
|
{
|
|
state.State = PickupStateSim.StateDash;
|
|
state.PhaseElapsed = 0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (state.State == PickupStateSim.StateDash)
|
|
{
|
|
float3 toPlayer = PlayerPosition - transform.Position;
|
|
toPlayer.y = 0f;
|
|
float distance = math.length(toPlayer);
|
|
if (distance <= effectiveCollectDistance)
|
|
{
|
|
MarkCollected(ref state, identity.EntityId);
|
|
}
|
|
else if (remainingDeltaTime > 0f && AbsorbSpeed > 0f)
|
|
{
|
|
float step = GetDashDistance(AbsorbSpeed, state.PhaseElapsed, remainingDeltaTime,
|
|
DashAccelerationDuration, DashMaxSpeedMultiplier);
|
|
float distanceToCollect = distance - effectiveCollectDistance;
|
|
float movedDistance = math.min(step, distanceToCollect);
|
|
transform.Position += toPlayer / distance * movedDistance;
|
|
state.PhaseElapsed += remainingDeltaTime;
|
|
if (movedDistance >= distanceToCollect)
|
|
{
|
|
MarkCollected(ref state, identity.EntityId);
|
|
}
|
|
}
|
|
}
|
|
|
|
RotateTowardPlayer(ref transform, PlayerPosition, deltaTime);
|
|
Transforms[index] = transform;
|
|
States[index] = state;
|
|
}
|
|
|
|
private static void BeginRecoil(ref PickupTransformSim transform, ref PickupStateSim state,
|
|
float3 playerPosition)
|
|
{
|
|
float3 direction = transform.Position - playerPosition;
|
|
direction.y = 0f;
|
|
if (math.lengthsq(direction) <= float.Epsilon)
|
|
{
|
|
direction = new float3(0f, 0f, -1f);
|
|
}
|
|
else
|
|
{
|
|
direction = math.normalize(direction);
|
|
}
|
|
|
|
state.State = PickupStateSim.StateRecoil;
|
|
state.PhaseElapsed = 0f;
|
|
state.RecoilStartPosition = transform.Position;
|
|
state.RecoilDirection = direction;
|
|
if (math.lengthsq(transform.Forward) <= float.Epsilon)
|
|
{
|
|
transform.Forward = new float3(0f, 0f, 1f);
|
|
}
|
|
}
|
|
|
|
private static void RotateWhileIdle(ref PickupTransformSim transform, float deltaTime)
|
|
{
|
|
float3 forward = transform.Forward;
|
|
forward.y = 0f;
|
|
forward = math.lengthsq(forward) <= float.Epsilon
|
|
? new float3(0f, 0f, 1f)
|
|
: math.normalize(forward);
|
|
|
|
transform.Forward = math.normalize(math.rotate(
|
|
quaternion.AxisAngle(new float3(0f, 1f, 0f), IdleRotationSpeedRadians * deltaTime), forward));
|
|
}
|
|
|
|
private void MarkCollected(ref PickupStateSim state, int entityId)
|
|
{
|
|
state.State = PickupStateSim.StateCollected;
|
|
CollectedPickups.AddNoResize(entityId);
|
|
}
|
|
|
|
private static float GetDashDistance(float speed, float elapsed, float deltaTime,
|
|
float accelerationDuration, float maxSpeedMultiplier)
|
|
{
|
|
if (accelerationDuration <= 0f)
|
|
{
|
|
return speed * maxSpeedMultiplier * deltaTime;
|
|
}
|
|
|
|
float acceleratedUntil = math.min(accelerationDuration, elapsed + deltaTime);
|
|
float acceleratedDuration = math.max(0f, acceleratedUntil - elapsed);
|
|
float averageMultiplier = 1f + (maxSpeedMultiplier - 1f) *
|
|
(elapsed + acceleratedUntil) / (2f * accelerationDuration);
|
|
float distance = speed * acceleratedDuration * averageMultiplier;
|
|
float constantDuration = deltaTime - acceleratedDuration;
|
|
return distance + speed * maxSpeedMultiplier * constantDuration;
|
|
}
|
|
|
|
private void RotateTowardPlayer(ref PickupTransformSim transform, float3 playerPosition,
|
|
float deltaTime)
|
|
{
|
|
float3 targetDirection = playerPosition - transform.Position;
|
|
targetDirection.y = 0f;
|
|
if (math.lengthsq(targetDirection) <= float.Epsilon)
|
|
{
|
|
return;
|
|
}
|
|
|
|
targetDirection = math.normalize(targetDirection);
|
|
float3 currentForward = transform.Forward;
|
|
currentForward.y = 0f;
|
|
currentForward = math.lengthsq(currentForward) <= float.Epsilon
|
|
? new float3(0f, 0f, 1f)
|
|
: math.normalize(currentForward);
|
|
|
|
float signedAngle = math.atan2(math.cross(currentForward, targetDirection).y,
|
|
math.dot(currentForward, targetDirection));
|
|
float angleStep = math.clamp(signedAngle, -TurnSpeedRadians * deltaTime,
|
|
TurnSpeedRadians * deltaTime);
|
|
transform.Forward = math.normalize(math.rotate(quaternion.AxisAngle(new float3(0f, 1f, 0f), angleStep),
|
|
currentForward));
|
|
}
|
|
}
|
|
}
|