using Code.Game.Gameplay.Player; using Code.OfficeGame.BattleMode.Actions.Deciders; using Code.OfficeGame.BattleMode.Timeline; using UnityEngine; using UnityEngine.AI; namespace Code.OfficeGame.BattleMode.Actions { public class BattleActionMove : BattleAction { private const float StoppingDistance = 0.15f; private const float RotateDistance = 1f; private const float NavMeshSampleRadius = 1.5f; private const float TurnSpeedDegPerSec = 720f; private const int StuckExtraFrames = 90; 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(); var positionDecisionResult = (PositionBattleActionDeciderResult)result; Vector3 startPos = entity.KinematicCharacterMotor.TransientPosition; Vector3 targetPos = positionDecisionResult.Position; Vector3 toTargetXZ = targetPos - startPos; toTargetXZ.y = 0f; 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 { float stepPerFrame = TurnSpeedDegPerSec * BattleTimeline.TickDelta; data.TotalFrames = Mathf.Max(2, Mathf.CeilToInt(angle / stepPerFrame)); } return data; } bool hasStart = NavMesh.SamplePosition(startPos, out NavMeshHit startHit, NavMeshSampleRadius, NavMesh.AllAreas); bool hasTarget = NavMesh.SamplePosition(targetPos, out NavMeshHit targetHit, NavMeshSampleRadius, NavMesh.AllAreas); if (!hasStart || !hasTarget) { data.TotalFrames = 0; data.Finished = true; return data; } Vector3 validStart = startHit.position; Vector3 validTarget = targetHit.position; if (!NavMesh.Raycast(validStart, validTarget, out _, NavMesh.AllAreas)) { 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 && path.corners.Length > 1) { path.corners[0] = startPos; path.corners[^1] = validTarget; data.Corners = path.corners; data.CurrentCornerIndex = 1; data.TotalFrames = CalculatePathFrames(data.Corners, entity.MovementController); } else { data.TotalFrames = 0; data.Finished = true; } return data; } public override void Tick( float tickDelta, EntityProperties entity, BattleActionDecisionResult battleActionDecisionResult, BattleActionData battleActionData) { 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) { float angle = Quaternion.Angle(movementController.Motor.TransientRotation, positionDecisionResult.Rotation); int framesLeft = Mathf.Max(1, movementActionData.TotalFrames - movementActionData.CurrentFrame + 1); float stepAngle = angle / framesLeft; Quaternion nextRot = Quaternion.RotateTowards( movementController.Motor.TransientRotation, positionDecisionResult.Rotation, stepAngle); entity.KinematicCharacterMotor.SetRotation(nextRot); StopMovementInputs(movementController); if (movementActionData.CurrentFrame >= movementActionData.TotalFrames || angle <= 0.5f) { entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation); battleActionData.Finished = true; } return; } if (movementActionData.Corners == null || movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length) { entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation); StopMovementInputs(movementController); battleActionData.Finished = true; return; } if (!movementActionData.WasStarted) { movementActionData.WasStarted = true; entity.animator.applyRootMotion = false; entity.animator.CrossFadeInFixedTime(MoveStateHash, 10 * BattleTimeline.TickDelta); } Vector3 currentPos = entity.KinematicCharacterMotor.TransientPosition; AdvanceCorners(movementActionData, currentPos); if (movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length) { entity.KinematicCharacterMotor.SetRotation(positionDecisionResult.Rotation); StopMovementInputs(movementController); battleActionData.Finished = true; return; } Vector3 targetCorner = movementActionData.Corners[movementActionData.CurrentCornerIndex]; Vector3 moveDirection = targetCorner - currentPos; moveDirection.y = 0f; float distance = moveDirection.magnitude; Vector3 moveNormal = distance > 0.0001f ? moveDirection / distance : Vector3.zero; bool isLastCorner = movementActionData.CurrentCornerIndex == movementActionData.Corners.Length - 1; bool preLastPoint = isLastCorner && distance < RotateDistance; float maxStep = movementController.MaxStableMoveSpeed * tickDelta; float inputMagnitude = (isLastCorner && distance < maxStep && maxStep > 0f) ? distance / maxStep : 1f; Quaternion lookRotation = preLastPoint ? positionDecisionResult.Rotation : (moveNormal.sqrMagnitude > 0.0001f ? Quaternion.LookRotation(moveNormal, movementController.Motor.CharacterUp) : movementController.Motor.TransientRotation); var aiInputs = new AICharacterInputs { 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 toFinal = movementActionData.Corners[^1] - postMovePos; toFinal.y = 0f; 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); StopMovementInputs(entity.MovementController); battleActionData.Finished = true; } } 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 { MoveVector = Vector3.zero, Look = movementController.Motor.TransientRotation }; movementController.SetInputs(ref emptyInputs); movementController.Motor.BaseVelocity = Vector3.zero; } private static int CalculatePathFrames(Vector3[] corners, MovementController movementController) { 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]; segment.y = 0f; totalDistanceXZ += segment.magnitude; } float effectiveDistance = Mathf.Max(0f, totalDistanceXZ - StoppingDistance); float stepPerFrame = movementController.MaxStableMoveSpeed * BattleTimeline.TickDelta; return stepPerFrame <= 0.0001f ? 0 : Mathf.Max(1, Mathf.CeilToInt(effectiveDistance / stepPerFrame)); } } }