//****************************************************************//
        // This constructors constructs an instance of the game engine. It//
        // requires to know the level and the size of each tile.          //
        //****************************************************************//
        public CGameEngine(CLevel level)
        {
            m_level = level;


            //////////////////////////////////////////////////
            // The board dimensions are in units of pixels.

            m_boardDims[0] = m_level.Width * CGameTextures.TILE_SIZE;
            m_boardDims[1] = m_level.Height * CGameTextures.TILE_SIZE;
        }
Beispiel #2
0
        /// <summary>
        /// This function imports the level from the given directory. It is assumed
        /// the text file containing the level is called Level.txt.
        /// </summary>
        /// <param name="directory">The directory the level is stored in.</param>
        /// <returns>The loaded level.</returns>
        public static CLevel ImportLevel(string directory)
        {
            //////////////////////////////////////////////////////
            // Create an instance of a level to populate.

            CLevel ret = new CLevel();

            using (StreamReader fs = new StreamReader(directory + "\\Level.txt"))
            {
                ///////////////////////////////////////////////////////////
                // Read in the dimensions of the level.

                string strDimY = Regex.Match(fs.ReadLine(), @"\d+").Value;
                int    DimY    = int.Parse(strDimY);

                string strDimX = Regex.Match(fs.ReadLine(), @"\d+").Value;
                int    DimX    = int.Parse(strDimX);


                ///////////////////////////////////////////////////////////
                // Size the level.

                ret.Resize(DimX, DimY);


                ///////////////////////////////////////////////////////////
                // Read in each tile type.

                for (int y = 0; y < DimY; y++)
                {
                    string strRow = fs.ReadLine();

                    for (int x = 0; x < DimX; x++)
                    {
                        if (strRow[x] == 'F')
                        {
                            ret.SetTileType(x, y, eTileType.Floor);
                        }
                        else
                        {
                            ret.SetTileType(x, y, eTileType.Wall);
                        }
                    }
                }


                /////////////////////////////////////////////////////////////
                // Get the player start position.

                string   strIn = fs.ReadLine();
                string[] bits  = strIn.Split(',');
                ret.StartPosition = new CPoint2i(int.Parse(Regex.Match(bits[0], @"\d+").Value),
                                                 int.Parse(Regex.Match(bits[1], @"\d+").Value));


                //////////////////////////////////////////////////////////////
                // Get the goal position.

                strIn            = fs.ReadLine();
                bits             = strIn.Split(',');
                ret.GoalPosition = new CPoint2i(int.Parse(Regex.Match(bits[0], @"\d+").Value),
                                                int.Parse(Regex.Match(bits[1], @"\d+").Value));


                //////////////////////////////////////////////////////////////
                // Get the time available.

                ret.Time = int.Parse(Regex.Match(fs.ReadLine(), @"\d+").Value);


                //////////////////////////////////////////////////////////////
                // Get the number of enemies in the level.

                int numEnemies = int.Parse(Regex.Match(fs.ReadLine(), @"\d+").Value);

                ret.EnemyPositions.Capacity = numEnemies;


                ///////////////////////////////////////////////////////////////
                // Read in each enemy from the level.

                for (int i = 0; i < numEnemies; i++)
                {
                    strIn = fs.ReadLine();
                    bits  = strIn.Split(',');
                    ret.EnemyPositions.Add(new CPoint2i(int.Parse(Regex.Match(bits[0], @"\d+").Value),
                                                        int.Parse(Regex.Match(bits[1], @"\d+").Value)));
                }


                //////////////////////////////////////////////////////////////
                // Read in the fire locations.

                int numFire = int.Parse(Regex.Match(fs.ReadLine(), @"\d+").Value);

                ret.FirePositions.Capacity = numFire;

                for (int i = 0; i < numFire; i++)
                {
                    strIn = fs.ReadLine();
                    bits  = strIn.Split(',');
                    ret.FirePositions.Add(new CPoint2i(int.Parse(Regex.Match(bits[0], @"\d+").Value),
                                                       int.Parse(Regex.Match(bits[1], @"\d+").Value)));
                }
            }


            /////////////////////////////////////////////////////////
            // return the level.

            return(ret);
        }
Beispiel #3
0
        /// <summary>
        /// This function exports the given level to the given directory.
        /// If there is already a level at the given directory it will be
        /// overwritten.
        /// </summary>
        /// <param name="levelToExport">The level to export.</param>
        /// <param name="directory">The directory to save the level.</param>
        public static void ExportLevel(CLevel levelToExport, string directory)
        {
            /////////////////////////////////////////////////////
            // Save out level:

            Directory.CreateDirectory(directory);

            using (StreamWriter fs = new StreamWriter(directory + "\\Level.txt"))
            {
                //////////////////////////////////////////////////////
                // Write the number of tiles in the height and width.

                fs.WriteLine("Height=" + levelToExport.Height.ToString());
                fs.WriteLine("Width=" + levelToExport.Width.ToString());


                //////////////////////////////////////////////////////
                // Write out the layout of the tiles.

                for (int y = 0; y < levelToExport.Height; y++)
                {
                    for (int x = 0; x < levelToExport.Width; x++)
                    {
                        if (levelToExport.GetTileType(x, y) == eTileType.Floor)
                        {
                            fs.Write('F');
                        }
                        else
                        {
                            fs.Write('W');
                        }
                    }
                    fs.WriteLine();
                }


                //////////////////////////////////////////////////////
                // Save the start position of the player and the goal
                // position.

                fs.WriteLine("StartPosition=" + levelToExport.StartPosition.ToString());
                fs.WriteLine("GoalPosition=" + levelToExport.GoalPosition.ToString());


                fs.WriteLine("TimeToComplete=" + levelToExport.Time.ToString());

                //////////////////////////////////////////////////////
                // Write out the number of enemies and the position of
                // each.

                fs.WriteLine("NumEnemies=" + levelToExport.EnemyPositions.Count.ToString());

                for (int i = 0; i < levelToExport.EnemyPositions.Count(); i++)
                {
                    fs.WriteLine(levelToExport.EnemyPositions[i].ToString());
                }


                fs.WriteLine("NumFire=" + levelToExport.FirePositions.Count.ToString());

                for (int i = 0; i < levelToExport.FirePositions.Count(); i++)
                {
                    fs.WriteLine(levelToExport.FirePositions[i].ToString());
                }
            }
        }
        //****************************************************************//
        // This function is used as an event handler for the load click  *//
        // event. This is used to load a new level. In addition to       *//
        // the data required for the level it also ensures the board is  *//
        // displayed.                                                    *//
        //****************************************************************//
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            ////////////////////////////////////////////////////////////
            // clear any existing children from the canvas.

            cvsMainScreen.Children.Clear();

            ///////////////////////////////////////////////////////////
            // Get the directory where the level data is stored and
            // load the data in.

            string fileDir = txtLevelDir.Text;

            currentLevel = CLevelParser.ImportLevel(fileDir);
            gameTextures = CLevelParser.ImportTextures(fileDir);


            ///////////////////////////////////////////////////////////
            // Draw the set of wall and floor tiles for the current
            // level and the goal icon. This is part of the game
            // we do not expect to change as it cannot move.

            DrawLevel();


            //////////////////////////////////////////////////////////
            // Add a game state, this represents the position and velocity
            // of all the enemies and the player. Basically, anything
            // that is dynamic that we expect to move around.

            gameState = new CGameState(currentLevel.EnemyPositions.Count());


            ///////////////////////////////////////////////////////////
            // Set up the player to have the correct .bmp and set it to
            // its initial starting point. The player's position is stored
            // as a tile index on the Clevel class, this must be converted
            // to a pixel position on the game state.

            playerIcon        = new Image();
            playerIcon.Width  = CGameTextures.TILE_SIZE;
            playerIcon.Height = CGameTextures.TILE_SIZE;

            playerIcon.Source = gameTextures.PlayerIcon;

            cvsMainScreen.Children.Add(playerIcon);


            //////////////////////////////////////////////////////////
            // Create instances of the enemies and fires for display. We must do
            // this as each child on a canvas must be a distinct object,
            // we could not simply add the same image multiple times.

            enemyIcons = new Image[currentLevel.EnemyPositions.Count()];

            for (int i = 0; i < currentLevel.EnemyPositions.Count(); i++)
            {
                enemyIcons[i] = new Image();

                enemyIcons[i].Width  = CGameTextures.TILE_SIZE;
                enemyIcons[i].Height = CGameTextures.TILE_SIZE;

                enemyIcons[i].Source = gameTextures.EnemyIcon;

                cvsMainScreen.Children.Add(enemyIcons[i]);
            }


            fireIcons = new Image[currentLevel.FirePositions.Count()];

            for (int i = 0; i < currentLevel.FirePositions.Count(); i++)
            {
                fireIcons[i] = new Image();

                fireIcons[i].Width  = CGameTextures.TILE_SIZE;
                fireIcons[i].Height = CGameTextures.TILE_SIZE;

                fireIcons[i].Source = gameTextures.FireIcon;

                cvsMainScreen.Children.Add(fireIcons[i]);

                CPoint2i tilePosition = CLevelUtils.GetPixelFromTileCoordinates(new CPoint2i(currentLevel.FirePositions[i].X, currentLevel.FirePositions[i].Y));


                Canvas.SetLeft(fireIcons[i], tilePosition.X);
                Canvas.SetTop(fireIcons[i], tilePosition.Y);
            }

            loadTextures();
            ////////////////////////////////////////////////////////////
            // Set each instance of a dynamic object to its initial position
            // as defined by the current level object.

            InitialiseGameState();


            ////////////////////////////////////////////////////////////
            // Render the current game state, this will render the player
            // and the enemies in their initial position.

            RenderGameState();
        }