diff --git a/Assets/Scripts/Ghost.cs b/Assets/Scripts/Ghost.cs index 8b996df..f76bbaf 100644 --- a/Assets/Scripts/Ghost.cs +++ b/Assets/Scripts/Ghost.cs @@ -70,6 +70,7 @@ namespace Marro.PacManUdon private bool isScared; private bool scattering; private PacManGhostFrozenState frozenState; + private Direction targetDirection; // Home private bool offGrid; @@ -174,6 +175,7 @@ namespace Marro.PacManUdon { var newDirection = GetInverseDirection(direction); SetDirection(newDirection); + SetTargetDirection(newDirection); turnAroundSoon = false; return nextPosition; } @@ -188,15 +190,25 @@ namespace Marro.PacManUdon PerformOffgridRelatedMovement(position, ref nextPosition); } - if (!offGrid && followingPredefinedPath) + if (!offGrid) { - nextPosition = ProcessPredefinedPath(position, nextPosition); + if (followingPredefinedPath) + { + ProcessPredefinedPath(position, ref nextPosition); + } + else if (CrossesTileCenter(position, nextPosition, direction)) + { + if (targetDirection != direction) + { + ApplyTargetDirection(position, out nextPosition); + } + else + { + TryToQueueTurn(position); + } + } } - else if (!offGrid && CrossesTileCenter(position, nextPosition, direction)) - { - TryToTurn(position, ref nextPosition); - } - + if (CrossesTileBorder(position, nextPosition, direction)) { var inTunnel = collisionManager.GhostMoveToTile(nextPosition, Index); @@ -238,6 +250,7 @@ namespace Marro.PacManUdon if ((XAxisAlligned || YAxisAlligned) && offGrid) { SetDirection(GetOffGridDirectionToTarget(nextPosition, target, direction)); + SetTargetDirection(direction); // Debug.Log($"{gameObject} Alligned X Axis: {XAxisAlligned}, Y Axis: {YAxisAlligned} with position: {position}, new nextPosition: {nextPosition}, new target: {target}, now moving in direction {direction}"); // nextPosition = GridMover.GetNextPosition(position, direction, speed); } @@ -245,10 +258,10 @@ namespace Marro.PacManUdon return nextPosition; } - private void TryToTurn(Vector2 position, ref Vector2 nextPosition) + private void TryToQueueTurn(Vector2 position) { - var gridPosition = PositionToGrid(position); - var availableDirections = collisionManager.GetAvailableDirections(position); + var upcomingGridPosition = PositionToGrid(position + directionVectors[(int)direction]); + var availableDirections = collisionManager.GetAvailableDirections(upcomingGridPosition); if ((availableDirections & (int)PacManCollisionInfoType.NoTurn) != 0 ) { @@ -259,23 +272,35 @@ namespace Marro.PacManUdon if (!isScared && (availableDirections & (int)PacManCollisionInfoType.HorizontalOnly) != 0) { - Debug.Log($"{name} Horizontal only!"); availableDirections &= ~0b0011; } - target = GetGridTarget(gridPosition); - var newDirection = GetGridDirectionToTargetGreedy(availableDirections, gridPosition, target); + target = GetGridTarget(upcomingGridPosition); + var newDirection = GetGridDirectionToTargetGreedy(availableDirections, upcomingGridPosition, target); if (newDirection == direction) { return; } + SetTargetDirection(newDirection); //Debug.Log($"{gameObject.name} Turned from direction {direction} to direction {newDirection}"); - nextPosition = GetNextPosition(gridPosition, directionVectors[(int)newDirection], speed, networkManager.SyncedDeltaTime); - SetDirection(newDirection); + } - private Vector2 ProcessPredefinedPath(Vector2 position, Vector2 nextPosition) + private void ApplyTargetDirection(Vector2 position, out Vector2 nextPosition) + { + var gridPosition = PositionToGrid(position); + nextPosition = GetNextPosition(gridPosition, directionVectors[(int)targetDirection], speed, networkManager.SyncedDeltaTime); + SetDirection(targetDirection); + } + + protected void SetTargetDirection(Direction direction) + { + targetDirection = direction; + UpdateAnimator(); + } + + private void ProcessPredefinedPath(Vector2 position, ref Vector2 nextPosition) { if (CrossesTileCenter(position, nextPosition, direction)) { @@ -291,6 +316,7 @@ namespace Marro.PacManUdon if (nextValidDirectionIndex == predefinedPathIndex) { SetDirection(predefinedPath[nextValidDirectionIndex]); + SetTargetDirection(direction); nextPosition = PositionToGrid(nextPosition) + GetVector(direction) * 0.01f; // Check if we've reached the end of the path, which includes making sure the path doesn't end on Vector2.zero @@ -310,7 +336,6 @@ namespace Marro.PacManUdon predefinedPathIndex++; } } - return nextPosition; } Vector2 GetGridTarget(Vector2 gridPosition) @@ -418,6 +443,7 @@ namespace Marro.PacManUdon offGrid = false; SetState(PacManGhostState.Normal); SetDirection(Direction.Left); + SetTargetDirection(Direction.Left); break; } } @@ -432,6 +458,7 @@ namespace Marro.PacManUdon { SetDirection(GetOffGridDirectionToTarget(GetPosition(), newTarget, Direction.Down)); } + SetTargetDirection(direction); // Debug.Log($"{gameObject} SetOffGridTarget with position {GetPosition()}, newTarget {newTarget}, startHorizontal {startHorizontal} resulted in direction {direction}"); target = newTarget; } @@ -531,9 +558,9 @@ namespace Marro.PacManUdon animator.SetFloat("DirX", 0); animator.SetFloat("DirY", -1); } - else if (specialLook || direction != Direction.Zero) + else if (specialLook || targetDirection != Direction.Zero) { - var vector = GetVector(direction); + var vector = GetVector(targetDirection); animator.SetFloat("DirX", vector.x); animator.SetFloat("DirY", vector.y); } diff --git a/Assets/Scripts/GridMover.cs b/Assets/Scripts/GridMover.cs index 0cff236..573a0d1 100644 --- a/Assets/Scripts/GridMover.cs +++ b/Assets/Scripts/GridMover.cs @@ -66,12 +66,6 @@ namespace Marro.PacManUdon UpdateAnimator(); } - public void SetDirection(Vector2 vector) - { - direction = VectorToDirection(vector); - UpdateAnimator(); - } - protected static Direction VectorToDirection(Vector2 vector) { var directionId = 0;