Ghost - PacMan collision detection

This commit is contained in:
2026-06-17 14:12:36 +02:00
parent e3b626f3d6
commit a7d1adf175
6 changed files with 302 additions and 127 deletions

View File

@@ -3,12 +3,13 @@ using UnityEngine;
namespace Marro.PacManUdon
{
enum PelletType
enum EatResult
{
None,
Pellet,
PowerPellet
}
public class PelletManager : SyncedObject
{
public int PelletCount => pellets.Length;
@@ -19,6 +20,7 @@ namespace Marro.PacManUdon
Pellet[] pellets;
Animator[] powerPellets;
Ghost[] ghosts;
bool powerPelletBlinkEnabled;
float powerPelletBlinkToggleInterval;
@@ -33,10 +35,14 @@ namespace Marro.PacManUdon
const int mazeWidth = 32;
const int mazeHeight = 32;
public void Initialize(GameManager gameManager, BonusFruit bonusFruit)
private int[] ghostPositions = new int[4];
private int pacManPosition;
public void Initialize(GameManager gameManager, BonusFruit bonusFruit, Ghost[] ghosts)
{
this.gameManager = gameManager;
this.bonusFruit = bonusFruit;
this.ghosts = ghosts;
gameObject.SetActive(true);
pellets = GetComponentsInChildren<Pellet>(includeInactive: true);
@@ -101,29 +107,50 @@ namespace Marro.PacManUdon
var index = GetTilemapIndex(position + directionVector);
var tile = collisionMap[index];
var result = (tile & (int)PacManCollisionInfoType.Wall) != 0;
Debug.Log($"IsWallUpcoming {position}, {directionVector}. index {index}, tile {tile}, result {result}, collisionMap.Length {collisionMap.Length}");
//Debug.Log($"IsWallUpcoming {position}, {directionVector}. index {index}, tile {tile}, result {result}, collisionMap.Length {collisionMap.Length}");
return result;
}
public bool IsInTunnel(Vector2 position)
public bool GhostMoveToTile(Vector2 position, int ghostIndex)
{
var result = (collisionMap[GetTilemapIndex(position)] & (int)PacManCollisionInfoType.Tunnel) != 0;
if (result)
var tile = GetTilemapIndex(position);
ghostPositions[ghostIndex] = tile;
if (tile == pacManPosition)
{
Debug.Log($"In tunnel at {position}");
Debug.Log("Ghost hit PacMan!");
ghosts[ghostIndex].HitPacMan();
}
return result;
return (collisionMap[tile] & (int)PacManCollisionInfoType.Tunnel) != 0;
}
internal PelletType EatAtTile(Vector2 position, Vector2 nextPosition)
internal EatResult PacManMoveToTile(Vector2 position, Vector2 nextPosition)
{
var tilemapIndex = GetTilemapIndex(nextPosition);
var tile = pelletMap[tilemapIndex];
pacManPosition = tilemapIndex;
TryEatGhost(tilemapIndex);
TryCollectFruit(tile, position, nextPosition);
return TryCollectPellet(tile, tilemapIndex);
}
private void TryEatGhost(int tilemapIndex)
{
for (int i = 0; i < ghosts.Length; i++)
{
if (ghostPositions[i] == tilemapIndex)
{
Debug.Log("PacMan hit ghost!");
ghosts[i].HitPacMan();
}
}
}
private void TryCollectFruit(int tile, Vector2 position, Vector2 nextPosition)
{
if (tile != (int)PacManConsumableType.FruitLeft && tile != (int)PacManConsumableType.FruitRight
@@ -134,12 +161,12 @@ namespace Marro.PacManUdon
var previousTile = pelletMap[GetTilemapIndex(position)];
Debug.Log($"On fruit tile {tile}, previous tile was {previousTile}. Position {position}, nextPosition {nextPosition}");
//Debug.Log($"On fruit tile {tile}, previous tile was {previousTile}. Position {position}, nextPosition {nextPosition}");
if (tile == (int)PacManConsumableType.FruitLeft && previousTile == (int)PacManConsumableType.FruitRight
|| tile == (int)PacManConsumableType.FruitRight && previousTile == (int)PacManConsumableType.FruitLeft)
{
Debug.Log("Collecting fruit");
//Debug.Log("Collecting fruit");
gameManager.GotFruit();
}
@@ -163,11 +190,11 @@ namespace Marro.PacManUdon
#endregion
#region Pellet collecting
private PelletType TryCollectPellet(int tile, int tilemapIndex)
private EatResult TryCollectPellet(int tile, int tilemapIndex)
{
if (tile < 0 || tile >= pellets.Length)
{
return PelletType.None;
return EatResult.None;
}
pelletMap[tilemapIndex] = (byte)PacManConsumableType.None;
@@ -180,9 +207,9 @@ namespace Marro.PacManUdon
PelletCollectedCount++;
var pelletType = pellet.isPowerPellet ? PelletType.PowerPellet : PelletType.Pellet;
var pelletType = pellet.isPowerPellet ? EatResult.PowerPellet : EatResult.Pellet;
gameManager.GotPellet(pellet, pelletType, PelletCollectedCount, PelletCount - PelletCollectedCount);
gameManager.GotPellet(pellet, pellet.isPowerPellet, PelletCollectedCount, PelletCount - PelletCollectedCount);
return pelletType;
}