Beispiel #1
0
        /// <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;
        }