Great timeline system (Age of AI WON)
This commit is contained in:
1 parent
2ce0391c16
commit
e312131725
182 files changed
+380070
-470221
No files matched your search
@@ -11,14 +11,14 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
private const float StoppingDistance = 0.15f;
|
||||
private const float RotateDistance = 1f;
|
||||
private const float NavMeshSampleRadius = 1.5f;
|
||||
private const float TurnSpeedDegPerSec = 720f;
|
||||
private const float TurnSpeedDegPerSec = 720f;
|
||||
private const int StuckExtraFrames = 90;
|
||||
|
||||
private int MoveStateHash => Animator.StringToHash("WalkBlend");
|
||||
private static readonly int MoveStateHash = Animator.StringToHash("WalkBlend");
|
||||
|
||||
public override BattleActionType ActionType => BattleActionType.Move;
|
||||
public override DeciderType DeciderType => DeciderType.Position;
|
||||
|
||||
// Ультра супер мега конструкция для проверки и утверждения точной позиции. Но не работающая.
|
||||
public override BattleActionData MountAndGetFrames(EntityProperties entity, BattleActionDecisionResult result)
|
||||
{
|
||||
MovementBattleActionData data = new();
|
||||
@@ -29,21 +29,17 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
|
||||
Vector3 toTargetXZ = targetPos - startPos;
|
||||
toTargetXZ.y = 0f;
|
||||
|
||||
Debug.DrawLine(startPos, targetPos, Color.red, 5f);
|
||||
|
||||
float distanceXZ = toTargetXZ.magnitude;
|
||||
|
||||
if (distanceXZ <= StoppingDistance)
|
||||
if (toTargetXZ.magnitude <= StoppingDistance)
|
||||
{
|
||||
data.IsRotateOnly = true;
|
||||
data.Corners = null;
|
||||
|
||||
float angle = Quaternion.Angle(entity.KinematicCharacterMotor.TransientRotation, positionDecisionResult.Rotation);
|
||||
|
||||
if (angle < 1f)
|
||||
{
|
||||
data.TotalFrames = 0;
|
||||
data.Finished = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -60,6 +56,7 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
if (!hasStart || !hasTarget)
|
||||
{
|
||||
data.TotalFrames = 0;
|
||||
data.Finished = true;
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -71,13 +68,12 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
data.Corners = new[] { startPos, validTarget };
|
||||
data.CurrentCornerIndex = 1;
|
||||
data.TotalFrames = CalculatePathFrames(data.Corners, entity.MovementController);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
NavMeshPath path = new();
|
||||
if (NavMesh.CalculatePath(validStart, validTarget, NavMesh.AllAreas, path)
|
||||
&& path.status == NavMeshPathStatus.PathComplete
|
||||
if (NavMesh.CalculatePath(validStart, validTarget, NavMesh.AllAreas, path)
|
||||
&& path.status == NavMeshPathStatus.PathComplete
|
||||
&& path.corners.Length > 1)
|
||||
{
|
||||
path.corners[0] = startPos;
|
||||
@@ -90,43 +86,31 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
else
|
||||
{
|
||||
data.TotalFrames = 0;
|
||||
data.Finished = true;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public override void Finish(EntityProperties entity, BattleActionData actionData, BattleActionDecisionResult result)
|
||||
{
|
||||
entity.KinematicCharacterMotor.BaseVelocity = Vector3.zero;
|
||||
|
||||
entity.MovementController.Freeze = true;
|
||||
entity.MovementController.InternalVelocityAdd = Vector3.zero;
|
||||
entity.MovementController.MoveInputVector = Vector3.zero;
|
||||
|
||||
Debug.Log($"{entity.GetEntityId()} is frozen");
|
||||
}
|
||||
|
||||
public override void Tick(
|
||||
float tickDelta,
|
||||
EntityProperties entity,
|
||||
BattleActionDecisionResult battleActionDecisionResult,
|
||||
float tickDelta,
|
||||
EntityProperties entity,
|
||||
BattleActionDecisionResult battleActionDecisionResult,
|
||||
BattleActionData battleActionData)
|
||||
{
|
||||
if (battleActionData is not MovementBattleActionData movementActionData
|
||||
|| battleActionDecisionResult is not PositionBattleActionDeciderResult positionDecisionResult)
|
||||
if (battleActionData is not MovementBattleActionData movementActionData
|
||||
|| battleActionDecisionResult is not PositionBattleActionDeciderResult positionDecisionResult)
|
||||
{
|
||||
battleActionData.Finished = true;
|
||||
return;
|
||||
}
|
||||
|
||||
var movementController = entity.MovementController;
|
||||
|
||||
movementController.Freeze = false;
|
||||
|
||||
movementActionData.CurrentFrame++;
|
||||
|
||||
if (movementActionData.IsRotateOnly)
|
||||
{
|
||||
movementActionData.CurrentFrame++;
|
||||
|
||||
float angle = Quaternion.Angle(movementController.Motor.TransientRotation, positionDecisionResult.Rotation);
|
||||
int framesLeft = Mathf.Max(1, movementActionData.TotalFrames - movementActionData.CurrentFrame + 1);
|
||||
float stepAngle = angle / framesLeft;
|
||||
@@ -134,11 +118,10 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
Quaternion nextRot = Quaternion.RotateTowards(
|
||||
movementController.Motor.TransientRotation,
|
||||
positionDecisionResult.Rotation,
|
||||
stepAngle
|
||||
);
|
||||
stepAngle);
|
||||
|
||||
entity.KinematicCharacterMotor.SetRotation(nextRot);
|
||||
StopMovementAndFinish(movementController);
|
||||
StopMovementInputs(movementController);
|
||||
|
||||
if (movementActionData.CurrentFrame >= movementActionData.TotalFrames || angle <= 0.5f)
|
||||
{
|
||||
@@ -152,7 +135,7 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
if (movementActionData.Corners == null || movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length)
|
||||
{
|
||||
entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation);
|
||||
StopMovementAndFinish(movementController);
|
||||
StopMovementInputs(movementController);
|
||||
battleActionData.Finished = true;
|
||||
return;
|
||||
}
|
||||
@@ -160,28 +143,17 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
if (!movementActionData.WasStarted)
|
||||
{
|
||||
movementActionData.WasStarted = true;
|
||||
float transitionDurationSeconds = 10 * BattleTimeline.TickDelta;
|
||||
entity.animator.CrossFadeInFixedTime(MoveStateHash, transitionDurationSeconds);
|
||||
entity.animator.applyRootMotion = false;
|
||||
entity.animator.CrossFadeInFixedTime(MoveStateHash, 10 * BattleTimeline.TickDelta);
|
||||
}
|
||||
|
||||
Vector3 currentPos = entity.KinematicCharacterMotor.TransientPosition;
|
||||
|
||||
while (movementActionData.CurrentCornerIndex < movementActionData.Corners.Length)
|
||||
{
|
||||
Vector3 toCorner = movementActionData.Corners[movementActionData.CurrentCornerIndex] - currentPos;
|
||||
toCorner.y = 0f;
|
||||
|
||||
if (toCorner.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
movementActionData.CurrentCornerIndex++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
AdvanceCorners(movementActionData, currentPos);
|
||||
|
||||
if (movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length)
|
||||
{
|
||||
entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation);
|
||||
StopMovementAndFinish(movementController);
|
||||
|
||||
StopMovementInputs(movementController);
|
||||
battleActionData.Finished = true;
|
||||
return;
|
||||
}
|
||||
@@ -197,7 +169,6 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
bool preLastPoint = isLastCorner && distance < RotateDistance;
|
||||
|
||||
float maxStep = movementController.MaxStableMoveSpeed * tickDelta;
|
||||
|
||||
float inputMagnitude = (isLastCorner && distance < maxStep && maxStep > 0f)
|
||||
? distance / maxStep
|
||||
: 1f;
|
||||
@@ -213,22 +184,62 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
MoveVector = moveNormal * inputMagnitude,
|
||||
Look = lookRotation
|
||||
};
|
||||
|
||||
movementController.SetInputs(ref aiInputs);
|
||||
}
|
||||
|
||||
public override void PostPhysicsTick(
|
||||
EntityProperties entity,
|
||||
BattleActionDecisionResult battleActionDecisionResult,
|
||||
BattleActionData battleActionData)
|
||||
{
|
||||
if (battleActionData.Finished
|
||||
|| battleActionData is not MovementBattleActionData movementActionData
|
||||
|| battleActionDecisionResult is not PositionBattleActionDeciderResult positionDecisionResult
|
||||
|| movementActionData.IsRotateOnly
|
||||
|| movementActionData.Corners == null)
|
||||
return;
|
||||
|
||||
Vector3 postMovePos = entity.KinematicCharacterMotor.TransientPosition;
|
||||
AdvanceCorners(movementActionData, postMovePos);
|
||||
|
||||
Vector3 postMovePos = movementController.Motor.TransientPosition;
|
||||
Vector3 toFinal = movementActionData.Corners[^1] - postMovePos;
|
||||
toFinal.y = 0f;
|
||||
|
||||
if (isLastCorner && toFinal.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
bool arrived = movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length
|
||||
|| toFinal.sqrMagnitude <= StoppingDistance * StoppingDistance;
|
||||
bool stuckTimeout = movementActionData.CurrentFrame > movementActionData.TotalFrames + StuckExtraFrames;
|
||||
|
||||
if (arrived || stuckTimeout)
|
||||
{
|
||||
entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation);
|
||||
StopMovementAndFinish(movementController);
|
||||
StopMovementInputs(entity.MovementController);
|
||||
battleActionData.Finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void StopMovementAndFinish(MovementController movementController)
|
||||
public override void Finish(EntityProperties entity, BattleActionData actionData, BattleActionDecisionResult result)
|
||||
{
|
||||
entity.KinematicCharacterMotor.BaseVelocity = Vector3.zero;
|
||||
entity.MovementController.Freeze = true;
|
||||
entity.MovementController.InternalVelocityAdd = Vector3.zero;
|
||||
entity.MovementController.MoveInputVector = Vector3.zero;
|
||||
}
|
||||
|
||||
private static void AdvanceCorners(MovementBattleActionData data, Vector3 pos)
|
||||
{
|
||||
while (data.CurrentCornerIndex < data.Corners.Length)
|
||||
{
|
||||
Vector3 toCorner = data.Corners[data.CurrentCornerIndex] - pos;
|
||||
toCorner.y = 0f;
|
||||
|
||||
if (toCorner.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
data.CurrentCornerIndex++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void StopMovementInputs(MovementController movementController)
|
||||
{
|
||||
var emptyInputs = new AICharacterInputs
|
||||
{
|
||||
@@ -239,13 +250,11 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
movementController.Motor.BaseVelocity = Vector3.zero;
|
||||
}
|
||||
|
||||
private int CalculatePathFrames(Vector3[] corners, MovementController movementController)
|
||||
private static int CalculatePathFrames(Vector3[] corners, MovementController movementController)
|
||||
{
|
||||
if (corners == null || corners.Length < 2)
|
||||
return 0;
|
||||
if (corners == null || corners.Length < 2) return 0;
|
||||
|
||||
float totalDistanceXZ = 0f;
|
||||
|
||||
for (int i = 1; i < corners.Length; i++)
|
||||
{
|
||||
Vector3 segment = corners[i] - corners[i - 1];
|
||||
@@ -256,10 +265,7 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
float effectiveDistance = Mathf.Max(0f, totalDistanceXZ - StoppingDistance);
|
||||
float stepPerFrame = movementController.MaxStableMoveSpeed * BattleTimeline.TickDelta;
|
||||
|
||||
if (stepPerFrame <= 0.0001f)
|
||||
return 0;
|
||||
|
||||
return Mathf.Max(1, Mathf.CeilToInt(effectiveDistance / stepPerFrame));
|
||||
return stepPerFrame <= 0.0001f ? 0 : Mathf.Max(1, Mathf.CeilToInt(effectiveDistance / stepPerFrame));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user