Switched to Direction enum

This commit is contained in:
2026-06-15 10:29:36 +02:00
parent 2ccf77c0eb
commit 73b3194c51
25 changed files with 2874 additions and 1987 deletions

View File

@@ -41,6 +41,7 @@ namespace Marro.PacManUdon
// External references
private GhostManager ghostManager;
private PelletManager pelletManager;
private Animator animator;
private new Renderer renderer;
private PacMan pacMan;
@@ -88,12 +89,13 @@ namespace Marro.PacManUdon
public bool IsScared => isScared;
public int Index { get; private set; }
public void Initialize(PacMan pacMan, Ghost blinky, Transform startTransform, Vector2 homePosition, Vector2 idlePosition1, Vector2 idlePosition2, Vector2 cornerPosition, int index)
public void Initialize(PelletManager pelletManager, PacMan pacMan, Ghost blinky, Transform startTransform, Vector2 homePosition, Vector2 idlePosition1, Vector2 idlePosition2, Vector2 cornerPosition, int index)
{
ghostManager = transform.parent.GetComponent<GhostManager>();
animator = GetComponent<Animator>();
renderer = GetComponent<Renderer>();
this.pelletManager = pelletManager;
this.pacMan = pacMan;
this.blinky = blinky;
this.homePosition = homePosition;
@@ -158,7 +160,7 @@ namespace Marro.PacManUdon
}
Vector2 position = GetPosition();
Vector2 nextPosition = GridMoverTools.GetNextPosition(position, direction, speed, networkManager.SyncedDeltaTime);
Vector2 nextPosition = GetNextPosition(position, directionVectors[(int)direction], speed, networkManager.SyncedDeltaTime);
nextPosition = ProcessNextPosition(position, nextPosition);
@@ -168,10 +170,11 @@ namespace Marro.PacManUdon
private Vector2 ProcessNextPosition(Vector2 position, Vector2 nextPosition)
{
if (turnAroundSoon && ghostState == PacManGhostState.Normal
&& GridMoverTools.CrossesTileBorder(position, nextPosition, direction.x != 0, direction.y != 0))
&& CrossesTileBorder(position, nextPosition, direction))
{
SetDirection(direction * -1);
//Debug.Log($"{gameObject} turned around to direction {GetDirection()}");
var newDirection = GetInverseDirection(direction);
Debug.Log($"{gameObject} turned around from direction {direction} to {newDirection}");
SetDirection(newDirection);
turnAroundSoon = false;
return nextPosition;
}
@@ -216,21 +219,19 @@ namespace Marro.PacManUdon
{
nextPosition = ProcessPredefinedPath(position, nextPosition);
}
else if (!offGrid && GridMoverTools.CrossesTileCenter(position, nextPosition, direction.x != 0, direction.y != 0))
else if (!offGrid && CrossesTileCenter(position, nextPosition, direction))
{
Vector2 gridPosition = GridMoverTools.PositionToGrid(position);
Vector2[] availableDirections = GetAvailableDirections(gridPosition, direction * -1);
if (availableDirections.Length > 1)
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)
{
target = GetGridTarget(gridPosition);
SetDirection(GetGridDirectionToTargetGreedy(availableDirections, gridPosition, target));
nextPosition = GridMoverTools.GetNextPosition(gridPosition, direction, speed, networkManager.SyncedDeltaTime);
// Debug.Log($"GetNextPosition at gridPosition {gridPosition} with direction {direction} and speed {speed} gives nextPosition {nextPosition}");
}
else if (availableDirections.Length == 1 && availableDirections[0] != direction)
{
SetDirection(availableDirections[0]);
nextPosition = GridMoverTools.GetNextPosition(gridPosition, direction, speed, networkManager.SyncedDeltaTime);
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}");
}
@@ -244,9 +245,26 @@ namespace Marro.PacManUdon
return nextPosition;
}
private int GetIllegalCardinalDirection(Direction direction)
{
switch (direction)
{
case Direction.Up:
return 1;
case Direction.Down:
return 0;
case Direction.Left:
return 3;
case Direction.Right:
return 2;
default:
return 0;
}
}
private Vector2 ProcessPredefinedPath(Vector2 position, Vector2 nextPosition)
{
if (GridMoverTools.CrossesTileCenter(position, nextPosition, direction.x != 0, direction.y != 0))
if (CrossesTileCenter(position, nextPosition, direction))
{
// Find the next valid direction which isn't Vector2.zero
int nextValidDirectionIndex = predefinedPathIndex;
@@ -254,13 +272,13 @@ namespace Marro.PacManUdon
{
nextValidDirectionIndex += 1;
}
if (!GridMoverTools.CheckCollisionInDirection(transform, nextPosition, predefinedPath[nextValidDirectionIndex]))
if (!CheckCollisionInDirection(transform, nextPosition, predefinedPath[nextValidDirectionIndex]))
{
// If we're at a Vector2.zero, we skip applying the direction and only increment.
if (nextValidDirectionIndex == predefinedPathIndex)
{
SetDirection(predefinedPath[nextValidDirectionIndex]);
nextPosition = GridMoverTools.PositionToGrid(nextPosition) + direction.normalized * 0.01f;
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
do
@@ -323,15 +341,15 @@ namespace Marro.PacManUdon
default:
return gridPosition;
case PacManGhostType.Blinky:
return GridMoverTools.PositionToGrid(pacMan.transform.localPosition);
return PositionToGrid(pacMan.transform.localPosition);
case PacManGhostType.Pinky:
return GridMoverTools.PositionToGrid(pacMan.transform.localPosition) + pacMan.GetDirection() * 4;
return PositionToGrid(pacMan.transform.localPosition) + pacMan.GetVector(direction) * 4;
case PacManGhostType.Inky:
return 2 * GridMoverTools.PositionToGrid(pacMan.transform.localPosition) + 4 * pacMan.GetDirection() - GridMoverTools.PositionToGrid(blinky.transform.localPosition);
return 2 * PositionToGrid(pacMan.transform.localPosition) + 4 * pacMan.GetVector(direction) - PositionToGrid(blinky.transform.localPosition);
case PacManGhostType.Clyde:
if (Vector2.Distance(gridPosition, GridMoverTools.PositionToGrid(pacMan.transform.localPosition)) >= 8)
if (Vector2.Distance(gridPosition, PositionToGrid(pacMan.transform.localPosition)) >= 8)
{
return GridMoverTools.PositionToGrid(pacMan.transform.localPosition);
return PositionToGrid(pacMan.transform.localPosition);
}
else
{
@@ -386,7 +404,7 @@ namespace Marro.PacManUdon
case PacManGhostState.Exiting:
offGrid = false;
SetState(PacManGhostState.Normal);
SetDirection(Vector2.left);
SetDirection(Direction.Left);
break;
}
}
@@ -395,11 +413,11 @@ namespace Marro.PacManUdon
{
if (startHorizontal)
{
SetDirection(GetOffGridDirectionToTarget(GetPosition(), newTarget, Vector2.right));
SetDirection(GetOffGridDirectionToTarget(GetPosition(), newTarget, Direction.Right));
}
else
{
SetDirection(GetOffGridDirectionToTarget(GetPosition(), newTarget, Vector2.down));
SetDirection(GetOffGridDirectionToTarget(GetPosition(), newTarget, Direction.Down));
}
// Debug.Log($"{gameObject} SetOffGridTarget with position {GetPosition()}, newTarget {newTarget}, startHorizontal {startHorizontal} resulted in direction {direction}");
target = newTarget;
@@ -413,69 +431,42 @@ namespace Marro.PacManUdon
return rngState;
}
Vector2[] GetAvailableDirections(Vector2 gridPosition, Vector2 discardDirection)
private Direction[] cardinalDirections = new Direction[] { Direction.Up, Direction.Down, Direction.Left, Direction.Right };
Direction GetGridDirectionToTargetGreedy(bool[] blockedDirections, Vector2 gridPosition, Vector2 targetGridPosition)
{
Vector2[] directions;
Vector2[] availableDirections;
if (horizontalOnly && ghostState == PacManGhostState.Normal)
Direction bestDirection = Direction.Zero;
float bestDistance = float.MaxValue;
for (int i = horizontalOnly ? 2 : 0; i < blockedDirections.Length; i++)
{
directions = new Vector2[] { new Vector2(1, 0), new Vector2(-1, 0) };
availableDirections = new Vector2[2];
}
else
{
directions = new Vector2[] { new Vector2(0, 1), new Vector2(1, 0), new Vector2(0, -1), new Vector2(-1, 0) };
availableDirections = new Vector2[4];
}
int availableDirectionsNum = 0;
for (int i = 0; i < directions.Length; i++)
{
if (directions[i].Equals(discardDirection) || GridMoverTools.CheckCollisionInDirection(transform, gridPosition, directions[i]))
if (blockedDirections[i])
{
continue;
}
availableDirections[availableDirectionsNum] = directions[i];
availableDirectionsNum++;
}
Vector2[] availableDirectionsTrimmed = new Vector2[availableDirectionsNum];
// Debug.Log($"{gameObject} Number of available directions: {availableDirectionsTrimmed.Length}");
Array.Copy(availableDirections, 0, availableDirectionsTrimmed, 0, availableDirectionsNum);
return availableDirectionsTrimmed;
}
Vector2 GetGridDirectionToTargetGreedy(Vector2[] availableDirections, Vector2 gridPosition, Vector2 targetGridPosition)
{
Vector2 bestDirection = Vector2.zero;
float bestDistance = float.MaxValue;
for (int i = 0; i < availableDirections.Length; i++)
{
Vector2 direction = availableDirections[i];
// Debug.Log("Evaluating direction " + direction);
float distance = Vector2.Distance(gridPosition + direction, targetGridPosition);
var direction = cardinalDirections[i];
float distance = Vector2.Distance(gridPosition + directionVectors[(int)direction], targetGridPosition);
if (distance < bestDistance)
{
bestDistance = distance;
bestDirection = direction;
}
}
// Debug.Log("Closest next tile is in direction " + bestDirection);
Debug.Log($"{gameObject.name} Closest next tile is in direction {bestDirection}");
return bestDirection;
}
Vector2 GetOffGridDirectionToTarget(Vector2 position, Vector2 target, Vector2 direction)
private Direction GetOffGridDirectionToTarget(Vector2 position, Vector2 target, Direction direction)
{
if ((direction.x != 0 && position.x != target.x) || position.y == target.y)
if ((IsHorizontal(direction) && position.x != target.x) || position.y == target.y)
{
// Debug.Log($"{gameObject} getOffGridDirectionToTarget with position {position}, target {target} and direction {direction} gives movement on the X axis in direction {new Vector2(target.x-position.x, 0).normalized}");
return new Vector2(target.x - position.x, 0).normalized;
return HorizontalToDirection(target.x - position.x);
}
else
{
// Debug.Log($"{gameObject} getOffGridDirectionToTarget with position {position}, target {target} and direction {direction} gives movement on the Y axis in direction {new Vector2(0, target.y-position.y).normalized}");
return new Vector2(0, target.y - position.y).normalized;
return VerticalToDirection(target.y - position.y);
}
}
@@ -526,10 +517,11 @@ namespace Marro.PacManUdon
animator.SetFloat("DirX", 0);
animator.SetFloat("DirY", -1);
}
else if (specialLook || !direction.Equals(Vector2.zero))
else if (specialLook || direction != Direction.Zero)
{
animator.SetFloat("DirX", direction.x);
animator.SetFloat("DirY", direction.y);
var vector = GetVector(direction);
animator.SetFloat("DirX", vector.x);
animator.SetFloat("DirY", vector.y);
}
}