/// <summary> /// The constructor will use the input GameState /// to initialize its fields. An exception will be /// thrown if the object passed as input is null /// </summary> /// <param name="gs">A GameState object</param> public Pacman(GameState gs) { if (Object.ReferenceEquals(null, gs)) { throw new ArgumentException("The GameState object passed to Pacman must not be null"); } this.gamestate = gs; this.maze = this.gamestate.Maze; this.ghostPack = this.gamestate.GhostPack; canMove = true; }
public Energizer(GhostPack ghosts) { this.ghosts = ghosts; }
/// <summary> /// The Parse method reads from a file and creates a GameState object. /// In the process of creation it instantiates all objects necessary to /// run the game and sets them inside the Maze. It also automatically makes /// the ScoreAndLives object subscribe to the related object events. /// </summary> /// <param name="file">The text file to read from</param> /// <returns></returns> public static GameState Parse(String content) { //string 2d array to hold the elements from the file text. string[,] map = null; Tile[,] tileArray = null; //Setting GameState Object to hold the state of the game and its properties GameState g = new GameState(); Pen pen = new Pen(); Maze maze = new Maze(); GhostPack gpack = new GhostPack(); //g.Pacman = new Pacman(GameState); g.Pen = pen; g.Maze = maze; g.GhostPack = gpack; ScoreAndLives score = new ScoreAndLives(g); Pacman pacman = new Pacman(g); Vector2 myPac = new Vector2(1, 1); pacman.Position = myPac; g.Pacman = pacman; g.Score = score; //read text from file try { string[] fileText = content.Split(new String[] { Environment.NewLine }, StringSplitOptions.None); int mapSize = fileText.Length; map = new string[mapSize, mapSize]; tileArray = new Tile[mapSize, mapSize]; //assign elements to the map string array Char delimiter = ','; for (int i = 0; i < map.GetLength(0); i++) { String[] elements = fileText[i].Split(delimiter); for (int j = 0; j < map.GetLength(0); j++) { map[i, j] = elements[j]; } } } catch (FileNotFoundException) { Console.WriteLine("Could Not Find File"); } //Iterate through the map 2d array that holds the elements from the file text. for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(0); x++) { switch (map[y, x]) { case "w": Wall wall = new Wall(x, y); Structure.Path wallPath = new Structure.Path(x, y, null); tileArray[x, y] = wall; break; case "p": Pellet pellet = new Pellet(); pellet.CollisionEvent += g.Score.IncrementScore; Structure.Path pelletMazePath = new Structure.Path(x, y, pellet); tileArray[x, y] = pelletMazePath; break; case "e": Energizer energizer = new Energizer(); energizer.CollisionEvent += g.Score.IncrementScore; Structure.Path energizerPath = new Structure.Path(x, y, energizer); tileArray[x, y] = energizerPath; break; case "m": Structure.Path emptyMazePath = new Structure.Path(x, y, null); tileArray[x, y] = emptyMazePath; break; case "x": Structure.Path emptyPath = new Structure.Path(x, y, null); tileArray[x, y] = emptyPath; pen.AddTile(emptyPath); break; case "P": Vector2 myPac2 = new Vector2(x, y); tileArray[x, y] = new Structure.Path(x, y, null); pacman.Position = myPac2; Pacman.StartPos = myPac2; break; case "1": Vector2 blinky_target = new Vector2(pacman.Position.X + 2, pacman.Position.Y); Ghost blinky = new Ghost(g, x, y, blinky_target, GhostState.Chasing, Enums.Color.Red); Ghost.ReleasePosition = blinky.Position; blinky.CollisionEvent += score.IncrementScore; blinky.PacmanDiedEvent += score.DeadPacman; gpack.Add(blinky); tileArray[x, y] = new Structure.Path(x, y, null); break; case "2": Vector2 pinky_target = new Vector2(pacman.Position.X + 4, pacman.Position.Y); Ghost pinky = new Ghost(g, x, y, pinky_target, GhostState.Chasing, Enums.Color.Pink); pinky.CollisionEvent += score.IncrementScore; pinky.PacmanDiedEvent += score.DeadPacman; gpack.Add(pinky); Structure.Path aPath = new Structure.Path(x, y, null); tileArray[x, y] = aPath; pen.AddTile(aPath); pen.AddToPen(pinky); break; case "3": Vector2 inky_target = new Vector2(pacman.Position.X + 6, pacman.Position.Y); Ghost inky = new Ghost(g, x, y, inky_target, GhostState.Chasing, Enums.Color.Yellow); inky.CollisionEvent += score.IncrementScore; inky.PacmanDiedEvent += score.DeadPacman; gpack.Add(inky); Structure.Path aPath1 = new Structure.Path(x, y, null); tileArray[x, y] = aPath1; pen.AddTile(aPath1); pen.AddToPen(inky); break; case "4": Vector2 clyde_target = new Vector2(pacman.Position.X + 1, pacman.Position.Y); Ghost clyde = new Ghost(g, x, y, clyde_target, GhostState.Chasing, Enums.Color.Green); clyde.CollisionEvent += score.IncrementScore; clyde.PacmanDiedEvent += score.DeadPacman; gpack.Add(clyde); Structure.Path aPath2 = new Structure.Path(x, y, null); tileArray[x, y] = aPath2; pen.AddTile(aPath2); pen.AddToPen(clyde); break; } } } //set the Tiles in the Maze object with the tileArray 2d array. g.Maze.SetTiles(tileArray); return(g); }