Ghost turn points baked

This commit is contained in:
2026-06-15 13:01:10 +02:00
parent 77681fe0ca
commit 8610c6be53
7 changed files with 1274 additions and 1091 deletions

View File

@@ -173,7 +173,6 @@ namespace Marro.PacManUdon
&& CrossesTileCenter(position, nextPosition, direction))
{
var newDirection = GetInverseDirection(direction);
Debug.Log($"{gameObject} turned around from direction {direction} to {newDirection}");
SetDirection(newDirection);
turnAroundSoon = false;
return nextPosition;
@@ -186,54 +185,16 @@ namespace Marro.PacManUdon
if (offGrid || ghostState == PacManGhostState.Returning)
{
bool XAxisAlligned = CheckAndAllignToTargetX(position, nextPosition, target);
bool YAxisAlligned = CheckAndAllignToTargetY(position, nextPosition, target);
if (offGrid)
{
if (XAxisAlligned)
{
nextPosition.x = target.x;
}
if (YAxisAlligned)
{
nextPosition.y = target.y;
}
}
if (XAxisAlligned && YAxisAlligned)
{
if (!offGrid)
{
nextPosition = target;
}
OffGridTargetReached();
// Debug.Log($"{gameObject} reached offGridTarget {target} at position {position}, new state {state}, offGrid is now {offGrid}, new direction: {direction}");
}
if ((XAxisAlligned || YAxisAlligned) && offGrid)
{
SetDirection(GetOffGridDirectionToTarget(nextPosition, target, 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);
}
PerformOffgridRelatedMovement(position, ref nextPosition);
}
if (!offGrid && followingPredefinedPath)
{
nextPosition = ProcessPredefinedPath(position, nextPosition);
}
else if (!offGrid && CrossesTileCenter(position, nextPosition, direction))
{
var gridPosition = PositionToGrid(position);
var blockedDirections = pelletManager.GetBlockedDirections(position);
blockedDirections[GetIllegalCardinalDirection(direction)] = true; // Not allowed to turn around
target = GetGridTarget(gridPosition);
var newDirection = GetGridDirectionToTargetGreedy(blockedDirections, gridPosition, target);
if (newDirection != direction)
{
Debug.Log($"{gameObject.name} Was moving in direction {direction}, continues with moving in direction {newDirection}");
nextPosition = GetNextPosition(gridPosition, directionVectors[(int)newDirection], speed, networkManager.SyncedDeltaTime);
SetDirection(newDirection);
}
// Debug.Log($"{gameObject} crossed tile center {gridPosition}, new target: {target}, new direction: {direction}");
TryToTurn(position, ref nextPosition);
}
var distance = Vector2.Distance(position, nextPosition);
@@ -245,20 +206,78 @@ namespace Marro.PacManUdon
return nextPosition;
}
private int GetIllegalCardinalDirection(Direction direction)
private Vector2 PerformOffgridRelatedMovement(Vector2 position, ref Vector2 nextPosition)
{
bool XAxisAlligned = CheckAndAllignToTargetX(position, nextPosition, target);
bool YAxisAlligned = CheckAndAllignToTargetY(position, nextPosition, target);
if (offGrid)
{
if (XAxisAlligned)
{
nextPosition.x = target.x;
}
if (YAxisAlligned)
{
nextPosition.y = target.y;
}
}
if (XAxisAlligned && YAxisAlligned)
{
if (!offGrid)
{
nextPosition = target;
}
OffGridTargetReached();
// Debug.Log($"{gameObject} reached offGridTarget {target} at position {position}, new state {state}, offGrid is now {offGrid}, new direction: {direction}");
}
if ((XAxisAlligned || YAxisAlligned) && offGrid)
{
SetDirection(GetOffGridDirectionToTarget(nextPosition, target, 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);
}
return nextPosition;
}
private void TryToTurn(Vector2 position, ref Vector2 nextPosition)
{
var gridPosition = PositionToGrid(position);
var availableDirections = pelletManager.GetAvailableDirections(position);
if (availableDirections == 0)
{
return;
}
availableDirections &= GetIllegalCardinalDirectionMask(direction); // Not allowed to turn around
target = GetGridTarget(gridPosition);
var newDirection = GetGridDirectionToTargetGreedy(availableDirections, gridPosition, target);
if (newDirection == direction)
{
return;
}
Debug.Log($"{gameObject.name} Turned from direction {direction} to direction {newDirection}");
nextPosition = GetNextPosition(gridPosition, directionVectors[(int)newDirection], speed, networkManager.SyncedDeltaTime);
SetDirection(newDirection);
}
private int GetIllegalCardinalDirectionMask(Direction direction)
{
switch (direction)
{
case Direction.Up:
return 1;
return 0b1101;
case Direction.Down:
return 0;
return 0b1110;
case Direction.Left:
return 3;
return 0b0111;
case Direction.Right:
return 2;
default:
return 0;
return 0b1011;
default:
return 0b1111;
}
}
@@ -431,20 +450,20 @@ namespace Marro.PacManUdon
return rngState;
}
private Direction[] cardinalDirections = new Direction[] { Direction.Up, Direction.Down, Direction.Left, Direction.Right };
Direction GetGridDirectionToTargetGreedy(bool[] blockedDirections, Vector2 gridPosition, Vector2 targetGridPosition)
private readonly Direction[] cardinalDirections = new Direction[] { Direction.Up, Direction.Left, Direction.Down, Direction.Right };
private readonly Direction[] horizontalDirections = new Direction[] { Direction.Left, Direction.Right };
Direction GetGridDirectionToTargetGreedy(int availableDirections, Vector2 gridPosition, Vector2 targetGridPosition)
{
Direction bestDirection = Direction.Zero;
float bestDistance = float.MaxValue;
for (int i = horizontalOnly ? 2 : 0; i < blockedDirections.Length; i++)
foreach (var direction in horizontalOnly ? horizontalDirections : cardinalDirections)
{
if (blockedDirections[i])
if (((int)direction & availableDirections) == 0)
{
continue;
}
// Debug.Log("Evaluating direction " + direction);
var direction = cardinalDirections[i];
float distance = Vector2.Distance(gridPosition + directionVectors[(int)direction], targetGridPosition);
if (distance < bestDistance)
{