//****************************************************************//
        // This initilaises the dynamic parts of the game to their initial//
        // positions as specified by the game level.                      //
        //****************************************************************//
        private void InitialiseGameState()
        {
            ////////////////////////////////////////////////////////////
            // Place the player at their initial position.

            gameState.Player.Position = CLevelUtils.GetPixelFromTileCoordinates(currentLevel.StartPosition);

            Random random = new Random();

            for (int i = 0; i < currentLevel.EnemyPositions.Count(); i++)
            {
                ////////////////////////////////////////////////////////////
                // Place each enemy at their initial position and give them an
                // initial random direction.

                gameState.Enemies[i].Position = CLevelUtils.GetPixelFromTileCoordinates(currentLevel.EnemyPositions[i]);

                gameState.Enemies[i].TargetPosition.X = gameState.Enemies[i].Position.X;
                gameState.Enemies[i].TargetPosition.Y = gameState.Enemies[i].Position.Y;


                /////////////////////////////////////////////////////////////
                // Create a random direction to walk in.

                int direction = random.Next() % 4;

                switch (direction)
                {
                case 0:
                    gameState.Enemies[i].Velocity.X = monster_speed;
                    gameState.Enemies[i].Velocity.Y = 0.0f;
                    break;

                case 1:
                    gameState.Enemies[i].Velocity.X = -monster_speed;
                    gameState.Enemies[i].Velocity.Y = 0.0f;
                    break;

                case 2:
                    gameState.Enemies[i].Velocity.X = 0.0f;
                    gameState.Enemies[i].Velocity.Y = monster_speed;
                    break;

                case 3:
                default:
                    gameState.Enemies[i].Velocity.X = 0.0f;
                    gameState.Enemies[i].Velocity.Y = -monster_speed;
                    break;
                }
            }
        }
        //****************************************************************//
        // This function updates the position of each enemy in the game   //
        // and the player, it then checks if there are collisions between //
        // the enemy or the player and raises a PlayerCaught event.       //
        // It also checks if the player has reached the end goal.         //
        //****************************************************************//
        public void UpdatePositions(CGameState gameState, float time_delta)
        {
            ///////////////////////////////////////////////////////////////////
            // First we update the positions of the players and enemies.

            foreach (CSprite sprite in gameState.Enemies)
            {
                UpdatePosition(sprite, time_delta);
            }

            UpdatePosition(gameState.Player, time_delta);


            ///////////////////////////////////////////////////////////////////
            // Next we check if our player has reached the goal position.

            CPoint2f goalPositionPixels = CLevelUtils.GetPixelFromTileCoordinates(m_level.GoalPosition);

            if (IsInCollision(gameState.Player.Position, goalPositionPixels))
            {
                OnGoalReached(this, "You Won!! Goal Reached!!");
                return;
            }


            ///////////////////////////////////////////////////////////////////
            // Next we check each enemy in turn to see if the user has been
            // caught.

            foreach (CSprite sprite in gameState.Enemies)
            {
                if (IsInCollision(gameState.Player.Position, sprite.Position))
                {
                    OnPlayerCaught(this, "You Lose!! Player Caught!!");
                    break;
                }
            }
        }
        //****************************************************************//
        // 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);
        }