Exemple #1
0
        /// <summary>
        /// Update projectile
        /// </summary>
        public bool Update(float elapsedTime, GameManager game)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            // add elapsed time for this frame
            this.elapsedTime += elapsedTime;

            // normalize time
            float normalizedTime = Math.Min(1.0f, this.elapsedTime / totalTime);

            // compute current projectile position
            Vector3 position = Vector3.Lerp(sourcePosition, destinationPosition,
                normalizedTime);

            // set postion into projectile transform matrix
            transform.Translation = position;

            // if projectile includes a particle system update its position
            if (system != null)
                system.SetTransform(systemTransform * transform);

            // check if projectile hit any player
            int playerHit = game.GetPlayerAtPosition(position);

            // if a player is hit or reached destination explode projectile
            if ((playerHit != player && playerHit != -1) ||
                normalizedTime == 1.0f)
            {
                // compute explosion position moving hit point into hit normal direction
                Vector3 explosionPosition =
                    position + 0.5f * animatedSpriteSize * transform.Backward;

                // set transform to explosion position
                transform.Translation = explosionPosition;

                // if an animated sprite explosion is available, create it
                if (animatedSpriteSize > 0)
                    game.AddAnimSprite(animatedSprite, explosionPosition,
                        animatedSpriteSize, 10.0f, animatedSpriteFrameRate,
                        animatedSpriteDrawMode, -1);

                // if splash damage is available, apply splash damage to nearby players
                if (explosionDamageRadius > 0)
                    game.AddDamageSplash(player, explosionDamage,
                        explosionPosition, explosionDamageRadius);

                // if exploded on a player add contact damage
                if (playerHit != -1 && game.GetPlayer(playerHit).IsAlive)
                    game.AddDamage(player, playerHit, contactDamage,
                        Vector3.Normalize(destinationPosition - sourcePosition));

                // if explosion sound is available, play it
                if (explosionSound != null)
                    game.PlaySound3D(explosionSound, explosionPosition);

                // add explosion particle system
                if (projectileType == ProjectileType.Missile)
                    game.AddParticleSystem(
                                    ParticleSystemType.MissileExplode,
                                    transform);
                else
                if (projectileType == ProjectileType.Blaster)
                    game.AddParticleSystem(
                                    ParticleSystemType.BlasterExplode,
                                    transform);

                // kill trail particle system
                if (system != null)
                    system.SetTotalTime(-1e10f);

                // return false to kill the projectile
                return false;
            }

            // return true to keep projectile alive
            return true;
        }
Exemple #2
0
        /// <summary>
        /// Update powerup for given elapsed time
        /// </summary>
        public bool Update(GameManager game, float elapsedTime)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            // add elapsed time for this frame
            this.elapsedTime += elapsedTime;

            // if waiting to respawn
            if (waitTime > 0)
            {
                // decrease wait time
                waitTime = Math.Max(0.0f, waitTime - elapsedTime);

                // if wait time is finished
                if (waitTime == 0)
                {
                    // add powerup spawn animated sprite
                    game.AddAnimSprite(AnimSpriteType.Spawn,
                        transform.Translation, 50, 40, 30, DrawMode.Additive, -1);

                    // play powerup spawn sound
                    game.PlaySound3D("powerup_spawn", transform.Translation);
                }

                // return true to keep powerup alive
                return true;
            }

            // calculate bobbing angles
            float turn_angle = (this.elapsedTime * GameOptions.PowerupTurnSpeed) %
                MathHelper.TwoPi;
            float move_angle = (this.elapsedTime * GameOptions.PowerupMoveSpeed) %
                MathHelper.TwoPi;

            // create bobbing matrix
            bobbing = Matrix.CreateRotationY(turn_angle) *
                Matrix.CreateTranslation((float)Math.Cos(move_angle) *
                        GameOptions.PowerupMoveDistance * Vector3.Up);

            // check for any player at the powerup location
            int playerHit = game.GetPlayerAtPosition(transform.Translation);
            if (playerHit != -1)
            {
                // disable powerup until respawn time
                waitTime = GameOptions.PowerupRespawnTime;

                // get player at powerup location
                PlayerShip p = game.GetPlayer(playerHit);
                switch( powerupType )
                {
                    case PowerupType.Energy:
                        p.AddEnergy(0.5f);        // add 50% energy
                        break;
                    case PowerupType.Missile:
                        p.AddMissile(3);        // add 3 missiles
                        break;
                }

                // add powerup spawn animates sprite
                game.AddAnimSprite(AnimSpriteType.Spawn,
                    transform.Translation, 40, 40, 30, DrawMode.Additive, -1);

                // play powerup get sound
                game.PlaySound("powerup_get");
            }

            // return true to keep powerup alive
            return true;
        }