增加收获属性逻辑,优化掉落物表现效果
This commit is contained in:
+178
-36
@@ -11,14 +11,10 @@ namespace SepCore.Simulation
|
||||
// W CollectedPickups
|
||||
public static JobHandle Schedule(PickupStore pickups, NativeList<int> collectedPickups,
|
||||
float deltaTime, float3 playerPosition, float absorbRange, float absorbSpeed, float collectDistance,
|
||||
JobHandle dependency)
|
||||
float recoilDistance, float recoilDuration, float dashAccelerationDuration,
|
||||
float dashMaxSpeedMultiplier, float turnSpeed, JobHandle dependency)
|
||||
{
|
||||
int pickupCount = pickups.Count;
|
||||
if (pickupCount == 0)
|
||||
{
|
||||
return dependency;
|
||||
}
|
||||
|
||||
PickupMovementJob job = new PickupMovementJob
|
||||
{
|
||||
Transforms = pickups.Transform.Data.AsArray(),
|
||||
@@ -27,9 +23,14 @@ namespace SepCore.Simulation
|
||||
Radii = pickups.Radius.Data.AsArray(),
|
||||
CollectedPickups = collectedPickups.AsParallelWriter(),
|
||||
PlayerPosition = playerPosition,
|
||||
AbsorbRange = math.max(0f, absorbRange),
|
||||
AbsorbSpeed = math.max(0f, absorbSpeed),
|
||||
CollectDistance = math.max(0f, collectDistance),
|
||||
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);
|
||||
@@ -39,6 +40,10 @@ namespace SepCore.Simulation
|
||||
[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;
|
||||
@@ -48,52 +53,189 @@ namespace SepCore.Simulation
|
||||
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];
|
||||
PickupIdentitySim identity = Identities[index];
|
||||
PickupRadiusSim radius = Radii[index];
|
||||
|
||||
float3 currentPosition = transform.Position;
|
||||
float3 horizontalPosition = new float3(currentPosition.x, 0f, currentPosition.z);
|
||||
float3 toPlayer = PlayerPosition - horizontalPosition;
|
||||
float sqrDistance = math.lengthsq(toPlayer);
|
||||
|
||||
float effectiveCollectDistance = math.max(CollectDistance, radius.Radius);
|
||||
float sqrCollectDistance = effectiveCollectDistance * effectiveCollectDistance;
|
||||
|
||||
if (sqrDistance <= sqrCollectDistance)
|
||||
if (state.State == PickupStateSim.StateCollected)
|
||||
{
|
||||
CollectedPickups.AddNoResize(identity.EntityId);
|
||||
return;
|
||||
}
|
||||
|
||||
bool isAbsorbing = state.State == PickupStateSim.StateAbsorbing;
|
||||
bool shouldAbsorb = isAbsorbing || (AbsorbRange > 0f && sqrDistance <= AbsorbRange * AbsorbRange);
|
||||
PickupIdentitySim identity = Identities[index];
|
||||
PickupRadiusSim radius = Radii[index];
|
||||
float effectiveCollectDistance = math.max(CollectDistance, radius.Radius);
|
||||
float deltaTime = DeltaTime;
|
||||
|
||||
if (shouldAbsorb && AbsorbSpeed > 0f && sqrDistance > float.Epsilon)
|
||||
if (state.State == PickupStateSim.StateIdle)
|
||||
{
|
||||
state.State = PickupStateSim.StateAbsorbing;
|
||||
float distance = math.sqrt(sqrDistance);
|
||||
float step = AbsorbSpeed * DeltaTime;
|
||||
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);
|
||||
|
||||
if (step >= distance)
|
||||
float3 toPlayer = PlayerPosition - transform.Position;
|
||||
toPlayer.y = 0f;
|
||||
if (AbsorbRange <= 0f || math.lengthsq(toPlayer) > AbsorbRange * AbsorbRange || AbsorbSpeed <= 0f)
|
||||
{
|
||||
transform.Position = new float3(PlayerPosition.x, currentPosition.y, PlayerPosition.z);
|
||||
CollectedPickups.AddNoResize(identity.EntityId);
|
||||
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
|
||||
{
|
||||
float3 direction = toPlayer / distance;
|
||||
transform.Position = currentPosition + direction * step;
|
||||
}
|
||||
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;
|
||||
|
||||
Transforms[index] = transform;
|
||||
States[index] = state;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user