Pellet collision

This commit is contained in:
2026-06-15 14:36:07 +02:00
parent 246718f1bd
commit c601dda10a
9 changed files with 318 additions and 229 deletions

View File

@@ -3,11 +3,19 @@ using UnityEngine;
namespace Marro.PacManUdon
{
enum PelletType
{
None,
Pellet,
PowerPellet
}
public class PelletManager : SyncedObject
{
public int PelletCount => pellets.Length;
public int PelletCollectedCount { get; private set; }
private GameManager gameManager;
Pellet[] pellets;
Animator[] powerPellets;
@@ -19,12 +27,15 @@ namespace Marro.PacManUdon
byte[] syncedPelletsCollected;
int[] collisionMap;
int[] pelletMap;
const int mazeWidth = 28;
const int mazeHeight = 31;
public void Initialize()
public void Initialize(GameManager gameManager)
{
this.gameManager = gameManager;
gameObject.SetActive(true);
pellets = GetComponentsInChildren<Pellet>(includeInactive: true);
@@ -32,6 +43,8 @@ namespace Marro.PacManUdon
powerPelletBlinkToggleInterval = PacManConstants.GetPowerPelletBlinkToggleInterval();
SetPowerPelletsBlink(false);
collisionMap = PacManConstants.GetMazeCollisionInfo();
RestoreAllPellets();
SubscribeToEvent(NetworkEventType.SyncPellets);
@@ -87,6 +100,36 @@ namespace Marro.PacManUdon
return result;
}
internal PelletType CollectPelletAt(Vector2 position)
{
var tilemapIndex = GetTilemapIndex(position);
var tile = pelletMap[tilemapIndex];
if (tile < 0 || tile >= pellets.Length)
{
return PelletType.None;
}
pelletMap[tilemapIndex] = -1;
var pellet = pellets[tile];
pellet.gameObject.SetActive(false);
Debug.Log($"Collecting pellet {tile} ({pellet.name})");
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 GetAvailableDirections(Vector2 position)
{
var directions = GetTileAt(position);
@@ -110,18 +153,6 @@ namespace Marro.PacManUdon
#endregion
#region Pellet collecting
public int PelletCollected(Pellet pellet)
{
pellet.gameObject.SetActive(false);
var index = pellet.transform.GetSiblingIndex();
syncedPelletsCollected[index/8] |= (byte)(1 << index%8);
PelletCollectedCount++;
return PelletCollectedCount;
}
public int RestoreAllPellets()
{
foreach (var pellet in pellets)
@@ -132,7 +163,7 @@ namespace Marro.PacManUdon
syncedPelletsCollected = new byte[pellets.Length/8 + 1];
PelletCollectedCount = 0;
collisionMap = PacManConstants.GetMazeCollisionInfo();
pelletMap = PacManConstants.GetMazePelletMap();
return PelletCount;
}