Better action system
This commit is contained in:
1 parent
2bd71f8dc5
commit
71bb4c3b5b
110 files changed
+73523
-86288
No files matched your search
@@ -2,6 +2,7 @@ using System.Threading;
|
||||
using Code.Game.Gameplay.Player;
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.Infrastructure.Input;
|
||||
using Code.OfficeGame.BattleMode.Actions.Deciders;
|
||||
using Code.OfficeGame.BattleMode.Actions.Player;
|
||||
using Code.OfficeGame.BattleMode.Marker;
|
||||
using Code.OfficeGame.BattleMode.Timeline;
|
||||
@@ -16,79 +17,61 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
{
|
||||
public class BattleActionMove : BattleAction
|
||||
{
|
||||
private CameraStateMachine _cameraStateMachine;
|
||||
private IInputService _inputService;
|
||||
private MovementController _movementController;
|
||||
|
||||
private NavMeshPath _path;
|
||||
private Vector3[] _corners;
|
||||
private int _currentCornerIndex;
|
||||
|
||||
private Vector3 _targetPosition;
|
||||
private LayerMask _navMeshMask;
|
||||
|
||||
private const float StoppingDistance = 0.15f;
|
||||
|
||||
[Inject]
|
||||
public void Construct(
|
||||
CameraStateMachine cameraStateMachine,
|
||||
IInputService inputService,
|
||||
MarkerManager markerManager)
|
||||
public override bool Tick(
|
||||
float tickDelta,
|
||||
EntityProperties entity,
|
||||
BattleActionDecisionResult battleActionDecisionResult,
|
||||
BattleActionData battleActionData)
|
||||
{
|
||||
_markerManager = markerManager;
|
||||
_cameraStateMachine = cameraStateMachine;
|
||||
_inputService = inputService;
|
||||
_movementController = GetComponent<MovementController>();
|
||||
_navMeshMask = LayerMask.GetMask("Environment");
|
||||
}
|
||||
var movementActionData = (MovementBattleActionData) battleActionData;
|
||||
|
||||
var entityMovementController = entity.MovementController;
|
||||
|
||||
public override void Tick(float tickDelta)
|
||||
{
|
||||
if (Finished) return;
|
||||
|
||||
if (_corners == null || _currentCornerIndex >= _corners.Length)
|
||||
if (movementActionData.Corners == null || movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length)
|
||||
{
|
||||
StopMovementAndFinish();
|
||||
return;
|
||||
StopMovementAndFinish(entityMovementController);
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector3 currentPos = _movementController.Motor.TransientPosition;
|
||||
Vector3 currentPos = entity.KinematicCharacterMotor.TransientPosition;
|
||||
|
||||
while (_currentCornerIndex < _corners.Length)
|
||||
while (movementActionData.CurrentCornerIndex < movementActionData.Corners.Length)
|
||||
{
|
||||
Vector3 toCorner = _corners[_currentCornerIndex] - currentPos;
|
||||
Vector3 toCorner = movementActionData.Corners[movementActionData.CurrentCornerIndex] - currentPos;
|
||||
toCorner.y = 0f;
|
||||
|
||||
if (toCorner.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
{
|
||||
_currentCornerIndex++;
|
||||
}
|
||||
movementActionData.CurrentCornerIndex++;
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_currentCornerIndex >= _corners.Length)
|
||||
if (movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length)
|
||||
{
|
||||
StopMovementAndFinish();
|
||||
StopMovementAndFinish(entity.MovementController);
|
||||
KinematicCharacterSystem.Simulate(
|
||||
tickDelta,
|
||||
KinematicCharacterSystem.CharacterMotors,
|
||||
KinematicCharacterSystem.PhysicsMovers
|
||||
);
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector3 targetCorner = _corners[_currentCornerIndex];
|
||||
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;
|
||||
|
||||
float maxStep = _movementController.MaxStableMoveSpeed * tickDelta;
|
||||
float inputMagnitude = (_currentCornerIndex == _corners.Length - 1 && distance < maxStep && maxStep > 0f)
|
||||
|
||||
float maxStep = entityMovementController.MaxStableMoveSpeed * tickDelta;
|
||||
float inputMagnitude =
|
||||
(movementActionData.CurrentCornerIndex == movementActionData.Corners.Length - 1
|
||||
&& distance < maxStep
|
||||
&& maxStep > 0f)
|
||||
? distance / maxStep
|
||||
: 1f;
|
||||
|
||||
@@ -97,11 +80,11 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
{
|
||||
MoveVector = moveNormal * inputMagnitude,
|
||||
Look = moveNormal.sqrMagnitude > 0.0001f
|
||||
? Quaternion.LookRotation(moveNormal, _movementController.Motor.CharacterUp)
|
||||
: _movementController.Motor.TransientRotation
|
||||
? Quaternion.LookRotation(moveNormal, entityMovementController.Motor.CharacterUp)
|
||||
: entityMovementController.Motor.TransientRotation
|
||||
};
|
||||
|
||||
_movementController.SetInputs(ref aiInputs);
|
||||
entityMovementController.SetInputs(ref aiInputs);
|
||||
|
||||
KinematicCharacterSystem.Simulate(
|
||||
tickDelta,
|
||||
@@ -109,110 +92,58 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
KinematicCharacterSystem.PhysicsMovers
|
||||
);
|
||||
|
||||
Vector3 postMovePos = _movementController.Motor.TransientPosition;
|
||||
Vector3 toFinal = _corners[^1] - postMovePos;
|
||||
Vector3 postMovePos = entityMovementController.Motor.TransientPosition;
|
||||
Vector3 toFinal = movementActionData.Corners[^1] - postMovePos;
|
||||
toFinal.y = 0f;
|
||||
|
||||
if (_currentCornerIndex >= _corners.Length - 1 && toFinal.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
if (movementActionData.CurrentCornerIndex >= movementActionData.Corners.Length - 1
|
||||
&& toFinal.sqrMagnitude <= StoppingDistance * StoppingDistance)
|
||||
{
|
||||
StopMovementAndFinish();
|
||||
StopMovementAndFinish(entityMovementController);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void StopMovementAndFinish()
|
||||
private void StopMovementAndFinish(MovementController movementController)
|
||||
{
|
||||
Debug.Log("MovementFinished");
|
||||
var emptyInputs = new AICharacterInputs
|
||||
{
|
||||
MoveVector = Vector3.zero,
|
||||
Look = _movementController.Motor.TransientRotation
|
||||
Look = movementController.Motor.TransientRotation
|
||||
};
|
||||
_movementController.SetInputs(ref emptyInputs);
|
||||
_movementController.Motor.BaseVelocity = Vector3.zero;
|
||||
|
||||
Finished = true;
|
||||
}
|
||||
|
||||
private CancellationTokenSource _waitForEnterTokenSource;
|
||||
private BattleActionDecisionResultCode _decisionResultCode;
|
||||
|
||||
public override async UniTask<BattleActionDecisionResult> DecideAction()
|
||||
{
|
||||
_waitForEnterTokenSource = new CancellationTokenSource();
|
||||
_decisionResultCode = BattleActionDecisionResultCode.CancelDecision;
|
||||
_targetPosition = _movementController.transform.position;
|
||||
|
||||
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Select,
|
||||
SelectPosition);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Confirm,
|
||||
ConfirmPosition);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Cancel,
|
||||
CancelPosition);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Watchment,
|
||||
MarkPosition);
|
||||
|
||||
_playerLockedDecisionMarker = _markerManager.GetMarkerInstance();
|
||||
_playerDecisionMarker = _markerManager.GetMarkerInstance();
|
||||
_playerDecisionMarker.Toggle(true);
|
||||
|
||||
await UniTask.WaitUntilCanceled(_waitForEnterTokenSource.Token).SuppressCancellationThrow();
|
||||
|
||||
_playerDecisionMarker.Toggle(false);
|
||||
_markerManager.ReleaseMarkerInstance(_playerDecisionMarker);
|
||||
_markerManager.ReleaseMarkerInstance(_playerLockedDecisionMarker);
|
||||
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Select,
|
||||
SelectPosition);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Confirm,
|
||||
ConfirmPosition);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Cancel,
|
||||
CancelPosition);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Watchment,
|
||||
MarkPosition);
|
||||
|
||||
_waitForEnterTokenSource.Dispose();
|
||||
_waitForEnterTokenSource = null;
|
||||
|
||||
Finished = _decisionResultCode != BattleActionDecisionResultCode.Decided;
|
||||
|
||||
return new() { ResultCode = _decisionResultCode, FrameDuration = _totalFrames};
|
||||
movementController.SetInputs(ref emptyInputs);
|
||||
movementController.Motor.BaseVelocity = Vector3.zero;
|
||||
}
|
||||
|
||||
public override BattleActionType ActionType => BattleActionType.Move;
|
||||
public override DeciderType DeciderType => DeciderType.Position;
|
||||
|
||||
private int _totalFrames;
|
||||
private MarkerManager _markerManager;
|
||||
|
||||
private void CancelPosition(InputAction.CallbackContext obj)
|
||||
public override BattleActionData MountAndGetFrames(EntityProperties entity, BattleActionDecisionResult result)
|
||||
{
|
||||
_decisionResultCode = BattleActionDecisionResultCode.CancelDecision;
|
||||
_waitForEnterTokenSource?.Cancel();
|
||||
}
|
||||
|
||||
private void ConfirmPosition(InputAction.CallbackContext input)
|
||||
{
|
||||
Vector3 startPos = _movementController.Motor.TransientPosition;
|
||||
|
||||
_path = new();
|
||||
MovementBattleActionData movementBattleActionData = new();
|
||||
|
||||
if ((_targetPosition - startPos).sqrMagnitude > StoppingDistance * StoppingDistance
|
||||
&& NavMesh.CalculatePath(startPos, _targetPosition, NavMesh.AllAreas, _path)
|
||||
&& _path.corners.Length > 0)
|
||||
{
|
||||
_corners = _path.corners;
|
||||
_currentCornerIndex = _corners.Length > 1 ? 1 : 0;
|
||||
_decisionResultCode = BattleActionDecisionResultCode.Decided;
|
||||
_totalFrames = CalculatePathFrames(_corners);
|
||||
}
|
||||
else
|
||||
{
|
||||
_decisionResultCode = BattleActionDecisionResultCode.Deciding;
|
||||
_totalFrames = 0;
|
||||
}
|
||||
Vector3 startPos = entity.KinematicCharacterMotor.TransientPosition;
|
||||
|
||||
_waitForEnterTokenSource?.Cancel();
|
||||
NavMeshPath path = new();
|
||||
|
||||
var positionDecisionResult = (PositionBattleActionDeciderResult) result;
|
||||
|
||||
if ((positionDecisionResult.Position - startPos).sqrMagnitude > StoppingDistance * StoppingDistance
|
||||
&& NavMesh.CalculatePath(startPos, positionDecisionResult.Position, NavMesh.AllAreas, path)
|
||||
&& path.corners.Length > 0)
|
||||
{
|
||||
movementBattleActionData.Corners = path.corners;
|
||||
movementBattleActionData.CurrentCornerIndex = movementBattleActionData.Corners.Length > 1 ? 1 : 0;
|
||||
movementBattleActionData.TotalFrames = CalculatePathFrames(movementBattleActionData.Corners, entity.MovementController);
|
||||
}
|
||||
else movementBattleActionData.TotalFrames = 0;
|
||||
|
||||
return movementBattleActionData;
|
||||
}
|
||||
private int CalculatePathFrames(Vector3[] corners)
|
||||
private int CalculatePathFrames(Vector3[] corners, MovementController movementController)
|
||||
{
|
||||
if (corners == null || corners.Length < 2)
|
||||
return 0;
|
||||
@@ -228,42 +159,12 @@ namespace Code.OfficeGame.BattleMode.Actions
|
||||
|
||||
float effectiveDistance = Mathf.Max(0f, totalDistanceXZ - StoppingDistance);
|
||||
|
||||
float stepPerFrame = _movementController.MaxStableMoveSpeed * BattleTimeline.TickDelta;
|
||||
float stepPerFrame = movementController.MaxStableMoveSpeed * BattleTimeline.TickDelta;
|
||||
|
||||
if (stepPerFrame <= 0.0001f)
|
||||
return 0;
|
||||
|
||||
// Округляем вверх, так как последний неполный шаг тоже занимает 1 целый кадр
|
||||
return Mathf.CeilToInt(effectiveDistance / stepPerFrame);
|
||||
}
|
||||
|
||||
private RaycastHit[] _hitBuffer = new RaycastHit[1];
|
||||
private Vector3 _lastViewedPosition;
|
||||
private PlayerDecisionMarker _playerDecisionMarker;
|
||||
private PlayerDecisionMarker _playerLockedDecisionMarker;
|
||||
|
||||
private void MarkPosition(InputAction.CallbackContext input)
|
||||
{
|
||||
var position = _cameraStateMachine.Camera.transform.position;
|
||||
var direction = _cameraStateMachine.Camera.transform.forward;
|
||||
|
||||
var hitCount = Physics.RaycastNonAlloc(position, direction, _hitBuffer, Mathf.Infinity, _navMeshMask);
|
||||
|
||||
if (hitCount == 0) return;
|
||||
|
||||
var hit = _hitBuffer[0];
|
||||
|
||||
_playerDecisionMarker.MoveMarker(hit.point);
|
||||
|
||||
_lastViewedPosition = hit.point;
|
||||
}
|
||||
|
||||
private void SelectPosition(InputAction.CallbackContext input)
|
||||
{
|
||||
_targetPosition = _lastViewedPosition;
|
||||
|
||||
_playerLockedDecisionMarker.MoveMarker(_targetPosition);
|
||||
_playerLockedDecisionMarker.Toggle(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user