ScrollingShooter.PlayerShip.Update C# (CSharp) Méthode

Update() public méthode

Updates the ship
public Update ( float elapsedTime ) : void
elapsedTime float
Résultat void
        public override void Update(float elapsedTime)
        {
            if (this.Dead)
            {
                DeathTimer -= elapsedTime;

                if (DeathTimer <= 0)
                {
                    //Respawn
                    if (this.Lives <= 0)
                    {
                        //Perma Death
                        ScrollingShooterGame.Game.PlayerDeath();
                    }
                    else
                    {
                        this.Health = this.MaxHealth;
                        this.Lives--;
                        this.Dead = false;

                        this.InvincibleTimer = 2;

                        //Respawn or whatever
                    }

                }
                return;
            }

            if (InvincibleTimer > 0)
            {
                InvincibleTimer -= elapsedTime;

                InvincibleFrame -= elapsedTime;
                if (InvincibleFrame < -.1)
                    InvincibleFrame += 0.3f;
            }

            if (!ScrollingShooterGame.LevelManager.Ending)
            {
                KeyboardState currentKeyboardState = Keyboard.GetState();

                // Update timers
                defaultGunTimer += elapsedTime;
                bladesPowerupTimer += elapsedTime;
                energyBlastTimer -= elapsedTime;
                bombTimer += elapsedTime;
                railgunTimer += elapsedTime;
                homingMissileTimer -= elapsedTime;
                shotgunTimer += elapsedTime;
                bubbleTimer += elapsedTime;
                fireballTimer += elapsedTime;
                freezeTimer += elapsedTime;

                if (!drunk)
                {
                    // Steer the ship up or down according to user input
                    if (currentKeyboardState.IsKeyDown(Keys.Up))
                    {
                        position.Y -= elapsedTime * velocity.Y;
                    }
                    else if (currentKeyboardState.IsKeyDown(Keys.Down))
                    {
                        position.Y += elapsedTime * velocity.Y;
                    }

                    // Steer the ship left or right according to user input
                    steeringState = SteeringState.Straight;

                    if (currentKeyboardState.IsKeyDown(Keys.Left))
                    {
                        if (currentKeyboardState.IsKeyDown(Keys.LeftShift) ||
                            currentKeyboardState.IsKeyDown(Keys.RightShift))
                        {
                            steeringState = SteeringState.HardLeft;
                            position.X -= elapsedTime * 2 * velocity.X;

                        }
                        else
                        {
                            steeringState = SteeringState.Left;
                            position.X -= elapsedTime * velocity.X;
                        }
                    }
                    else if (currentKeyboardState.IsKeyDown(Keys.Right))
                    {
                        if (currentKeyboardState.IsKeyDown(Keys.LeftShift) ||
                            currentKeyboardState.IsKeyDown(Keys.RightShift))
                        {
                            position.X += elapsedTime * 2 * velocity.X;
                            steeringState = SteeringState.HardRight;
                        }
                        else
                        {
                            position.X += elapsedTime * velocity.X;
                            steeringState = SteeringState.Right;
                        }
                    }
                }

                //Player is drunk and movements are reversed.
                else
                {
                    //Decrease drunkCounter and make the player sober if their drunk time is up.
                    drunkCounter--;
                    if (drunkCounter == 0)
                    {
                        SoberUp();
                    }
                    // Steer the ship up or down according to user input
                    if (currentKeyboardState.IsKeyDown(Keys.Up))
                    {
                        position.Y += elapsedTime * velocity.Y;
                    }

                    else if (currentKeyboardState.IsKeyDown(Keys.Down))
                    {
                        position.Y -= elapsedTime * velocity.Y;
                    }

                    // Steer the ship left or right according to user input
                    steeringState = SteeringState.Straight;

                    if (currentKeyboardState.IsKeyDown(Keys.Left))
                    {
                        if (currentKeyboardState.IsKeyDown(Keys.LeftShift) ||
                            currentKeyboardState.IsKeyDown(Keys.RightShift))
                        {
                            steeringState = SteeringState.HardLeft;
                            position.X += elapsedTime * 2 * velocity.X;

                        }
                        else
                        {
                            steeringState = SteeringState.Left;
                            position.X += elapsedTime * velocity.X;
                        }
                    }
                    else if (currentKeyboardState.IsKeyDown(Keys.Right))
                    {
                        if (currentKeyboardState.IsKeyDown(Keys.LeftShift) ||
                            currentKeyboardState.IsKeyDown(Keys.RightShift))
                        {
                            position.X -= elapsedTime * 2 * velocity.X;
                            steeringState = SteeringState.HardRight;
                        }
                        else
                        {
                            position.X -= elapsedTime * velocity.X;
                            steeringState = SteeringState.Right;
                        }
                    }
                }
                // Do player clamping
                // Note: 384 = worldView width / 2 and 360 = worldView height / 2
                // I assumed it would be faster to compare hardcoded numbers than to reference the variable directly
                if ((position.X - Bounds.Width / 2) < 0) position.X = Bounds.Width / 2;
                else if ((position.X + Bounds.Width / 2) > 384) position.X = 384 - Bounds.Width / 2;
                if (position.Y < ((ScrollingShooterGame.LevelManager.scrollDistance * -0.5f) + Bounds.Height / 2))
                    position.Y = (ScrollingShooterGame.LevelManager.scrollDistance * -0.5f) + Bounds.Height / 2;
                else if (position.Y > ((ScrollingShooterGame.LevelManager.scrollDistance * -0.5f) + 360 - Bounds.Height / 2))
                    position.Y = (ScrollingShooterGame.LevelManager.scrollDistance * -0.5f) + 360 - Bounds.Height / 2;

                // Fire bomb
                if (currentKeyboardState.IsKeyDown(Keys.B))
                {
                    //checks if player has the bomb power up
                    if ((PowerupType & PowerupType.Bomb) > 0)
                    {
                        if (bombTimer > 1.5f)
                        {
                            ScrollingShooterGame.GameObjectManager.CreateProjectile(ProjectileType.Bomb, position);
                            bombTimer = 0f;
                        }
                    }
                }
                if (bladesPowerupTimer > 10.0f && (PowerupType & PowerupType.Blades) > 0)
                {
                    unApplyBlades();
                }

                // Used to test the energy blast powerup levels
                //if (currentKeyboardState.IsKeyDown(Keys.F) && oldKeyboardState.IsKeyUp(Keys.F))
                // energyBlastLevel++;
                if ((PowerupType & PowerupType.Blades) == 0)
                {
                    // Fire weapons
                    if (currentKeyboardState.IsKeyDown(Keys.Space))
                    {
                        if ((PowerupType & PowerupType.Freezewave) > 0)
                        {
                            if (freezeTimer > .5f)
                            {
                                ScrollingShooterGame.GameObjectManager.CreateProjectile(ProjectileType.FreezewaveProjectile, position);
                                freezeTimer = 0;
                            }
                        }
                        // Streaming weapons
                        if ((PowerupType & PowerupType.BubbleBeam) > 0)
                        {
                            if (bubbleTimer > .1f)
                            {
                                ScrollingShooterGame.GameObjectManager.CreateProjectile(ProjectileType.BubbleBullet, position);
                                bubbleTimer = 0f;
                            }
                        }

                        // Fires a shotgun shot if the shotgun powerup is active and half a second has passed since the last shot
                        if ((PowerupType & PowerupType.ShotgunPowerup) > 0 && shotgunTimer > 0.5f)
                        {
                            TriggerShotgun();
                            shotgunFired.Play();
                            shotgunTimer = 0;
                        }

                        // Default gun
                        if (defaultGunTimer > 0.25f & (PowerupType & PowerupType.Default) > 0)
                        {
                            ScrollingShooterGame.GameObjectManager.CreateProjectile(ProjectileType.Bullet, position);
                            bulletFired.Play();
                            defaultGunTimer = 0f;
                        }
                        else if (fireballTimer > 0.4f & (PowerupType & PowerupType.Fireball) > 0)
                        {
                            TriggerFireball();
                            bulletFired.Play();
                            fireballTimer = 0f;
                        }

                        if ((PowerupType & PowerupType.HomingMissiles) > 0)
                        {
                            if (homingMissileTimer <= 0)
                            {
                                homingMissileTimer = homingMissileFireRate;
                                TriggerHomingMissile();
                                rocketFired.Play();
                            }
                        }

                        //Conditionals to fire railgun.
                        if ((PowerupType & PowerupType.Railgun) > 0)
                        {
                            if (railgunTimer > 3.0f)
                            {
                                TriggerRailgun();
                                railgunTimer = 0f;
                            }
                        }

                        // Energy Blast Gun
                        if (((PowerupType & PowerupType.EnergyBlast) > 0) && energyBlastTimer < 0)
                        {
                            TriggerEnergyBlast();
                            laserFired.Play();
                        }

                        // Fire-once weapons
                        if (oldKeyboardState.IsKeyUp(Keys.Space))
                        {

                            if ((PowerupType & PowerupType.DroneWave) > 0)
                            {
                                TriggerDroneWave();
                            }
                        }

                        if ((PowerupType & PowerupType.Frostball) > 0)
                            TriggerFrostball();

                        if ((PowerupType & PowerupType.Birdcrap) > 0)
                        {
                            TriggerBirdcrap();
                        }

                        if ((PowerupType & PowerupType.Bomb) > 0)
                            TriggerBomb();
                    }
                }

                // store the current keyboard state for next frame
                oldKeyboardState = currentKeyboardState;
            }
        }