public PacmanGhost(int number) { Facing = PacmanPacman.Direction.start; IsDead = true; IsVulnerable = false; DeadCounter = 50 * number; Location = new PacmanCoordinate(13m, 12m); }
/// <summary> /// Checks the result of executing a direction based on the position of an entity /// </summary> /// <param name="d">Direction entity is trying to move towards</param> /// <param name="pos">Position of entity currently</param> /// <returns>Whether a move is valid</returns> public bool ValidMove(PacmanPacman.Direction d, PacmanCoordinate pos) { switch (d) { case PacmanPacman.Direction.start: return(true); case PacmanPacman.Direction.up: return(GetTile(pos.XRoundPos, pos.YRoundPos - 1) != Tile.wall); case PacmanPacman.Direction.down: return(GetTile(pos.XRoundPos, pos.YRoundPos + 1) != Tile.wall); case PacmanPacman.Direction.left: return(GetTile(pos.XRoundPos - 1, pos.YRoundPos) != Tile.wall); case PacmanPacman.Direction.right: return(GetTile(pos.XRoundPos + 1, pos.YRoundPos) != Tile.wall); } 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()}")); }