//****************************************************************//
        // This function tests if a given position is in collision with a //
        // fire, if so it will return true.                               //
        //****************************************************************//
        private bool IsInCollisionWithFire(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);

            foreach (CPoint2f posFire in m_level.FirePositions)
            {
                if ((Math.Pow(tilePos.X - posFire.X, 2.0) + Math.Pow(tilePos.Y - posFire.Y, 2.0)) <= 0.1)
                {
                    return(true);
                }
            }

            return(false);
        }
        //****************************************************************//
        // 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);
        }
        //***********************************************************************//
        // This function can be used to compute a pixel position from a given    //
        // tile position.                                                        //
        //***********************************************************************//
        public static CPoint2i GetPixelFromTileCoordinates(CPoint2i TileCoordinate)
        {
            CPoint2i ret = new CPoint2i();

            ret.X = TileCoordinate.X * CGameTextures.TILE_SIZE;
            ret.Y = TileCoordinate.Y * CGameTextures.TILE_SIZE;
            return(ret);
        }
        //***********************************************************************//
        // This function can be used to compute a tile position from a given     //
        // pixel position.                                                       //
        //***********************************************************************//
        public static CPoint2i GetTileCoordinatesFromPixel(CPoint2i PixelPosition)
        {
            CPoint2i ret = new CPoint2i();

            ret.X = PixelPosition.X / CGameTextures.TILE_SIZE;
            ret.Y = PixelPosition.Y / CGameTextures.TILE_SIZE;
            return(ret);
        }
        private void cvsMainScreen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point    p        = e.GetPosition(cvsMainScreen);
            CPoint2i pixelPos = new CPoint2i((int)p.X, (int)p.Y);
            CPoint2i mapPos   = CLevelUtils.GetTileCoordinatesFromPixel(pixelPos);

            if (FloorWallToggleRadio.IsChecked == true)
            {
                if (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Wall)
                {
                    currentLevel.SetTileType(mapPos.X, mapPos.Y, eTileType.Floor);
                }
                else if (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor)
                {
                    currentLevel.SetTileType(mapPos.X, mapPos.Y, eTileType.Wall);
                }
            }
            else if (PlayerRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor))
            {
                currentLevel.StartPosition = mapPos;
            }
            else if (EnemyRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor))
            {
                //Fill In here
            }
            else if (GoalRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor))
            {
                currentLevel.GoalPosition = mapPos;
            }
            else if (WallRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor))
            {
                currentLevel.SetTileType(mapPos.X, mapPos.Y, eTileType.Wall);
            }
            else if (FloorRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Wall))
            {
                currentLevel.SetTileType(mapPos.X, mapPos.Y, eTileType.Floor);
            }
            else if (FireRadio.IsChecked == true && (currentLevel.GetTileType(mapPos.X, mapPos.Y) == eTileType.Floor))
            {
                //Fill In here
            }
            reRender();
        }
        //****************************************************************//
        // 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();
        }
        //****************************************************************//
        // 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);
        }