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

@@ -18,6 +18,11 @@ namespace Marro.PacManUdon
byte[] syncedPelletsCollected;
int[] tilemap;
const int mazeWidth = 28;
const int mazeHeight = 31;
public void Initialize()
{
gameObject.SetActive(true);
@@ -32,6 +37,7 @@ namespace Marro.PacManUdon
SubscribeToEvent(NetworkEventType.SyncPellets);
}
#region Power pellet blink
public override void SyncedUpdate()
{
if (!powerPelletBlinkEnabled)
@@ -72,7 +78,39 @@ namespace Marro.PacManUdon
{
powerPelletBlinkEnabled = !frozen;
}
#endregion
#region Collision
public bool IsWallUpcoming(Vector2 position, Vector2 directionVector)
{
var result = GetTileAt(position + directionVector) == (int)PacManTileType.Wall;
Debug.Log($"Is wall upcoming {result} at position {position}, direction {directionVector}");
return result;
}
public bool[] GetBlockedDirections(Vector2 position)
{
var results = new bool[4];
results[0] = IsWallUpcoming(position, Vector2.down);
results[1] = IsWallUpcoming(position, Vector2.up);
results[2] = IsWallUpcoming(position, Vector2.left);
results[3] = IsWallUpcoming(position, Vector2.right);
return results;
}
private int GetTileAt(Vector2 position) => tilemap[GetTilemapIndex(position)];
private int GetTilemapIndex(Vector2 position)
{
position = Clamp(position, 0, mazeWidth - 1, 1 - mazeHeight, 0);
var index = (int)(position.x + 0.5) - (int)(position.y - 0.5) * mazeWidth;
return index;
}
#endregion
#region Pellet collecting
public int PelletCollected(Pellet pellet)
{
pellet.gameObject.SetActive(false);
@@ -95,6 +133,8 @@ namespace Marro.PacManUdon
syncedPelletsCollected = new byte[pellets.Length/8 + 1];
PelletCollectedCount = 0;
tilemap = PacManConstants.GetMazeDefinition();
return PelletCount;
}
@@ -131,5 +171,33 @@ namespace Marro.PacManUdon
SetPelletsCollectedFromSync();
return true;
}
#endregion
#region Utils
private static Vector2 Clamp(Vector2 vector, float xMin, float xMax, float yMin, float yMax)
{
if (vector.x < xMin)
{
vector.x = xMin;
}
if (vector.x > xMax)
{
vector.x = xMax;
}
if (vector.y < yMin)
{
vector.y = yMin;
}
if (vector.y > yMax)
{
vector.y = yMax;
}
return vector;
}
#endregion
}
}