Bonus fruit hitbox

This commit is contained in:
2026-06-15 17:21:16 +02:00
parent c601dda10a
commit a24f237bd5
11 changed files with 439 additions and 294 deletions

View File

@@ -15,6 +15,7 @@ namespace Marro.PacManUdon
public int PelletCollectedCount { get; private set; }
private GameManager gameManager;
private BonusFruit bonusFruit;
Pellet[] pellets;
Animator[] powerPellets;
@@ -32,9 +33,10 @@ namespace Marro.PacManUdon
const int mazeWidth = 28;
const int mazeHeight = 31;
public void Initialize(GameManager gameManager)
public void Initialize(GameManager gameManager, BonusFruit bonusFruit)
{
this.gameManager = gameManager;
this.bonusFruit = bonusFruit;
gameObject.SetActive(true);
pellets = GetComponentsInChildren<Pellet>(includeInactive: true);
@@ -96,38 +98,38 @@ namespace Marro.PacManUdon
#region Collision
public bool IsWallUpcoming(Vector2 position, Vector2 directionVector)
{
var result = GetTileAt(position + directionVector) == (int)PacManTileType.Wall;
var result = GetTileAt(position + directionVector) == (int)PacManCollisionType.Wall;
return result;
}
internal PelletType CollectPelletAt(Vector2 position)
internal PelletType EatAtTile(Vector2 position, Vector2 nextPosition)
{
var tilemapIndex = GetTilemapIndex(position);
var tilemapIndex = GetTilemapIndex(nextPosition);
var tile = pelletMap[tilemapIndex];
if (tile < 0 || tile >= pellets.Length)
TryCollectFruit(tile, position, nextPosition);
return TryCollectPellet(tile, tilemapIndex);
}
private void TryCollectFruit(int tile, Vector2 position, Vector2 nextPosition)
{
if (tile != (int)PacManConsumableType.FruitLeft && tile != (int)PacManConsumableType.FruitRight
|| !bonusFruit.Active)
{
return PelletType.None;
return;
}
pelletMap[tilemapIndex] = -1;
var previousTile = pelletMap[GetTilemapIndex(position)];
var pellet = pellets[tile];
pellet.gameObject.SetActive(false);
Debug.Log($"On fruit tile {tile}, previous tile was {previousTile}. Position {position}, nextPosition {nextPosition}");
Debug.Log($"Collecting pellet {tile} ({pellet.name})");
if (tile == (int)PacManConsumableType.FruitLeft && previousTile == (int)PacManConsumableType.FruitRight
|| tile == (int)PacManConsumableType.FruitRight && previousTile == (int)PacManConsumableType.FruitLeft)
{
Debug.Log("Collecting fruit");
var index = pellet.transform.GetSiblingIndex();
syncedPelletsCollected[index / 8] |= (byte)(1 << index % 8);
PelletCollectedCount++;
var pelletType = pellet.isPowerPellet ? PelletType.PowerPellet : PelletType.Pellet;
gameManager.GotPellet(pellet, pelletType, PelletCollectedCount, PelletCount - PelletCollectedCount);
return pelletType;
gameManager.GotFruit();
}
}
public int GetAvailableDirections(Vector2 position)
@@ -144,7 +146,7 @@ namespace Marro.PacManUdon
private int GetTileAt(Vector2 position) => collisionMap[GetTilemapIndex(position)];
private int GetTilemapIndex(Vector2 position)
internal static 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;
@@ -153,6 +155,30 @@ namespace Marro.PacManUdon
#endregion
#region Pellet collecting
private PelletType TryCollectPellet(int tile, int tilemapIndex)
{
if (tile < 0 || tile >= pellets.Length)
{
return PelletType.None;
}
pelletMap[tilemapIndex] = -1;
var pellet = pellets[tile];
pellet.gameObject.SetActive(false);
var index = pellet.transform.GetSiblingIndex();
syncedPelletsCollected[index / 8] |= (byte)(1 << index % 8);
PelletCollectedCount++;
var pelletType = pellet.isPowerPellet ? PelletType.PowerPellet : PelletType.Pellet;
gameManager.GotPellet(pellet, pelletType, PelletCollectedCount, PelletCount - PelletCollectedCount);
return pelletType;
}
public int RestoreAllPellets()
{
foreach (var pellet in pellets)