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

@@ -7,8 +7,9 @@ namespace Marro.PacManUdon
[RequireComponent(typeof(Renderer))]
public class PacMan : GridMover
{
private GameManager gameController;
private GameManager gameManager;
private PlayerInput input;
private PelletManager pelletManager;
private float defaultSpeed;
private float powerPelletSpeed;
private float speed;
@@ -40,9 +41,10 @@ namespace Marro.PacManUdon
#endregion
public void Initialize(PlayerInput input, Transform startTransform, GameManager gameController)
public void Initialize(PlayerInput input, Transform startTransform, GameManager gameManager, PelletManager pelletManager)
{
this.gameController = gameController;
this.gameManager = gameManager;
this.pelletManager = pelletManager;
this.input = input;
animator = GetComponent<Animator>();
renderer = GetComponent<Renderer>();
@@ -57,7 +59,7 @@ namespace Marro.PacManUdon
public void Reset()
{
transform.SetLocalPositionAndRotation(startPosition, startRotation);
direction = Vector2.left;
direction = Direction.Left;
targetDirection = Vector2.left;
speed = defaultSpeed;
kinematic = false;
@@ -104,7 +106,7 @@ namespace Marro.PacManUdon
}
Vector2 position = GetPosition();
Vector2 nextPosition = GridMoverTools.GetNextPosition(position, direction, speed, networkManager.SyncedDeltaTime); // The position pacman will move to, assuming it doens't get changed
Vector2 nextPosition = GetNextPosition(position, directionVectors[(int)direction], speed, networkManager.SyncedDeltaTime); // The position pacman will move to, assuming it doens't get changed
if (!kinematic)
{
@@ -123,34 +125,35 @@ namespace Marro.PacManUdon
private Vector2 ProcessNextPosition(Vector2 position, Vector2 nextPosition)
{
if (GridMoverTools.CrossesTileCenter(position, nextPosition, direction.x != 0, false) // If pacman is moving horizontally, check if he may cross the center of a tile in that axis
&& (targetDirection.x == 0 || GridMoverTools.CheckCollisionInDirection(transform, nextPosition, new Vector2(direction.x, 0))))
if (CrossesTileCenter(position, nextPosition, Direction.Left) // If pacman is moving horizontally, check if he may cross the center of a tile in that axis
&& (targetDirection.x == 0 || pelletManager.IsWallUpcoming(nextPosition, directionVectors[(int)HorizontalComponent(direction)])))
{ // If the target direction is in the other axis or if we're about to run into a wall
nextPosition.x = GridMoverTools.PositionToGrid(nextPosition).x; // Snap pacman to the center of his current tile in this axis
SetDirection(new Vector2(0, direction.y));
// Debug.Log($"{gameObject} crossed X tile center from {currentPosition}, nextPosition is now {nextPosition} and direction is now {direction}");
nextPosition.x = PositionToGrid(nextPosition).x; // Snap pacman to the center of his current tile in this axis
SetDirection(VerticalComponent(direction));
Debug.Log($"{gameObject} crossed X tile center from {position}, nextPosition is now {nextPosition} and direction is now {direction}");
}
if (GridMoverTools.CrossesTileCenter(position, nextPosition, false, direction.y != 0) // See comments above but now vertical
&& (targetDirection.y == 0 || GridMoverTools.CheckCollisionInDirection(transform, nextPosition, new Vector2(0, direction.y))))
if (CrossesTileCenter(position, nextPosition, Direction.Down) // See comments above but now vertical
&& (targetDirection.y == 0 || pelletManager.IsWallUpcoming(nextPosition, directionVectors[(int)VerticalComponent(direction)])))
{
nextPosition.y = GridMoverTools.PositionToGrid(nextPosition).y;
SetDirection(new Vector2(direction.x, 0));
// Debug.Log($"{gameObject} crossed Y tile center from {currentPosition} with targetDirection {targetDirection}, nextPosition is now {nextPosition} and direction is now {direction}");
nextPosition.y = PositionToGrid(nextPosition).y;
SetDirection(HorizontalComponent(direction));
Debug.Log($"{gameObject} crossed Y tile center from {position} with targetDirection {targetDirection}, nextPosition is now {nextPosition} and direction is now {direction}");
}
Vector2 inputDirection = input.GetDirection();
if (!inputDirection.Equals(Vector2.zero) && !inputDirection.Equals(targetDirection) // Ignore neutral input or input in our current direction
&& (inputDirection.x == 0 || (Math.Round(nextPosition.y, 5) - 0.5) % 1 != 0) && (inputDirection.y == 0 || (Math.Round(nextPosition.x, 5) - 0.5) % 1 != 0) // Target grid position near the edge of a tile may not be correct, ignore inputs near the border
&& !GridMoverTools.CheckCollisionInDirection(transform, nextPosition, inputDirection))
if (!inputDirection.Equals(Vector2.zero) && !inputDirection.Equals(targetDirection) // Ignore neutral input or input in our current direction
&& (inputDirection.x == 0 || (Math.Round(nextPosition.y, 5) - 0.5) % 1 != 0) // Target grid position near the edge of a tile may not be correct, ignore inputs near the border
&& (inputDirection.y == 0 || (Math.Round(nextPosition.x, 5) - 0.5) % 1 != 0)
&& !CheckCollisionInDirection(transform, nextPosition, inputDirection))
{ // Check if the requested direction does not have a wall
if (inputDirection.x != 0)
{ // Move in the requested direction, as well as perpundicular to it to get to the center of the tunnel
SetDirection(inputDirection + new Vector2(0, GridMoverTools.PositionToGrid(nextPosition).y - nextPosition.y).normalized);
SetDirection(inputDirection + new Vector2(0, PositionToGrid(nextPosition).y - nextPosition.y).normalized);
}
else
{
SetDirection(inputDirection + new Vector2(GridMoverTools.PositionToGrid(nextPosition).x - nextPosition.x, 0).normalized);
SetDirection(inputDirection + new Vector2(PositionToGrid(nextPosition).x - nextPosition.x, 0).normalized);
}
SetTargetDirection(inputDirection); // This is the direction most logic should assume pacman is moving, the actual direction may be different due to cornering
@@ -165,7 +168,7 @@ namespace Marro.PacManUdon
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;
@@ -173,14 +176,14 @@ 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]);
SetTargetDirection(predefinedPath[nextValidDirectionIndex]);
nextPosition = GridMoverTools.PositionToGrid(nextPosition) + direction.normalized * 0.01f;
nextPosition = PositionToGrid(nextPosition) + directionVectors[(int)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
@@ -324,19 +327,19 @@ namespace Marro.PacManUdon
{
if (pellet.isPowerPellet)
{
gameController.GotPowerPellet(pellet);
gameManager.GotPowerPellet(pellet);
freezeSeconds = 0.05f;
}
else
{
gameController.GotPellet(pellet);
gameManager.GotPellet(pellet);
freezeSeconds = 0.0166666666666667f;
}
return;
}
else if (other.gameObject.GetComponent<BonusFruit>())
{
gameController.GotFruit();
gameManager.GotFruit();
}
}