Пример #1
0
 /// <summary>
 /// Add a new animated sprite
 /// </summary>
 public bool Add(AnimSprite a)
 {
     if (animatedSprites.Count < GameOptions.MaxSprites)
     {
         animatedSprites.AddLast(a);
         return(true);
     }
     return(false);
 }
Пример #2
0
        /// <summary>
        /// Updates the player ship for given elapsed time
        /// </summary>
        public void Update(
            float elapsedTime,          // elapsed time on this frame
            CollisionMesh collision,    // level collision mesh
            EntityList entities)        // level spawn points
        {
            if (collision == null)
            {
                throw new ArgumentNullException("collision");
            }
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }

            // updates damage screen time (zero for no damage indication)
            damageTime = Math.Max(0.0f, damageTime - elapsedTime);

            // if player dead
            if (IsAlive == false)
            {
                // disable engine particle system
                particleBoost.Enabled = false;

                // updates dead time (if zero, player is alive)
                deadTime = Math.Max(0.0f, deadTime - elapsedTime);

                // if player dead time expires, respawn
                if (IsAlive == true)
                {
                    // reset player to a random spawn point
                    Reset(entities.GetTransformRandom(random));

                    // add spawn animated sprite in front of player
                    Vector3 Pos = movement.position + 10 * movement.rotation.Forward;
                    gameManager.AddAnimSprite(AnimSpriteType.Spawn,
                                              Pos, 140, 80, 30, DrawMode.Additive, playerIndex);

                    // play spawn sound
                    gameManager.PlaySound("ship_spawn");

                    // reset energy, shield and boost
                    energy       = 1.0f;
                    shield       = 1.0f;
                    boost        = 1.0f;
                    missileCount = 3;
                }

                return;
            }

            // hold position before movement
            Vector3 lastPostion = movement.position;

            // update movement
            movement.Update(elapsedTime);

            // test for collision with level
            Vector3 collisionPosition;

            if (collision.BoxMove(box, lastPostion, movement.position,
                                  1.0f, 0.0f, 3, out collisionPosition))
            {
                // update to valid position after collision
                movement.position = collisionPosition;

                // compute new velocity after collision
                Vector3 newVelocity =
                    (collisionPosition - lastPostion) * (1.0f / elapsedTime);

                // if collision sound enabled
                if (collisionSound)
                {
                    // test collision angle to play collision sound
                    Vector3 WorldVel = movement.WorldVelocity;
                    float   dot      = Vector3.Dot(
                        Vector3.Normalize(WorldVel), Vector3.Normalize(newVelocity));
                    if (dot < 0.7071f)
                    {
                        // play collision sound
                        gameManager.PlaySound("ship_collide");

                        // set rumble intensity
                        dot = 1 - 0.5f * (dot + 1);
                        gameManager.SetVibration(playerIndex, dot * 0.5f);

                        // disable collision sounds until ship stops colliding
                        collisionSound = false;
                    }
                }

                // set new velocity after collision
                movement.WorldVelocity = newVelocity;
            }
            else
            {
                // clear of collisions, re-enable collision sounds
                collisionSound = true;
            }

            // update player transform
            transform             = movement.rotation;
            transform.Translation = movement.position;

            // compute inverse transform
            transformInverse = Matrix.Invert(transform);

            // get normalized player velocity
            float velocityFactor = movement.VelocityFactor;

            // update bobbing
            bobbingTime += elapsedTime;
            float bobbingFactor = 1.0f - velocityFactor;
            float time          = GameOptions.ShipBobbingSpeed * bobbingTime %
                                  (2 * MathHelper.TwoPi);
            float distance = bobbingFactor * GameOptions.ShipBobbingRange;

            bobbing.M41        = distance * (float)Math.Sin(time * 0.5f);
            bobbing.M42        = distance * (float)Math.Sin(time);
            bobbingInverse.M41 = -bobbing.M41;
            bobbingInverse.M42 = -bobbing.M42;

            // compute transform with bobbing
            Matrix bobbingTransform = bobbing * transform;

            // update particle system position
            particleBoost.Enabled = true;
            particleBoost.SetTransform(boostTransform * bobbingTransform);

            // if shield active
            if (shieldUse)
            {
                // update shield position
                animatedSpriteShield.Position = bobbingTransform.Translation +
                                                10f * bobbingTransform.Forward;

                // update shiled charge
                shield -= elapsedTime / GameOptions.ShieldUse;

                // if shield charge depleted
                if (shield < 0)
                {
                    // disable shield
                    shieldUse = false;
                    shield    = 0;

                    // kill shield animated sprite
                    animatedSpriteShield.SetTotalTime(0);
                    animatedSpriteShield = null;
                }
            }
            else
            {
                // change shield
                shield = Math.Min(1.0f,
                                  shield + elapsedTime / GameOptions.ShieldRecharge);
            }

            // if boost active
            if (boostUse)
            {
                // increase ship maximum velocity
                movement.maxVelocity = GameOptions.MovementVelocityBoost;
                // apply impulse force forward
                AddImpulseForce(transform.Forward * GameOptions.BoostForce);

                // set particle system velocity scale
                particleBoost.VelocityScale = Math.Min(1.0f,
                                                       particleBoost.VelocityScale + 4.0f * elapsedTime);

                // update shield charge
                boost -= elapsedTime / GameOptions.BoostUse;

                // if  boost depleated
                if (boost < 0)
                {
                    // disable boost
                    boostUse = false;
                    boost    = 0;
                }
            }
            else
            {
                // slowly returns ship maximum velocity to normal levels
                if (movement.maxVelocity > GameOptions.MovementVelocity)
                {
                    movement.maxVelocity -= GameOptions.BoostSlowdown * elapsedTime;
                }

                // slowly returns particle system velocity scale to normal levels
                particleBoost.VelocityScale = Math.Max(0.1f,
                                                       particleBoost.VelocityScale - 2.0f * elapsedTime);

                // charge boost
                boost = Math.Min(1.0f,
                                 boost + elapsedTime / GameOptions.BoostRecharge);
            }

            // charge blaster
            blaster = Math.Min(1.0f,
                               blaster + elapsedTime / GameOptions.BlasterChargeTime);

            // charge missile
            missile = Math.Min(1.0f,
                               missile + elapsedTime / GameOptions.MissileChargeTime);

            // update chase camera
            chaseCamera.ChasePosition  = transform.Translation;
            chaseCamera.ChaseDirection = transform.Forward;
            chaseCamera.Up             = transform.Up;
            chaseCamera.Update(elapsedTime, collision);
        }
Пример #3
0
        /// <summary>
        /// Process input for player ship (movement and weapons)
        /// </summary>
        public void ProcessInput(float elapsedTime, InputManager input, int player)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // if dead, don't process input for player
            if (IsAlive == false)
            {
                return;
            }

            // process movement related inputs
            movement.ProcessInput(elapsedTime, input.CurrentState, player);

            // if player invert Y is enabled, invert X rotation force
            if (gameManager.GetInvertY(player))
            {
                movement.rotationForce.X = -movement.rotationForce.X;
            }

            // if blaster ready and input activated
            if (blaster == 1)
            {
                if (input.IsKeyPressed(player, Keys.Space) ||
                    input.CurrentState.padState[player].Triggers.Right > 0)
                {
                    // fire blaster
                    FireProjectile(ProjectileType.Blaster, GameOptions.BlasterVelocity);
                    // reset charge time
                    blaster = 0;
                }
            }

            // if missile is ready and input activated
            if (missile == 1 && missileCount > 0)
            {
                if (input.IsKeyPressed(player, Keys.Enter) ||
                    input.IsTriggerPressedLeft(player))
                {
                    // fire missile
                    FireProjectile(ProjectileType.Missile, GameOptions.MissileVelocity);
                    // subtract missile count
                    AddMissile(-1);
                    // reset charge time
                    missile = 0;
                }
            }

            // if shield is ready and input
            if (shield == 1)
            {
                if (input.IsKeyPressed(player, Keys.R) || input.IsButtonPressedA(player))
                {
                    // activate shield
                    shieldUse = true;
                    // create animated sprite
                    animatedSpriteShield =
                        gameManager.AddAnimSprite(AnimSpriteType.Shield,
                                                  transform.Translation + transform.Forward * 10,
                                                  160, 80, 15, DrawMode.Additive, player);
                    // play shield sound
                    gameManager.PlaySound("shield_activate");
                }
            }

            // if boost ready and input activated
            if (boost == 1)
            {
                if (input.IsKeyPressed(player, Keys.LeftShift) ||
                    input.IsKeyPressed(player, Keys.RightShift) ||
                    input.IsButtonPressedY(player) ||
                    input.IsButtonPressedLeftStick(player))
                {
                    // activate boost
                    boostUse = true;
                    // play boost sound
                    gameManager.PlaySound("ship_boost");
                }
            }

            // if camara switch input activated
            if (input.IsKeyPressed(player, Keys.Back) || input.IsButtonPressedB(player))
            {
                // switch 3rd person mode
                camera3rdPerson = !camera3rdPerson;
                // reset camera
                chaseCamera.Reset();
            }
        }
Пример #4
0
 /// <summary>
 /// Add a new animated sprite
 /// </summary>
 public bool Add(AnimSprite a)
 {
     if (animatedSprites.Count < GameOptions.MaxSprites)
     {
         animatedSprites.AddLast(a);
         return true;
     }
     return false;
 }
Пример #5
0
        /// <summary>
        /// Create a new animated sprite and add it to the animated sprite manager
        /// </summary>
        public AnimSprite AddAnimSprite(
            AnimSpriteType type,
            Vector3 position,
            float radius,
            float viewOffset,
            float frameRate,
            DrawMode mode,
            int player)
        {
            // create animated sprite
            AnimSprite a = new AnimSprite(type, position, radius, viewOffset,
                animatedSpriteTextures[(int)type], 256, 256,
                frameRate, mode, player);

            // add it to the animated sprite manager
            animatedSprite.Add(a);

            return a;
        }
Пример #6
0
        // level spawn points
        /// <summary>
        /// Updates the player ship for given elapsed time
        /// </summary>
        public void Update(
            float elapsedTime,          // elapsed time on this frame
            CollisionMesh collision,    // level collision mesh
            EntityList entities)
        {
            if (collision == null)
            {
                throw new ArgumentNullException("collision");
            }
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }

            // updates damage screen time (zero for no damage indication)
            damageTime = Math.Max(0.0f, damageTime - elapsedTime);

            // if player dead
            if (IsAlive == false)
            {
                // disable engine particle system
                particleBoost.Enabled = false;

                // updates dead time (if zero, player is alive)
                deadTime = Math.Max(0.0f, deadTime - elapsedTime);

                // if player dead time expires, respawn
                if (IsAlive == true)
                {
                    // reset player to a random spawn point
                    Reset(entities.GetTransformRandom(random));

                    // add spawn animated sprite in front of player
                    Vector3 Pos = movement.position + 10 * movement.rotation.Forward;
                    gameManager.AddAnimSprite(AnimSpriteType.Spawn,
                                    Pos, 140, 80, 30, DrawMode.Additive, playerIndex);

                    // play spawn sound
                    gameManager.PlaySound("ship_spawn");

                    // reset energy, shield and boost
                    energy = 1.0f;
                    shield = 1.0f;
                    boost = 1.0f;
                    missileCount = 3;
                }

                return;
            }

            // hold position before movement
            Vector3 lastPostion = movement.position;

            // update movement
            movement.Update(elapsedTime);

            // test for collision with level
            Vector3 collisionPosition;
            if (collision.BoxMove(box, lastPostion, movement.position,
                                1.0f, 0.0f, 3, out collisionPosition))
            {
                // update to valid position after collision
                movement.position = collisionPosition;

                // compute new velocity after collision
                Vector3 newVelocity =
                    (collisionPosition - lastPostion) * (1.0f / elapsedTime);

                // if collision sound enabled
                if (collisionSound)
                {
                    // test collision angle to play collision sound
                    Vector3 WorldVel = movement.WorldVelocity;
                    float dot = Vector3.Dot(
                        Vector3.Normalize(WorldVel), Vector3.Normalize(newVelocity));
                    if (dot < 0.7071f)
                    {
                        // play collision sound
                        gameManager.PlaySound("ship_collide");

                        // set rumble intensity
                        dot = 1 - 0.5f * (dot + 1);
                        gameManager.SetVibration(playerIndex, dot * 0.5f);

                        // disable collision sounds until ship stops colliding
                        collisionSound = false;
                    }
                }

                // set new velocity after collision
                movement.WorldVelocity = newVelocity;
            }
            else
                // clear of collisions, re-enable collision sounds
                collisionSound = true;

            // update player transform
            transform = movement.rotation;
            transform.Translation = movement.position;

            // compute inverse transform
            transformInverse = Matrix.Invert(transform);

            // get normalized player velocity
            float velocityFactor = movement.VelocityFactor;

            // update bobbing
            bobbingTime += elapsedTime;
            float bobbingFactor = 1.0f - velocityFactor;
            float time = GameOptions.ShipBobbingSpeed * bobbingTime %
                (2 * MathHelper.TwoPi);
            float distance = bobbingFactor * GameOptions.ShipBobbingRange;
            bobbing.M41 = distance * (float)Math.Sin(time * 0.5f);
            bobbing.M42 = distance * (float)Math.Sin(time);
            bobbingInverse.M41 = -bobbing.M41;
            bobbingInverse.M42 = -bobbing.M42;

            // compute transform with bobbing
            Matrix bobbingTransform = bobbing * transform;

            // update particle system position
            particleBoost.Enabled = true;
            particleBoost.SetTransform(boostTransform * bobbingTransform);

            // if shield active
            if (shieldUse)
            {
                // update shield position
                animatedSpriteShield.Position = bobbingTransform.Translation +
                    10f * bobbingTransform.Forward;

                // update shiled charge
                shield -= elapsedTime / GameOptions.ShieldUse;

                // if shield charge depleted
                if (shield < 0)
                {
                    // disable shield
                    shieldUse = false;
                    shield = 0;

                    // kill shield animated sprite
                    animatedSpriteShield.SetTotalTime(0);
                    animatedSpriteShield = null;
                }
            }
            else
                // change shield
                shield = Math.Min(1.0f,
                            shield + elapsedTime / GameOptions.ShieldRecharge);

            // if boost active
            if (boostUse)
            {
                // increase ship maximum velocity
                movement.maxVelocity = GameOptions.MovementVelocityBoost;
                // apply impulse force forward
                AddImpulseForce(transform.Forward * GameOptions.BoostForce);

                // set particle system velocity scale
                particleBoost.VelocityScale = Math.Min(1.0f,
                    particleBoost.VelocityScale + 4.0f * elapsedTime);

                // update shield charge
                boost -= elapsedTime / GameOptions.BoostUse;

                // if  boost depleated
                if (boost < 0)
                {
                    // disable boost
                    boostUse = false;
                    boost = 0;
                }
            }
            else
            {
                // slowly returns ship maximum velocity to normal levels
                if (movement.maxVelocity > GameOptions.MovementVelocity)
                    movement.maxVelocity -= GameOptions.BoostSlowdown * elapsedTime;

                // slowly returns particle system velocity scale to normal levels
                particleBoost.VelocityScale = Math.Max(0.1f,
                    particleBoost.VelocityScale - 2.0f * elapsedTime);

                // charge boost
                boost = Math.Min(1.0f,
                            boost + elapsedTime / GameOptions.BoostRecharge);
            }

            // charge blaster
            blaster = Math.Min(1.0f,
                            blaster + elapsedTime / GameOptions.BlasterChargeTime);

            // charge missile
            missile = Math.Min(1.0f,
                            missile + elapsedTime / GameOptions.MissileChargeTime);

            // update chase camera
            chaseCamera.ChasePosition = transform.Translation;
            chaseCamera.ChaseDirection = transform.Forward;
            chaseCamera.Up = transform.Up;
            chaseCamera.Update(elapsedTime, collision);
        }
Пример #7
0
        /// <summary>
        /// Process input for player ship (movement and weapons)
        /// </summary>
        public void ProcessInput(float elapsedTime, InputManager input, int player)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            // if dead, don't process input for player
            if (IsAlive == false)
            {
                return;
            }

            // process movement related inputs
            movement.ProcessInput(elapsedTime, input.CurrentState, player);

            // if player invert Y is enabled, invert X rotation force
            if (gameManager.GetInvertY(player))
                movement.rotationForce.X = -movement.rotationForce.X;

            // if blaster ready and input activated
            if (blaster == 1)
                if (input.IsKeyPressed(player, Keys.Space) ||
                    input.CurrentState.padState[player].Triggers.Right > 0)
                {
                    // fire blaster
                    FireProjectile(ProjectileType.Blaster, GameOptions.BlasterVelocity);
                    // reset charge time
                    blaster = 0;
                }

            // if missile is ready and input activated
            if (missile == 1 && missileCount > 0)
                if (input.IsKeyPressed(player, Keys.Enter) ||
                    input.IsTriggerPressedLeft(player))
                {
                    // fire missile
                    FireProjectile(ProjectileType.Missile, GameOptions.MissileVelocity);
                    // subtract missile count
                    AddMissile(-1);
                    // reset charge time
                    missile = 0;
                }

            // if shield is ready and input
            if (shield == 1)
                if (input.IsKeyPressed(player,Keys.R) || input.IsButtonPressedA(player))
                {
                    // activate shield
                    shieldUse = true;
                    // create animated sprite
                    animatedSpriteShield =
                        gameManager.AddAnimSprite(AnimSpriteType.Shield,
                        transform.Translation + transform.Forward * 10,
                        160, 80, 15, DrawMode.Additive, player);
                    // play shield sound
                    gameManager.PlaySound("shield_activate");
                }

            // if boost ready and input activated
            if (boost == 1)
                if (input.IsKeyPressed(player, Keys.LeftShift) ||
                    input.IsKeyPressed(player, Keys.RightShift) ||
                    input.IsButtonPressedY(player) ||
                    input.IsButtonPressedLeftStick(player))
                {
                    // activate boost
                    boostUse = true;
                    // play boost sound
                    gameManager.PlaySound("ship_boost");
                }

            // if camara switch input activated
            if (input.IsKeyPressed(player, Keys.Back) || input.IsButtonPressedB(player))
            {
                // switch 3rd person mode
                camera3rdPerson = !camera3rdPerson;
                // reset camera
                chaseCamera.Reset();
            }
        }