Exemplo n.º 1
0
        public void Update(GameTimer timer, bool soundMuted)
        {
            fireTouch = false;
            currentBombTouch = false;
            currentHealthPowerupTouch = false;
            currentAmmoPowerupTouch = false;

            shootButton.ButtonPressed = false;
            bombButton.ButtonPressed = false;

            tc = TouchPanel.GetState();
            if (tc.Count > 0)
                foreach (TouchLocation tl in tc)
                {
                    if (tl.Position.X > 650 && tl.Position.Y > 350)
                    {
                        fireTouch = true;
                        shootButton.ButtonPressed = true;
                    }
                    else if(tl.Position.X < 150 && tl.Position.Y > 350 && !currentBombTouch)
                    {
                        currentBombTouch = true;
                        bombButton.ButtonPressed = true;
                    }
                    else if ((tl.Position.X > 150 && tl.Position.X < 182) && (tl.Position.Y > 438 && tl.Position.Y < 470) && !currentHealthPowerupTouch)
                    {
                        currentHealthPowerupTouch = true;
                    }
                    else if ((tl.Position.X > 618 && tl.Position.X < 650) && (tl.Position.Y > 438 && tl.Position.Y < 470) && !currentAmmoPowerupTouch)
                    {
                        currentAmmoPowerupTouch = true;
                    }
                }

            if (!this.IsAlive && !explosionCreated && !this.Crash)
            {
                eh.CreateExplosion("normal", this.Position, contentManager);
                explosionCreated = true;
                //this.score.SaveScore();
            }
            else if (!this.IsAlive && !explosionCreated && this.Crash)
            {
                eh.CreateExplosion("huge", new Vector2(this.X - 32, this.Y - 64), contentManager);
                explosionCreated = true;
                //this.score.SaveScore();
            }
            else if (this.IsAlive)
            {
                if (this.Y < 0)
                    this.Rotation *= -1f;

                if (fireTouch && reloadTime == 0 && this.Ammo > 0)
                {
                    Vector2 direction = new Vector2((float)Math.Cos((double)(this.Rotation)), (float)Math.Sin((double)(this.Rotation)));
                    direction.Normalize();
                    Bullet newBullet = new Bullet(new Vector2(this.X + 2, this.Y + 3), direction, this.Rotation);
                    newBullet.LoadContent(contentManager);
                    bullets.Add(newBullet);
                    if (!soundMuted)
                        browning.Play(0.5f, 0, 0);
                    this.Ammo -= 1;
                }
                else if (currentBombTouch && !oldBombTouch && this.AvailibleBombs > 0)
                {
                    Bomb newBomb = new Bomb(this.Position);
                    newBomb.LoadContent(contentManager);
                    bombs.Add(newBomb);
                    AvailibleBombs--;
                }

                if (currentHealthPowerupTouch && !oldHealthPowerupTouch && this.healthPowerups.Count > 0)
                {
                    if (this.Hitpoints + 5 < this.MaxHealth)
                        this.Hitpoints += 5;
                    else
                        this.Hitpoints = this.MaxHealth;
                    this.healthPowerups.RemoveAt(healthPowerups.Count - 1);
                }
                if (currentAmmoPowerupTouch && !oldAmmoPowerupTouch && this.ammoPowerups.Count > 0)
                {
                    if (this.Ammo + 50 < this.MaxAmmo)
                        this.Ammo += 50;
                    else
                        this.Ammo = this.MaxAmmo;
                    this.ammoPowerups.RemoveAt(ammoPowerups.Count - 1);
                }

                reloadTime += (float)timer.UpdateInterval.TotalSeconds;
                if (reloadTime > 0.05f)
                    reloadTime = 0;

                this.Y += 5f * this.Rotation;

                healthBar.FilledWidth = this.Hitpoints * healthBar.W / maxHealth;
                ammoBar.FilledWidth = this.Ammo * ammoBar.W / maxAmmo;

                this.Animate();
            }

            foreach (Bullet b in bullets)
            {
                b.Update(timer);
                if (b.Position.X - this.Position.X > 700)
                {
                    bullets.Remove(b);
                    break;
                }
            }

            foreach (Bomb b in bombs)
            {
                b.Update(timer);
                if (b.Position.Y > 445)
                {
                    eh.CreateExplosion("normal", b.Position, contentManager);
                    bombs.Remove(b);
                    break;
                }
            }

            eh.Update(timer, this.IsAlive, soundMuted); //if palyer dies background stops so explosion can't move away, on the other hand when bomb lands explosion has to go off the screen

            oldBombTouch = currentBombTouch;

            oldAmmoPowerupTouch = currentAmmoPowerupTouch;

            oldHealthPowerupTouch = currentHealthPowerupTouch;

            if (this.Hitpoints < 1 || this.Y > 445)
            {
                this.IsAlive = false;

                if (this.Y > 445)
                    this.Crash = true;
            }
        }
Exemplo n.º 2
0
        public void Update(GameTimer timer, PowerupHandler pu , ContentManager theContentManager, Player player, bool soundsMuted)
        {
            this.Position += direction * velocity * (float)timer.UpdateInterval.TotalSeconds;

            if (this.Hitpoints < 1 && !ExplosionCreated)
            {
                this.GeneratePowerups(pu, theContentManager);

                this.IsAlive = false;
                explosionHandler.CreateExplosion("normal", new Vector2(this.X - 47, this.Y - 87), contentManager);
                this.ExplosionCreated = true;
                player.score.AddPoints(10);
            }

            if (this.IsAlive &&  reloadTime == 0 && random.Next(this.Hitpoints / 2 + 1) == 1)
            {
                Bullet newBullet = new Bullet(new Vector2(this.X - 45, this.Y - 84), direction, this.Rotation);
                newBullet.LoadContent(contentManager);
                bullets.Add(newBullet);
                //if (!soundsMuted)
                //    mg.Play(0.075f, 0, 0);
            }

            reloadTime += (float)timer.UpdateInterval.TotalSeconds;
            if (reloadTime > 0.05f)
                reloadTime = 0;

            foreach (Bullet b in bullets)
            {
                b.Update(timer);
                if (b.Position.X - this.Position.X > 700)
                {
                    bullets.Remove(b);
                    break;
                }
            }

            explosionHandler.Update(timer, player.IsAlive, soundsMuted); //player.IsAlive indicates that all explosions are moving towards the player as long palyer is alive and background is scrolling

            this.Animate(player);
        }