/// <summary>
 /// Create the player
 /// </summary>
 /// <param name="col">The column of the tile where the player starts</param>
 /// <param name="row">The row of the tile where the player starts</param>
 /// <param name="colour">The starting colour of the tank</param>
 public void MakePlayer(float col, float row, Brush colour)
 {
     _playerTank = new Tank(col, row, colour);
 }
        /// <summary>
        /// Reads and loads the file into the battlefield interface
        /// </summary>
        /// <param name="mapString">The file name that contains the map stage</param>
        private void LoadMapString(string mapString)
        {
            //Store the lines in the text file
            string[] lines;
            //Count the amount of line in the text file
            int count = 0;



            //This is to get the amount of lines in the file
            //Read the file that contains the stage
            using (StreamReader file = new StreamReader(mapString))
            {
                //Check if there is a line and loop
                while (file.Peek() != -1)
                {
                    //Goes to next line
                    file.ReadLine();
                    ////Increase count of lines
                    count++;
                }
                //State the size of the int array
                lines = new string[count];
            }
            //Read the file to store information of the file's lines
            using (StreamReader file = new StreamReader(mapString))
            {
                //Loop to save the file lines to the array
                for (int i = 0; i < count; i++)
                {
                    //convert file line to int and store it in the array
                    lines[i] = file.ReadLine();
                }
            }
            //Nested loop for creating the map tiles in the battlefield interface
            //Checking each row of battlefield
            int col         = _battlefield.GetLength(0);
            int row         = _battlefield.GetLength(1);
            int tankCounter = 0;

            for (int y = 0; y < row; y++)
            {
                //Checking each column of battlefield
                for (int x = 0; x < col; x++)
                {
                    //Create char values for each number in the file line
                    char[] line = lines[y].ToCharArray();
                    lines[y] = lines[y].Trim();
                    //Convert each char value into int
                    int value = (int)char.GetNumericValue(line[x]);
                    //Check if it is a valid tile type value
                    if (value <= NUMBER_OF_TILES_TYPES)
                    {
                        //Create battlefield interface
                        _battlefield[x, y] = (MapTile)value;
                        PointF location = new PointF(x * TILE_SIZE, y * TILE_SIZE);
                        //Check if battlefield tile is the starting location
                        if (_battlefield[x, y] == MapTile.StartingLocation)
                        {
                            //Create player on starting location battlefield tile
                            _playerTank = new Tank(x * TILE_SIZE, y * TILE_SIZE, Brushes.Black);
                        }
                        if (_battlefield[x, y] == MapTile.NormalTank)
                        {
                            tankCounter++;
                            _enemyTank.Add(new AITanks(x * TILE_SIZE, y * TILE_SIZE, 2, location, Brushes.Brown));
                        }
                        if (_battlefield[x, y] == MapTile.RocketTankStartLocation)
                        {
                            tankCounter++;
                            //_enemyTank.Add(new )
                        }
                        if (_battlefield[x, y] == MapTile.DisabledTankStartTank)
                        {
                            tankCounter++;
                            _enemyTank.Add(new AserranDisabledTank(x * TILE_SIZE, y * TILE_SIZE, 2, location, Brushes.Brown));
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// create the graphic of the player barrel by fetching the 4 points of it
        /// </summary>
        /// <param name="tank"> pass in the player tank location </param>
        /// <returns> return the array of the barrel points for the player </returns>
        private PointF[] CreateTankBarrelGraphic(Tank tank)
        {
            //make array for the barrel ppints
            Point[] basePoints = new Point[4];

            // right side
            if (mouseLocation.X > player.Location.X)
            {
                // top left corner
                basePoints[0] = new Point(-5, -2);

                //top right corner
                basePoints[1] = new Point(25, -2);

                //bottom right corner
                basePoints[2] = new Point(25, 2);

                //bottom left corner
                basePoints[3] = new Point(-5, 2);
            }

            //left side
            else if (mouseLocation.X < player.Location.X)
            {
                // top left corner
                basePoints[0] = new Point(-25, -2);

                //top right corner
                basePoints[1] = new Point(5, -2);

                //bottom right corner
                basePoints[2] = new Point(5, 2);

                //bottom left corner
                basePoints[3] = new Point(-25, 2);
            }
            else if (mouseLocation.X == player.Location.X)
            {
                if (mouseLocation.Y > player.Location.Y)
                {
                    // top left corner
                    basePoints[0] = new Point(-5, -2);

                    //top right corner
                    basePoints[1] = new Point(25, -2);

                    //bottom right corner
                    basePoints[2] = new Point(25, 2);

                    //bottom left corner
                    basePoints[3] = new Point(-5, 2);
                }

                else
                {
                    // top left corner
                    basePoints[0] = new Point(-25, -2);

                    //top right corner
                    basePoints[1] = new Point(5, -2);

                    //bottom right corner
                    basePoints[2] = new Point(5, 2);

                    //bottom left corner
                    basePoints[3] = new Point(-25, 2);
                }
            }



            float angle = -(float)Math.Atan((player.Location.Y - mouseLocation.Y + (player.Size.Height / 2)) / (mouseLocation.X - player.Location.X - (player.Size.Width / 2)));

            //float angle = (float)Math.Atan((mouseLocation.Y + player.Location.Y + (player.Size.Height / 2)) / (mouseLocation.X - player.Location.X + (player.Size.Width / 2)));
            //Barrel Points Array for the modified points
            PointF[] barrelPoints = new PointF[4];
            //Barrel X Values are 8 pixels from tanks x value and 8 pixels from tanks x value plus width
            //Barrel Y values are 20 pixels from tank y value plus height divided by 2

            float cos = (float)Math.Cos(angle);
            float sin = (float)Math.Sin(angle);

            // put the base points into the barrel point
            for (int i = 0; i < 4; i++)
            {
                barrelPoints[i] = new PointF(basePoints[i].X * cos + basePoints[i].Y * sin + player.Location.X + 10, basePoints[i].X * sin - basePoints[i].Y * cos + player.Location.Y + 10);
            }

            return(barrelPoints);
        }