//****************************************************************//
        // This function tests if a given position is in collision with a //
        // wall, if so it will return true.                               //
        //****************************************************************//
        private bool IsInCollisionWithWall(CPoint2f pos)
        {
            /////////////////////////////////////////////////////////////////
            // First we check if we've moved outside of the board this will also
            // return true.

            if (pos.X < 0 || pos.Y < 0 || pos.X + CGameTextures.TILE_SIZE > m_boardDims[0] || pos.Y + CGameTextures.TILE_SIZE > m_boardDims[1])
            {
                return(true);
            }


            //////////////////////////////////////////////////////////////////
            // Next we calculate the centre of the tile and see if this is in
            // collision. This will allow the player to walk a little bit (50%)
            // into walls.

            CPoint2i centre = new CPoint2i();

            centre.X = (int)pos.X + CGameTextures.TILE_SIZE / 2;
            centre.Y = (int)pos.Y + CGameTextures.TILE_SIZE / 2;

            CPoint2i tilePos = CLevelUtils.GetTileCoordinatesFromPixel(centre);

            return(m_level.GetTileType(tilePos.X, tilePos.Y) == eTileType.Wall);
        }
Beispiel #2
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 draws the static parts of the level onto the    *//
        // canvas.                                                       *//
        //****************************************************************//
        private void DrawLevel()
        {
            /////////////////////////////////////////////////////////////
            // Compute the width of the canvas, this will be the number
            // of tiles multiplied by the tile size (in pixels).

            int width  = currentLevel.Width * CGameTextures.TILE_SIZE;
            int height = currentLevel.Height * CGameTextures.TILE_SIZE;

            cvsMainScreen.Width  = width;
            cvsMainScreen.Height = height;


            /////////////////////////////////////////////////////////////
            // Loop through the level setting each tiled position on the
            // canvas.

            for (int y = 0; y < currentLevel.Height; y++)
            {
                for (int x = 0; x < currentLevel.Width; x++)
                {
                    /////////////////////////////////////////////////////////
                    // We must create a new instance of the image as an image
                    // can only be added once to a given canvas.

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


                    //////////////////////////////////////////////////////////
                    // Set the position of the tile, we must convert from tile
                    // coordinates to pixel coordinates.

                    CPoint2i tilePosition = CLevelUtils.GetPixelFromTileCoordinates(new CPoint2i(x, y));


                    Canvas.SetLeft(texture, tilePosition.X);
                    Canvas.SetTop(texture, tilePosition.Y);


                    //////////////////////////////////////////////////////////
                    // Check whether it should be a wall tile or floor tile.

                    if (currentLevel.GetTileType(x, y) == eTileType.Wall)
                    {
                        texture.Source = gameTextures.WallTexture;
                    }
                    else
                    {
                        texture.Source = gameTextures.FloorTexture;
                    }

                    cvsMainScreen.Children.Add(texture);
                }
            }


            ////////////////////////////////////////////////////////////
            // The goal is also static as it does not move so we will
            // also add this now also.

            Image goalImg = new Image();

            goalImg.Width  = CGameTextures.TILE_SIZE;
            goalImg.Height = CGameTextures.TILE_SIZE;

            goalImg.Source = gameTextures.GoalIcon;

            CPoint2i GoalPosition = CLevelUtils.GetPixelFromTileCoordinates(new CPoint2i(currentLevel.GoalPosition.X, currentLevel.GoalPosition.Y));

            Canvas.SetLeft(goalImg, GoalPosition.X);
            Canvas.SetTop(goalImg, GoalPosition.Y);

            cvsMainScreen.Children.Add(goalImg);
        }