Ejemplo n.º 1
0
        public void Update(TimeSpan GameTime, Input input, List<Entity> entityList)
        {
            #region Player Death
            if (!Alive)
            {
                if (canRespawn == false)
                {
                    //Player fadeout animation
                    DeathDeltaTime = GameTime.TotalMilliseconds - DeathStartTimer;

                    //stops the DeathDeltaTimer from going over 1800, to stop errors with percentage
                    if (DeathDeltaTime > 1800) { DeathDeltaTime = 1800; }

                    //Calculate a percentage oh time till the death animation stops so that we can color the line accordingly
                    double percentage = (1800 - DeathDeltaTime) / 1800;

                    //Sets the color of the player depending on the delta time
                    Model.ModelColor = Color.FromArgb(255, (int)(percentage * 255), (int)(percentage * 255), (int)(percentage * 255));

                    if (DeathDeltaTime >= 1800)
                    {
                        canRespawn = true;
                    }

                    //Stops the flame from being drawn when the player is dead;
                    DrawFlame = false;
                }
            }
            #endregion

            #region Player Movement
            //Moving and rotating the player depending of different input
            if (Alive)
            {
                if (input.IsKeyDown(Keys.Right))
                {
                    AddToAngle(PlayerTurnSpeed);
                }
                if (input.IsKeyDown(Keys.Left))
                {
                    AddToAngle(-PlayerTurnSpeed);
                }
                if (input.IsKeyDown(Keys.Up))
                {
                    Velocity = Direction * PlayerSpeed;
                }

                //Displaying the flickering flame if the player is moving and
                if (input.IsKeyDown(Keys.Up))
                {
                    if (GameTime.TotalMilliseconds > (FlameStartTimer + PlayerFlameInterval))
                    {
                        FlameStartTimer = GameTime.TotalMilliseconds;
                        DrawFlame = !DrawFlame;
                    }
                }

                if (input.IsKeyUp(Keys.Up))
                {
                    DrawFlame = false;
                    FlameStartTimer = 0;
                }
            }
            #endregion

            #region Player Shooting
            if (input.IsKeyDown(Keys.Space) && Alive)
            {
                if (GameTime.TotalMilliseconds > (BulletStartTimer + PlayerBulletInterval))
                {
                    //Fire a new bullet and reset the start time

                    //add a rotated vector of -25 in the Y direction to have the bullet shoot from the nose of the ship
                    Bullet bullet = new Bullet(Game, Position + new Vector2(0, -25).Rotate(Angle), Direction, GameTime.TotalMilliseconds);
                    bulletList.Add(bullet);

                    //reset the bullet timer to stop the ship from firing a constant stream of bullets
                    BulletStartTimer = GameTime.TotalMilliseconds;
                }
            }

            //Update every bullet in the list
            foreach (Bullet bullet in bulletList)
            {
                bullet.Update(GameTime, input);
            }
            #endregion

            #region Asteroids and Bullet Collision
            foreach (Bullet bullet in bulletList)
            {
                if (bullet.Alive)
                {
                    foreach (Entity entity in entityList.ToList())
                    {
                        if (entity is Asteroid)
                        {
                            Asteroid asteroid = (Asteroid)entity;
                            if (asteroid.Alive)
                            {
                                if (Hitbox.Collision(bullet.BulletHitbox, asteroid.AsteroidHitbox))
                                {
                                    asteroid.BulletHitPosition = bullet.Position;
                                    asteroid.Kill();
                                    bullet.Kill();

                                    if (AsteroidKill != null) { AsteroidKill(asteroid.Size); }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            #region Player and Asteroid Collision
            if (Alive && !IsImmortal)
            {
                foreach (Entity entity in entityList.ToList())
                {
                    if (entity is Asteroid)
                    {
                        Asteroid asteroid = (Asteroid)entity;
                        if (asteroid.Alive)
                        {
                            if (Hitbox.Collision(asteroid.AsteroidHitbox, PlayerHitbox))
                            {
                                //kill the asteroid that collided with the player (causing it to split)
                                asteroid.Kill();

                                //call the on plater death event
                                if (Death != null) { Death(); }

                                //call the asteroid killed event
                                if (AsteroidKill != null) { AsteroidKill(asteroid.Size); }

                                //Set alive to false and set the death animation start timer
                                Alive = false;
                                DeathStartTimer = GameTime.TotalMilliseconds;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Immortality
            if (IsImmortal)
            {
                double TimePassed = GameTime.TotalMilliseconds - ImmortalityStartTime;

                if (TimePassed > ImmortalityTime) //stop the immortality
                {
                    Hide = false;
                    IsImmortal = false;
                }
                else
                {
                    int ModResult = ((int)TimePassed) / ImmortalityFlashInterval;
                    Hide = MathUtil.IsEven(ModResult);
                }
            }

            #endregion

            //Move the player by its velocity
            base.Update(GameTime, input);

            //Lower the velocity to add the effect of friction to the player
            //If the keys are still being pressed, then this will be discounted
            //because the velocity is reset
            Velocity = Velocity - (Velocity * PlayerFrictionFactor);
        }