/// <summary> /// Checks whether a ghost has collided with pacman and handles resetting positions after death /// </summary> private bool PacmanGhostCollide() { for (int i = 0; i < Ghosts.Length; i++) { if (Ghosts[i].GetPosition().Collide(Pacman.GetPosition())) { if (Ghosts[i].IsVulnerable) { Ghosts[i].IsDead = true; Ghosts[i].IsVulnerable = false; Ghosts[i].StartDeathCounter(); Ghosts[i].Location.Xpos = 13; Ghosts[i].Location.Ypos = 12; Score += 200 * GhostScoreMultiplier; CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.GhostDie, i + $" 1 {200 * GhostScoreMultiplier}")); } else if (!Ghosts[i].IsDead) { Pacman.Location.Xpos = 13; Pacman.Location.Ypos = 17; Pacman.Facing = PacmanPacman.Direction.right; Pacman.Lives--; CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.PacmanLives, $"{Pacman.Lives}")); Ghosts = new PacmanGhost[4] { new PacmanGhost(1), new PacmanGhost(2), new PacmanGhost(3), new PacmanGhost(4) }; if (Pacman.Lives <= 0) { Pacman.Lives = 0; GameRunning = false; TimeEnded = DateTime.Now; return(true); } } } } return(false); }
/// <summary>Processes every tick of the game base on directions of each entity passed in the array /// p should be passed 5 directions /// p[0] represents Pacman /// p[1] through p[4] represent ghosts in order passed </summary> public void UpdateGame(PacmanPacman.Direction p) { SpawnGhost(); if (FruitSpawnCounter >= 450) { CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.FruitSpawn, Board.SpawnFruit().ToString())); FruitSpawnCounter = -1; } FruitSpawnCounter++; // Move ghosts for (int i = 0; i < Ghosts.Length; i++) { if (Ghosts[i].IsDead) { continue; } ProcessGhostMove(i); } PacmanGhostCollide(); // Move Pacman if (Pacman.Location.Xpos % 1 != 0) { if (p == PacmanPacman.Direction.left || p == PacmanPacman.Direction.right) { Pacman.Facing = p; } Pacman.Move(); CheckTile(Pacman.GetPosition()); } else if (Pacman.Location.Ypos % 1 != 0) { if (p == PacmanPacman.Direction.up || p == PacmanPacman.Direction.down) { Pacman.Facing = p; } Pacman.Move(); CheckTile(Pacman.GetPosition()); } else if (Pacman.Location.Xpos % 1 == 0 && Pacman.Location.Ypos % 1 == 0) { if (Board.ValidMove(p, Pacman.GetPosition())) { Pacman.Facing = p; } if (Board.ValidMove(Pacman.Facing, Pacman.GetPosition())) { Pacman.Move(); CheckTile(Pacman.GetPosition()); } } PacmanGhostCollide(); if (PoweredUpCounter == 0) { foreach (var g in Ghosts) { g.IsVulnerable = false; } GhostScoreMultiplier = 1; } if (PoweredUpCounter > -1) { PoweredUpCounter--; } for (var i = 0; i < Ghosts.Length; i++) { CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.GhostUpdate, $"{i} {Ghosts[i].GetPosition().ToString()} {Ghosts[i].IsDead} {Ghosts[i].IsVulnerable}")); } CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.PacmanUpdate, $"{Pacman.GetPosition().ToString()}")); }