Bunch of cleaning
This commit is contained in:
@@ -39,6 +39,7 @@ namespace Marro.PacManUdon
|
||||
{
|
||||
[SerializeField] private PacManGhostType ghostType;
|
||||
[SerializeField] private PacManGhostStartState startState;
|
||||
[SerializeField] private GameObject targetIndicator;
|
||||
|
||||
// External references
|
||||
private GhostManager ghostManager;
|
||||
@@ -116,6 +117,8 @@ namespace Marro.PacManUdon
|
||||
Index = index;
|
||||
|
||||
SubscribeToEvent(NetworkEventType.GhostUpdate);
|
||||
|
||||
targetIndicator.transform.parent = transform.parent;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
@@ -280,30 +283,50 @@ namespace Marro.PacManUdon
|
||||
var upcomingGridPosition = PositionToGrid(position + directionVectors[(int)direction]);
|
||||
var availableDirections = collisionManager.GetAvailableDirections(upcomingGridPosition);
|
||||
|
||||
if ((availableDirections & (int)PacManCollisionInfoType.NoTurn) != 0 )
|
||||
if ((availableDirections & (int)PacManCollisionInfoType.NoTurn) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
availableDirections &= ~(int)GetInverseDirection(direction); // Not allowed to turn around
|
||||
|
||||
if (!isScared && (availableDirections & (int)PacManCollisionInfoType.HorizontalOnly) != 0)
|
||||
{
|
||||
availableDirections &= ~0b0011;
|
||||
availableDirections &= 0b1100;
|
||||
}
|
||||
|
||||
availableDirections &= ~(int)GetInverseDirection(direction) & 0b1111; // Not allowed to turn around, also filter flags other than direction
|
||||
|
||||
if (availableDirections == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Direction newDirection;
|
||||
if (isSingleBitSet[availableDirections])
|
||||
{
|
||||
newDirection = (Direction)availableDirections;
|
||||
}
|
||||
else
|
||||
{
|
||||
target = GetGridTarget(upcomingGridPosition);
|
||||
targetIndicator.transform.localPosition = target;
|
||||
newDirection = GetGridDirectionToTargetGreedy(availableDirections, upcomingGridPosition, 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}");
|
||||
|
||||
}
|
||||
|
||||
// Static fields are not yet supported on user-defined types
|
||||
private readonly bool[] isSingleBitSet = new bool[]
|
||||
{
|
||||
//0000,0001, 0010, 0011 , 0100, 0101 , 0110 , 0111 , 1000, 1001 , 1010 , 1011 , 1100 , 1101 , 1110 , 1111
|
||||
false, true, true, false, true, false, false, false, true, false, false, false, false, false, false, false
|
||||
};
|
||||
|
||||
private void ApplyTargetDirection(Vector2 position, out Vector2 nextPosition)
|
||||
{
|
||||
var gridPosition = PositionToGrid(position);
|
||||
@@ -327,7 +350,7 @@ namespace Marro.PacManUdon
|
||||
if (nextValidDirectionIndex == predefinedPathIndex)
|
||||
{
|
||||
SetDirectionAndTargetDirection(predefinedPath[nextValidDirectionIndex]);
|
||||
nextPosition = PositionToGrid(nextPosition) + GetVector(direction) * 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
|
||||
@@ -348,57 +371,56 @@ namespace Marro.PacManUdon
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 GetGridTarget(Vector2 gridPosition)
|
||||
private Vector2 GetGridTarget(Vector2 gridPosition)
|
||||
{
|
||||
// if (followingPredefinedPath)
|
||||
// {
|
||||
// predefinedPathIndex++;
|
||||
// if (predefinedPathIndex >= predefinedPath.Length)
|
||||
// {
|
||||
// followingPredefinedPath = false;
|
||||
// }
|
||||
// return gridPosition + predefinedPath[predefinedPathIndex];
|
||||
// }
|
||||
if (isScared)
|
||||
{
|
||||
return gridPosition + directionVectors[(int)cardinalDirections[PseudoRNG() % 4]];
|
||||
}
|
||||
|
||||
switch (ghostState)
|
||||
if (ghostState == PacManGhostState.Returning)
|
||||
{
|
||||
return homePosition;
|
||||
}
|
||||
|
||||
if (scattering)
|
||||
{
|
||||
return cornerPosition;
|
||||
}
|
||||
|
||||
switch (ghostType)
|
||||
{
|
||||
default:
|
||||
return gridPosition;
|
||||
case PacManGhostState.Normal:
|
||||
if (scattering)
|
||||
case PacManGhostType.Blinky: // Chase PacMan directly
|
||||
return PositionToGrid(pacMan.transform.localPosition);
|
||||
case PacManGhostType.Pinky: // Try to get ahead of PacMan
|
||||
return GetTargetAheadOfPacMan(4);
|
||||
case PacManGhostType.Inky: // Try to attack from the opposite side of Blinky
|
||||
var blinkyPosition = PositionToGrid(blinky.transform.localPosition);
|
||||
return ((GetTargetAheadOfPacMan(2) - blinkyPosition) * 2) + blinkyPosition;
|
||||
case PacManGhostType.Clyde: // Chase PacMan, but retreat to corner if PacMan gets to close
|
||||
if (Vector2.Distance(gridPosition, PositionToGrid(pacMan.transform.localPosition)) < 8)
|
||||
{
|
||||
return cornerPosition;
|
||||
}
|
||||
switch (ghostType)
|
||||
{
|
||||
default:
|
||||
return gridPosition;
|
||||
case PacManGhostType.Blinky:
|
||||
return PositionToGrid(pacMan.transform.localPosition);
|
||||
case PacManGhostType.Pinky:
|
||||
return PositionToGrid(pacMan.transform.localPosition) + pacMan.GetVector(direction) * 4;
|
||||
case PacManGhostType.Inky:
|
||||
return 2 * PositionToGrid(pacMan.transform.localPosition) + 4 * pacMan.GetVector(direction) - PositionToGrid(blinky.transform.localPosition);
|
||||
case PacManGhostType.Clyde:
|
||||
if (Vector2.Distance(gridPosition, PositionToGrid(pacMan.transform.localPosition)) >= 8)
|
||||
{
|
||||
return PositionToGrid(pacMan.transform.localPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Debug.Log($"{gameObject} goes to cornerPosition {cornerPosition}");
|
||||
return cornerPosition;
|
||||
}
|
||||
}
|
||||
case PacManGhostState.Returning:
|
||||
return homePosition;
|
||||
return PositionToGrid(pacMan.transform.localPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetTargetAheadOfPacMan(int tilesInFront)
|
||||
{
|
||||
var direction = pacMan.GetTargetDirection();
|
||||
var result = PositionToGrid(pacMan.transform.localPosition) + directionVectors[(int)direction] * tilesInFront;
|
||||
|
||||
if (direction == Direction.Up) // Reproducing a bug in the original game
|
||||
{
|
||||
result.x -= tilesInFront;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CheckAndAllignToTargetX(Vector2 currentPosition, Vector2 nextPosition, Vector2 target)
|
||||
{
|
||||
return (currentPosition.x - target.x) * (nextPosition.x - target.x) <= 0.01;
|
||||
@@ -469,13 +491,12 @@ namespace Marro.PacManUdon
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
foreach (var direction in horizontalOnly ? horizontalDirections : cardinalDirections)
|
||||
foreach (var direction in cardinalDirections)
|
||||
{
|
||||
if (((int)direction & availableDirections) == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user