Ghost look ahead

This commit is contained in:
2026-06-18 11:41:23 +02:00
parent 64e445e8a6
commit e4ffe97c03
2 changed files with 46 additions and 25 deletions

View File

@@ -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,13 +190,23 @@ namespace Marro.PacManUdon
PerformOffgridRelatedMovement(position, ref nextPosition);
}
if (!offGrid && followingPredefinedPath)
if (!offGrid)
{
nextPosition = ProcessPredefinedPath(position, nextPosition);
}
else if (!offGrid && CrossesTileCenter(position, nextPosition, direction))
{
TryToTurn(position, ref nextPosition);
if (followingPredefinedPath)
{
ProcessPredefinedPath(position, ref nextPosition);
}
else if (CrossesTileCenter(position, nextPosition, direction))
{
if (targetDirection != direction)
{
ApplyTargetDirection(position, out nextPosition);
}
else
{
TryToQueueTurn(position);
}
}
}
if (CrossesTileBorder(position, nextPosition, direction))
@@ -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);
}

View File

@@ -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;