예제 #1
0
        //Wander method.
        public void Wander(Map m, Tank t, List<EnemyTanks>others)
        {
            //If this AI tank is defeated, add its bounding spheres to the list of walls in the map.
            if(this.LivesRemaining <= 0)
            {
                if (!addedToWalls)
                {
                    foreach (BoundingSphere sp in this.Spheres)
                    {
                        m.Walls.Add(sp);
                    }

                    addedToWalls = true;
                }

               return;

            }

            Random rand = new Random();

            float rotStep = 1.0f;

            //calculate random angle of direction to move in. //Delayed by a counter to avoid jitter.
            if (count == 10)
            {
                //Calculate new angle.
                float tempAngle = (float)rand.NextDouble() * (rotStep - (-rotStep)) - rotStep;
                ChassisAngle += tempAngle;
                updateSpheres();

                //Check if new angle conflicts with walls. If so, undo angle.
                foreach (BoundingSphere b in m.Walls)
                {
                    for (int i = 0; i < this.Spheres.Count; i++)
                    {

                        if (this.Spheres[i].Intersects(b) || this.AvoidanceSphere.Intersects(b))
                        {
                            ChassisAngle -= tempAngle;
                            updateSpheres();
                            break;
                        }
                    }
                }
            }

            // update position
            Vector2 newPos = new Vector2((float)Math.Cos((ChassisAngle)) * 1f, (float)Math.Sin((ChassisAngle)) * 1f);
            updatePosition(m, newPos, t, others);

            //If tank has a target, aim cannon in its direction to fire.
            if (this.Target!=null)
            {
                Vector2 aim = new Vector2(this.Target.Position.X, this.Target.Position.Y) - new Vector2(this.Position.X, this.Position.Y);
                if (this.Target.Position.X > this.Position.X && this.Target.Position.Y > this.Position.Y)
                {
                    this.CannonAngle = (float)Math.Atan2(aim.Y, aim.X);
                }
                else
                {
                    this.CannonAngle = (float)Math.Atan2(aim.Y, aim.X);
                }
            }

            //Else, allign cannon with chassis.
            else
            {
                this.CannonAngle = this.ChassisAngle;
            }

            //Update bounding spheres and delay counter..
            updateSpheres();
            count--;
            if (count == 0)
            {
                count = 10;
            }
        }
예제 #2
0
        //This method uses the bounding sphere in front of the tank to scan for nearby tanks.
        public bool scan(Tank t, List<EnemyTanks>others)
        {
            //Do nothing if this tank is defeated.
            if (this.LivesRemaining <= 0)
            {
                return false;
            }

            //If the player tank is spotted, it is the new target.
            for (int j = 0; j < t.Spheres.Count; j++)
            {
                //The player is only added as a target if the seek timer has been reset.
                if (this.ShootSphere.Intersects(t.Spheres[j]) && foundTarget == false && t.LivesRemaining > 0 && seekCount == 100)
                {
                    this.foundTarget = true;
                    this.target = t;
                    return foundTarget;
                }

            }

            //If enemy tank is spotted, it is the new target.
            foreach (EnemyTanks et in others)
            {
                if (this.TankColor != et.TankColor)
                {

                   for (int j = 0; j < et.Spheres.Count; j++)
                   {
                       if (this.ShootSphere.Intersects(et.Spheres[j]) && foundTarget == false && et.LivesRemaining > 0)
                       {
                           this.foundTarget = true;
                           this.target = et;
                           return foundTarget;
                       }
                   }

                }
            }

            //If no targets are found, reset scanning variables.
            this.target = null;
            this.foundTarget = false;
            return foundTarget;
        }
예제 #3
0
        //This method updates position.
        public void updatePosition(Map m, Vector2 pos, Tank t, List<EnemyTanks> others)
        {
            Position += pos; //Add new position.

            //If new position conflicts with walls, undo the position change.
            //Also set off seek delay counter. If tank is seeking player, it will lose sight of them upon collision for a short time.
            foreach (BoundingSphere b in m.Walls)
            {
                for (int i = 0; i < this.Spheres.Count; i++)
                {
                    if (this.Spheres[i].Intersects(b))
                    {
                        if (seekCount == 100 && seekCount > 0)
                        {
                            seekCount--;
                        }

                        this.target = null;
                        this.Position -= pos;
                        this.updateSpheres();
                        break;
                    }
                }
            }

            //If tank is close enough to player tank, check for collision. If collides, undo position change.
            if ((this.Position - t.Position).Length() <= 65)
            {
                for (int i = 0; i < this.Spheres.Count; i++)
                {
                    for (int j = 0; j < t.Spheres.Count; j++)
                    {
                        if (this.Spheres[i].Intersects(t.Spheres[j]))
                        {
                            this.Position -= pos;
                            this.updateSpheres();
                            break;
                        }
                    }
                }
            }

            //If tank is close enough to other AI tanks, check for collision. If collides, undo position change.
            foreach (EnemyTanks et in others)
            {
                if (this.TankColor != et.TankColor)
                {
                    if ((this.Position - et.Position).Length() <= 65)
                    {
                        for (int i = 0; i < this.Spheres.Count; i++)
                        {
                            for (int j = 0; j < et.Spheres.Count; j++)
                            {
                                if (this.Spheres[i].Intersects(et.Spheres[j]))
                                {
                                    this.Position -= pos;
                                    this.updateSpheres();
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            //Update seek delay counter.
            if (seekCount < 100 && seekCount > 0)
            {
                seekCount--;
            }

            if (seekCount == 0)
            {
                seekCount = 100;
            }
        }
예제 #4
0
        //Seek method. The AI tank will seek the player until it gets close enough or hits a wall.
        public void Seek(Map m, Tank t, List<EnemyTanks> others)
        {
            //Don't seek if the target is not the player tank.
            if (target != t)
            {
                return;
            }

            //If seek counter is full, seek player.
            if (seekCount == 100)
            {
                //Update cannon angle.
                Vector2 aim = new Vector2(this.Target.Position.X, this.Target.Position.Y) - new Vector2(this.Position.X, this.Position.Y);
                if (this.Target.Position.X > this.Position.X && this.Target.Position.Y > this.Position.Y)
                {
                    this.CannonAngle = (float)Math.Atan2(aim.Y, aim.X);
                }
                else
                {
                    this.CannonAngle = (float)Math.Atan2(aim.Y, aim.X);
                }

                Vector2 direction = target.Position - Position;

                direction.Normalize();
                Vector2 newPos = new Vector2((float)Math.Cos((ChassisAngle)) * direction.X, (float)Math.Sin((ChassisAngle)) * direction.Y);

                //Chase until close enough to player tank.
                if ((this.Position - target.Position).Length() >= 105)
                {
                    this.still = false;
                    ChassisAngle = CannonAngle;
                    updateSpheres();
                    this.updatePosition(m, direction, t, others);
                }

                //When close enough, stop chasing player.
                else
                {
                    this.still = true;
                }

            }
        }
예제 #5
0
        //This method updates the active bullets.
        public void update(Map m, Tank t1, List<EnemyTanks> others)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                if (bullets[i] != null)
                {
                    Vector2 direction = bullets[i].Target - bullets[i].Position; //Move in target's direction.

                    if (direction.Length() <= 1.5) //Don't fire if aimed very close to the firing tank.
                    {
                        bullets[i].LifeTime = 0;
                    }

                    direction.Normalize();
                    bullets[i].Position += direction * 2.7f;

                    bullets[i].LifeTime--;

                    int x = (int)Math.Floor(bullets[i].Position.X / 20);
                    int y = (int)Math.Floor(bullets[i].Position.Y / 20);

                    //If bullet is out of bounds, over a wall tile or out of lifetime(fire range), remove bullet object.
                    if (x < 0 || x > 63 || y < 0 || y > 35 || m.getTileType(x, y) == 1 || bullets[i].LifeTime < 1)
                    {
                        bullets.RemoveAt(i);
                        i--;
                        break;

                    }

                    if (i >= 0)
                    {
                        //If bullet is within range of player tank, if it overlaps wth any of its spheres, remove bullet object.
                        if ((bullets[i].Position - t1.Position).Length() <= 31 && bullets[i].BulletColor != t1.TankColor)
                        {
                            foreach (BoundingSphere s in t1.Spheres)
                            {
                                Vector2 spherePos = new Vector2(s.Center.X, s.Center.Y);
                                if ((bullets[i].Position - spherePos).Length() <= 10)
                                {
                                    if (t1.LivesRemaining > 0)
                                    {
                                        t1.LivesRemaining--;
                                    }
                                    bullets.RemoveAt(i);
                                    i--;
                                    break;

                                }
                            }
                        }
                    }

                    if (i >= 0)
                    {
                        foreach (EnemyTanks e in others)
                        {
                            if (i >= 0)
                            {
                                //If bullet is within range of another AI tank, if it overlaps wth any of its spheres, remove bullet object.
                                if ((bullets[i].Position - e.Position).Length() <= 31 && bullets[i].BulletColor != e.TankColor)
                                {
                                    foreach (BoundingSphere s in e.Spheres)
                                    {
                                        Vector2 spherePos = new Vector2(s.Center.X, s.Center.Y);
                                        if ((bullets[i].Position - spherePos).Length() <= 10)
                                        {
                                            if (e.LivesRemaining > 0)
                                            {
                                                e.LivesRemaining--;
                                            }
                                            bullets.RemoveAt(i);
                                            i--;
                                            break;
                                        }
                                    }
                                }
                            }

                            else
                            {
                                break;
                            }
                        }
                    }

                }
            }
        }
예제 #6
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            lastKs = ks;
            ks = Keyboard.GetState();
            Keys[] keys = ks.GetPressedKeys();

            lastMs = ms;
            ms = Mouse.GetState();

            switch (currentGameState)
            {
                //Display all tutorial messages in succession.
                case GameState.Tutorial:
                    if (ks.IsKeyDown(Keys.Space) && lastKs.IsKeyUp(Keys.Space))
                    {
                        switch (currentTutorialState)
                        {
                            case TutorialState.Tutorial1:
                                currentTutorialState = TutorialState.Tutorial2;
                                break;
                            case TutorialState.Tutorial2:
                                currentTutorialState = TutorialState.Tutorial3;
                                break;
                            case TutorialState.Tutorial3:
                                currentTutorialState = TutorialState.Tutorial4;
                                break;
                            case TutorialState.Tutorial4:
                                currentTutorialState = TutorialState.Tutorial5;
                                break;
                            case TutorialState.Tutorial5:
                                currentTutorialState = TutorialState.Tutorial6;
                                break;
                            case TutorialState.Tutorial6:
                                currentGameState = GameState.Play;
                                break;
                        }
                    }
                    break;

                //Main menu state.
                case GameState.MainMenu:
                    if (btnArray[0].isClicked == true)
                    {
                        currentGameState = GameState.Tutorial;
                        MediaPlayer.Stop();
                        MediaPlayer.Play(gameMusic);
                        MediaPlayer.IsRepeating = true;
                    }
                    else if (btnArray[1].isClicked == true)
                    {
                        currentGameState = GameState.Controls;
                        if (btnBack.isClicked == true)
                            btnBack.isClicked = false;
                    }
                    else if (btnArray[2].isClicked == true)
                        this.Exit();
                    btnArray[0].Update(ms);
                    btnArray[1].Update(ms);
                    btnArray[2].Update(ms);
                    break;
                case GameState.Controls:
                    if (btnBack.isClicked == true)
                    {
                        currentGameState = GameState.MainMenu;
                        btnArray[1].isClicked = false;
                    }
                    btnBack.Update(ms);
                    break;

                case GameState.Play:

                    //Resets the tanks and map wall data in case of bugs.
                    if (resetTanks)
                    {
                        m1 = new Map(tiles);
                        t1 = new Tank(tankTex, new Vector2(80, 60), 3);
                        ets.Clear();
                        ets.Add(new EnemyTanks(tankTex, new Vector2(80, 600), 1));
                        ets.Add(new EnemyTanks(tankTex, new Vector2(1180, 600), 2));
                        ets[1].ChassisAngle = -90;
                        ets[1].CannonAngle = -90;
                        ets.Add(new EnemyTanks(tankTex, new Vector2(1180, 60), 0));
                        ets[2].ChassisAngle = -180;
                        ets[2].CannonAngle = -180;

                        resetTanks = false;
                    }

                    //Update particle emitters for enemy tanks (displayed when tanks are defeated).
                    for (int i = 0; i < 3; i++)
                    {
                        explosions[i].Update();
                        explosions[i].EmitterLocation = ets[i].Position;
                    }

                    //If player tank is defeated, switch to game over.
                    if (t1.livesRemaining <= 0)
                    {
                        currentGameState = GameState.Lose;
                    }

                    //Updated timers for enemy tanks. and update their frames.
                    timer0 += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    timer1 += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    timer2 += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    for (int i = 0; i < 3; i++)
                    {
                        switch (i)
                        {
                            case 0:
                                if (timer0 > interval && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame++;
                                        ets[i].updateRectangles();
                                        timer0 = 0f;
                                    }
                                }
                                if (ets[i].CurrentFrame == 2 && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame = 0;
                                        ets[i].updateRectangles();
                                    }
                                }
                                break;
                            case 1:
                                if (timer1 > interval && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame++;
                                        ets[i].updateRectangles();
                                        timer1 = 0f;
                                    }
                                }
                                if (ets[i].CurrentFrame == 2 && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame = 0;
                                        ets[i].updateRectangles();
                                    }
                                }
                                break;
                            case 2:
                                if (timer2 > interval && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame++;
                                        ets[i].updateRectangles();
                                        timer2 = 0f;
                                    }
                                }
                                if (ets[i].CurrentFrame == 2 && ets[i].LivesRemaining > 0)
                                {
                                    if (!ets[i].Still)
                                    {
                                        ets[i].CurrentFrame = 0;
                                        ets[i].updateRectangles();
                                    }
                                }
                                break;

                            default:
                                break;

                        }

                        //Check if enemy tanks found targets. If one is found, create new bullets to fire.
                        bool willShoot = ets[i].scan(t1, ets);
                        if (willShoot)
                        {

                            switch (i)
                            {
                                case 0:
                                    if (canFire0)
                                    {
                                        bm.createBullet(ets[i].TankColor, ets[i].Position, new Vector2(ets[i].Target.Position.X, ets[i].Target.Position.Y));
                                        canFire0 = false;
                                        cdBar1.cd = 0; //Update cooldownt timer.
                                    }

                                    ets[i].FoundTarget = false;
                                    break;
                                case 1:
                                    if (canFire1)
                                    {
                                        bm.createBullet(ets[i].TankColor, ets[i].Position, new Vector2(ets[i].Target.Position.X, ets[i].Target.Position.Y));
                                        canFire1 = false;
                                        cdBar2.cd = 0;
                                    }

                                    ets[i].FoundTarget = false;
                                    break;
                                case 2:
                                    if (canFire2)
                                    {
                                        bm.createBullet(ets[i].TankColor, ets[i].Position, new Vector2(ets[i].Target.Position.X, ets[i].Target.Position.Y));
                                        canFire2 = false;
                                        cdBar3.cd = 0;
                                    }

                                    ets[i].FoundTarget = false;
                                    break;

                                default:
                                    break;
                            }

                        }

                        //If enemy tank found player, seek player. Else, wander.
                        if (ets[i].Target != null && ets[i].Target == t1)
                        {
                            ets[i].Seek(m1, t1, ets);
                        }
                        else
                        {
                            ets[i].Wander(m1, t1, ets);
                            }
                    }

                    //Remove mosue visibility (for targeting reticle display)
                    IsMouseVisible = false;
                    //Mouse input handler.
                        if (lastMs.LeftButton == ButtonState.Released && ms.LeftButton == ButtonState.Pressed)
                        {
                            if (canPlayerFire)
                            {
                                bm.createBullet(t1.TankColor, t1.Position, new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
                                canPlayerFire = false;
                                cdBar.cd = 0;
                            }
                        }

                    //Update all cooldown bars.
                        if (cdBar.cd == 0)
                        {
                            while (cdBar.cd < playerFireCount)
                                cdBar.cd++;
                        }
                        else if (cdBar.cd >= playerFireCount)
                        {
                            cdBar.cd = playerFireCount;
                        }

                        if (cdBar1.cd == 0)
                        {
                            while (cdBar1.cd < fireCount0)
                                cdBar1.cd++;
                        }
                        else if (cdBar1.cd >= fireCount0)
                        {
                            cdBar1.cd = fireCount0;
                        }

                        if (cdBar2.cd == 0)
                        {
                            while (cdBar2.cd < fireCount1)
                                cdBar2.cd++;
                        }
                        else if (cdBar2.cd >= fireCount1)
                        {
                            cdBar2.cd = fireCount1;
                        }

                        if (cdBar3.cd == 0)
                        {
                            while (cdBar3.cd < fireCount2)
                                cdBar3.cd++;
                        }
                        else if (cdBar3.cd >= fireCount2)
                        {
                            cdBar3.cd = fireCount2;
                        }

                    //Input handler.
                        foreach (Keys key in keys)
                        {
                            switch (key)
                            {
                                //Forward motion.
                                case Keys.W:
                                    playerTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                                    if (playerTimer > interval)
                                    {
                                        //Show the next frame
                                        t1.CurrentFrame++;
                                        t1.updateRectangles();
                                        //Reset the timer
                                        playerTimer = 0f;
                                    }

                                    if (t1.CurrentFrame == 2)
                                    {
                                        t1.CurrentFrame = 0;
                                        t1.updateRectangles();
                                    }

                                    t1.Position += new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                    t1.updateSpheres();

                                    //Check for collisions with walls after movement. If collides, undo movement.
                                    foreach(BoundingSphere b in m1.Walls)
                                    {
                                        for (int i = 0; i < t1.Spheres.Count; i++)
                                        {
                                            int x = (int)Math.Floor(t1.Spheres[i].Center.X / 20);
                                            int y = (int)Math.Floor(t1.Spheres[i].Center.Y / 20);

                                            if (t1.Spheres[i].Intersects(b))
                                            {
                                                t1.Position -= new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                                t1.updateSpheres();
                                                break;
                                            }
                                        }
                                    }

                                    //Check for collisions with other tanks after movement. If collides, undo movement.
                                    foreach (EnemyTanks et in ets)
                                    {
                                        if ((t1.Position - et.Position).Length() <= 65)
                                        {
                                            for (int i = 0; i < t1.Spheres.Count; i++)
                                            {
                                                for (int j = 0; j < et.Spheres.Count; j++)
                                                {
                                                    if (t1.Spheres[i].Intersects(et.Spheres[j]))
                                                    {
                                                        t1.Position -= new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                                        t1.updateSpheres();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    break;

                                    //Same procedures as forward motion.
                                case Keys.S:
                                    playerTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                                    if (playerTimer > interval)
                                    {
                                        //Show the next frame
                                        t1.CurrentFrame++;
                                        t1.updateRectangles();
                                        //Reset the timer
                                        playerTimer = 0f;
                                    }

                                    if (t1.CurrentFrame == 2)
                                    {
                                        t1.CurrentFrame = 0;
                                        t1.updateRectangles();
                                    }

                                    t1.Position -= new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                    t1.updateSpheres();
                                    foreach (BoundingSphere b in m1.Walls)
                                    {
                                        for (int i = 0; i < t1.Spheres.Count; i++)
                                        {

                                            if (t1.Spheres[i].Intersects(b))
                                            {

                                                t1.Position += new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                                t1.updateSpheres();
                                                break;
                                            }
                                        }
                                    }
                                    foreach (EnemyTanks et in ets)
                                    {
                                        if ((t1.Position - et.Position).Length() <= 65)
                                        {
                                            for (int i = 0; i < t1.Spheres.Count; i++)
                                            {
                                                for (int j = 0; j < et.Spheres.Count; j++)
                                                {
                                                    if (t1.Spheres[i].Intersects(et.Spheres[j]))
                                                    {
                                                        t1.Position += new Vector2((float)Math.Cos((t1.ChassisAngle)) * step, (float)Math.Sin((t1.ChassisAngle)) * step);
                                                        t1.updateSpheres();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    break;

                                    //A and D keys rotate.
                                case Keys.A:
                                    t1.ChassisAngle -= 0.03f;
                                    t1.updateRectangles();
                                    t1.updateSpheres();
                                    foreach (BoundingSphere b in m1.Walls)
                                    {
                                        for (int i = 0; i < t1.Spheres.Count; i++)
                                        {
                                            if (t1.Spheres[i].Intersects(b))
                                            {
                                                t1.ChassisAngle += 0.03f;
                                                t1.updateRectangles();
                                                t1.updateSpheres();
                                                break;
                                            }
                                        }
                                    }

                                    foreach (EnemyTanks et in ets)
                                    {
                                        if ((t1.Position - et.Position).Length() <= 65)
                                        {
                                            for (int i = 0; i < t1.Spheres.Count; i++)
                                            {
                                                for (int j = 0; j < et.Spheres.Count; j++)
                                                {
                                                    if (t1.Spheres[i].Intersects(et.Spheres[j]))
                                                    {
                                                        t1.ChassisAngle += 0.03f;
                                                        t1.updateSpheres();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    break;

                                case Keys.D:
                                    t1.ChassisAngle += 0.03f;
                                    t1.updateRectangles();
                                    t1.updateSpheres();
                                    foreach (BoundingSphere b in m1.Walls)
                                    {
                                        for (int i = 0; i < t1.Spheres.Count; i++)
                                        {
                                            if (t1.Spheres[i].Intersects(b))
                                            {
                                                t1.ChassisAngle -= 0.03f;
                                                t1.updateRectangles();
                                                t1.updateSpheres();
                                                break;
                                            }
                                        }
                                    }

                                    foreach (EnemyTanks et in ets)
                                    {
                                        if ((t1.Position - et.Position).Length() <= 65)
                                        {
                                            for (int i = 0; i < t1.Spheres.Count; i++)
                                            {
                                                for (int j = 0; j < et.Spheres.Count; j++)
                                                {
                                                    if (t1.Spheres[i].Intersects(et.Spheres[j]))
                                                    {
                                                        t1.ChassisAngle -= 0.03f;
                                                        t1.updateSpheres();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    break;

                                    //Debug controls. Space key displays positions of bounding spheres on tank.
                                    //U key resets the tanks and map wall data.
                                case Keys.Space:
                                    if (lastKs.IsKeyUp(Keys.Space))
                                    {
                                        Tank.DrawBSPheres = !Tank.DrawBSPheres;
                                    }
                                    break;

                                case Keys.U:
                                    if (lastKs.IsKeyUp(Keys.U))
                                    {
                                        resetTanks = true;
                                    }
                                    break;
                            }

                        }

                    //Compute player tank's cannon angle based on mouse position.
                        Vector2 playerAim = new Vector2(Mouse.GetState().X, Mouse.GetState().Y) - new Vector2(t1.Position.X, t1.Position.Y);
                        if (Mouse.GetState().X > t1.Position.X && Mouse.GetState().Y > t1.Position.Y)
                        {
                            t1.CannonAngle = (float)Math.Atan2(playerAim.Y, playerAim.X);
                        }
                        else
                        {
                            t1.CannonAngle = (float)Math.Atan2(playerAim.Y, playerAim.X);
                        }

                    //Update cooldown timers tanks.
                        if (playerFireCount > 0 && !canPlayerFire)
                        {
                            playerFireCount--;
                        }
                        else
                        {
                            canPlayerFire = true;
                            playerFireCount = 100;
                        }

                        if (fireCount0 > 0 && !canFire0)
                        {
                            fireCount0--;
                            ets[0].Target = null;
                        }
                        else
                        {
                            canFire0 = true;
                            fireCount0 = 100;
                        }

                        if (fireCount1 > 0 && !canFire1)
                        {
                            fireCount1--;
                            ets[1].Target = null;
                        }
                        else
                        {
                            canFire1 = true;
                            fireCount1 = 100;
                        }

                        if (fireCount2 > 0 && !canFire2)
                        {
                            fireCount2--;
                            ets[2].Target = null;
                        }
                        else
                        {
                            canFire2 = true;
                            fireCount2 = 100;
                        }

                    //Update all active bullets.
                        bm.update(m1, t1, ets);
                        break;
                    }

            base.Update(gameTime);
        }
예제 #7
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            reticle = Content.Load<Texture2D>("reticle");
            tankTex = Content.Load<Texture2D>("tanksheet4");
            bullet = Content.Load<Texture2D>("bullet");
            tiles = Content.Load<Texture2D>("tiles");
            frame = Content.Load<Texture2D>("stwhudframe");

            wizard = Content.Load<Texture2D>("wizard");
            speechBubble = Content.Load<Texture2D>("bubble");
            keyboardKeys = Content.Load<Texture2D>("keyboardcontrols");

            pericles20 = Content.Load<SpriteFont>("Pericles20");
            pericles16 = Content.Load<SpriteFont>("Pericles16");

            GameOver = Content.Load<Texture2D>("gameOver");

            winFool = Content.Load<Texture2D>("win");

            Tank.texture2 = bullet;

            livesOffset = 300;

            m1 = new Map(tiles);
            t1 = new Tank(tankTex, new Vector2(80, 60), 3);
            t1Life = new Tank(tankTex, new Vector2(graphics.PreferredBackBufferWidth / 20, (21 * graphics.PreferredBackBufferHeight / 22)), 3);

            ets.Add(new EnemyTanks(tankTex, new Vector2(80, 600), 1));
            ets.Add(new EnemyTanks(tankTex, new Vector2(1180, 600), 2));
            ets[1].ChassisAngle = -90;
            ets[1].CannonAngle = -90;
            ets.Add(new EnemyTanks(tankTex, new Vector2(1180, 60), 0));
            ets[2].ChassisAngle = -180;
            ets[2].CannonAngle = -180;

            etLives.Add(new EnemyTanks(tankTex, new Vector2((graphics.PreferredBackBufferWidth / 20) + livesOffset, (21 * graphics.PreferredBackBufferHeight / 22)), 1));
            etLives.Add(new EnemyTanks(tankTex, new Vector2((graphics.PreferredBackBufferWidth / 20) + livesOffset * 2, (21 * graphics.PreferredBackBufferHeight / 22)), 2));
            etLives.Add(new EnemyTanks(tankTex, new Vector2((graphics.PreferredBackBufferWidth / 20) + livesOffset * 3, (21 * graphics.PreferredBackBufferHeight / 22)), 0));

            bm = new BulletManager(bullet);

            cdBar = new CoolDownBar(this, 2);
            cdBar1 = new CoolDownBar(this, 1);
            cdBar2 = new CoolDownBar(this, 3);
            cdBar3 = new CoolDownBar(this, 0);

            //display the mouse
            IsMouseVisible = true;

            Viewport viewPort = new Viewport();
            viewPort.X = 0;
            viewPort.Y = 0;
            viewPort.Width = graphics.PreferredBackBufferWidth;
            viewPort.Height = graphics.PreferredBackBufferHeight + graphics.PreferredBackBufferHeight / 20;

            ctrlsTutorial = new Tutorial(this, Content, viewPort, this.graphics.GraphicsDevice);

            ctrlsTutorial.AKeyText = "Move Left";
            ctrlsTutorial.WKeyText = "Move Up";
            ctrlsTutorial.SKeyText = "Move Down";
            ctrlsTutorial.DKeyText = "Move Right";

            ctrlsTutorial.LeftMouseClickText = "FIRE!";
            ctrlsTutorial.LeftMouseClickText = "FIRE!";

            // title
            title = Content.Load<Texture2D>("title");

            // menu items
            btnPlay = new Button(Content.Load<Texture2D>("btnPlay"), graphics.GraphicsDevice);
            btnPlay.SetPosition(new Vector2((graphics.PreferredBackBufferWidth / 2) + graphics.PreferredBackBufferWidth / 12, (graphics.PreferredBackBufferHeight / 3)));
            btnControls = new Button(Content.Load<Texture2D>("btnControls"), graphics.GraphicsDevice);
            btnControls.SetPosition(new Vector2((int)(graphics.PreferredBackBufferWidth / 2) + graphics.PreferredBackBufferWidth / 12, (int)(graphics.PreferredBackBufferHeight / 3) + (int)(graphics.PreferredBackBufferHeight / 6)));
            btnExit = new Button(Content.Load<Texture2D>("btnExit"), graphics.GraphicsDevice);
            btnExit.SetPosition(new Vector2((int)(graphics.PreferredBackBufferWidth / 2) + graphics.PreferredBackBufferWidth / 12, (int)(graphics.PreferredBackBufferHeight / 3) + (int)(graphics.PreferredBackBufferHeight / 3)));

            // controls screen button
            btnBack = new Button(Content.Load<Texture2D>("btnBack"), graphics.GraphicsDevice);
            btnBack.SetPosition(new Vector2(graphics.PreferredBackBufferWidth / 24, graphics.PreferredBackBufferHeight - ((btnBack.size.Y) + graphics.PreferredBackBufferHeight / 24)));

            menuBackground = Content.Load<Texture2D>("castleMenuBack");
            controlsBackground = Content.Load<Texture2D>("tankSchem");

            hat1 = Content.Load<Texture2D>("hat1");
            hat2 = Content.Load<Texture2D>("hat2");
            hat3 = Content.Load<Texture2D>("hat3");

            //Load textures for the particle effects.
            images = new List<Texture2D>();
            images.Add(Content.Load<Texture2D>("circle"));
            images.Add(Content.Load<Texture2D>("star"));
            images.Add(Content.Load<Texture2D>("diamond"));

            explosions = new List<ParticleSystem>();
            explosions.Add(new ParticleSystem(images, Vector2.Zero, 1, 0));
            explosions.Add(new ParticleSystem(images, Vector2.Zero, 3, 0));
            explosions.Add(new ParticleSystem(images, Vector2.Zero, 2, 0));

            titleMusic = Content.Load<Song>("STW title");
            gameMusic = Content.Load<Song>("STW Battle");
            MediaPlayer.Play(titleMusic);
            MediaPlayer.IsRepeating = true;
            // TODO: use this.Content to load your game content here
        }