public void LoadLevel(Level level) { State = GameState.Initialising; Level = level; Ghosts.Clear(); foreach (var ghost in level.Ghosts) { Ghosts.Add(new Ghost(ghost.X, ghost.Y, ghost.Logic)); } ResetCharacters(); FillPellets(); HasWon = false; }
/// <summary> /// Initialize the instance. This method MUST BE called before using this class. /// </summary> /// <param name="maze">The current maze</param> /// <param name="pac">The pac the user is playing</param> /// <param name="leader">The leader of the ghost (often, it is Blinky, the red ghost)</param> /// <param name="others">Array containing the others ghosts (all ghosts except the leader)</param> public void Initialize(Maze maze, Pac pac, Ghost leader, Ghost[] others) { PlayBeginning = -1; if (Ghosts == null) { Ghosts = new List <Ghost>(4); } else { Ghosts.Clear(); } Map = maze; Pac = pac; Ghosts.Add(leader); Ghosts.AddRange(others); // Search startup points for every ghosts List <Cell> list = Map.SearchTile(TileType.BLINKY_STARTUP); if (list != null && list.Count > 0) { Vector3 result = list[0].Dimension.Min; Ghosts[0].StartingPoint = Entrance = new Vector2(result.X, result.Y); // Now, the program must find the start area for the other ghosts list = Map.SearchTile(TileType.GHOSTS_STARTUP); if (list != null && list.Count >= (Ghosts.Count - 1)) { for (int i = 1, maxi = Ghosts.Count; i < maxi; i++) { // 1 <= i <= Ghosts.Count, but Ghosts.Count-1 <= list.Count. Therefore, list[i-1] will not throw an ArrayOutOfBoundException result = list[i - 1].Dimension.Min; Ghosts[i].StartingPoint = new Vector2(result.X, result.Y); } } else { throw new InvalidOperationException("No start point for the ghosts in the maze"); } } else { throw new InvalidOperationException("No start point for Blinky in the maze"); } IsInitialized = true; }
public static void InitializeMap() { Walls.Clear(); Nodes.Clear(); EmptySquares.Clear(); Dots.Clear(); Powerups.Clear(); Ghosts.Clear(); Invaders.Clear(); //Initializes border of map string line = ""; int number = 0; using (StreamReader sr = new StreamReader("Maps/pacman" + PacmanGame.maps[PacmanGame.mapNumber].ToString() + ".txt")) { while (true) { line = sr.ReadLine(); if (line == null) { break; } for (int counter = 0; counter < line.Length; counter++) { wallMap[counter, number] = line[counter].ToString() == "0"; } number++; } } //Fills in rest of map using two dimensional bool array wallMap for (int x = 0; x < wallMap.GetLength(0); x++) { for (int y = 0; y < wallMap.GetLength(1); y++) { if (!wallMap[x, y]) { Walls.Add(new Wall(new Rectangle(x * PacmanGame.gridSize + PacmanGame.horizontalSpace, y * PacmanGame.gridSize + PacmanGame.verticalSpace, PacmanGame.gridSize, PacmanGame.gridSize))); } } } //Fills map with empty squares, dots and nodes in all places without walls //variable used to count nodes int variable = 0; for (int y = 1; y < PacmanGame.mapHeight / PacmanGame.gridSize - 1; y++) { for (int x = 1; x < PacmanGame.mapWidth / PacmanGame.gridSize - 1; x++) { Rectangle rectangle = new Rectangle(PacmanGame.gridSize * x + PacmanGame.horizontalSpace, PacmanGame.gridSize * y + PacmanGame.verticalSpace, PacmanGame.gridSize, PacmanGame.gridSize); bool value = true; foreach (Wall wall in Walls) { if (wall.Position == rectangle) { value = false; } } if (value) { EmptySquares.Add(new EmptySquare(rectangle)); Dots.Add(new Dot(rectangle)); Nodes.Add(new Node(variable, rectangle)); variable++; } } } //Remove the dot where the pacman starts Dots.Remove(new Dot(new Rectangle(PacmanGame.gridSize, PacmanGame.gridSize, PacmanGame.gridSize, PacmanGame.gridSize))); //Adds powerups Random random = new Random(); int[] values = Enumerable.Range(0, Map.Nodes.Count).OrderBy(x => random.Next()).ToArray(); for (int counter = 0; counter < 4; counter++) { int index = values[counter]; Rectangle rectangleValue = Map.Nodes.ElementAt(index).Position; Powerups.Add(new Powerup(rectangleValue)); //Remove the dot where the powerup is Dots.Remove(new Dot(rectangleValue)); } createAdjacencyList(); Paddles[0] = new Paddle(new Rectangle(PacmanGame.horizontalSpace / 2, PacmanGame.screenHeight / 2, PacmanGame.gridSize, PacmanGame.gridSize * 5), Player.Left); Paddles[1] = new Paddle(new Rectangle(PacmanGame.screenWidth - PacmanGame.horizontalSpace / 2, PacmanGame.screenHeight / 2, PacmanGame.gridSize, PacmanGame.gridSize * 5), Player.Right); }