예제 #1
0
        public virtual void Update(GameTime gameTime)
        {
            // Decrease the cool down time because when coolDownTimeTicks == 0,
            // the gun is allowed to fire
            // ----------------------------------------------------------------
            if (coolDownTimeTicks > 0)
            {
                coolDownTimeTicks -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            // If the shoot key is pressed or the Xbox trigger is pulled
            // ---------------------------------------------------------
            if (InputHandler.KeyDown(shootKey) || InputHandler.ButtonDown(Buttons.RightTrigger, owner.PlayerIndex) ||
                owner.GetType() == typeof(ComputerPlayer))
            {
                // Only shoot if the gun is cooled down
                if (coolDownTimeTicks < 0)
                {
                    OnTrigger(owner, null);
                    coolDownTimeTicks = coolDownTime;
                    fired             = true;
                }
            }
            else
            {
                fired = false;
            }

            // Here's the bullets's foreach method with a lambda!
            // ---------------------------------------------------
            // Update each of the guns bullets and if the bullet's
            // destroyMe variable returns true, delete the bullet.
            // ---------------------------------------------------
            bullets.ForEach((b) =>
            {
                b.Update(gameTime);
                if (b.DestroyMe)
                {
                    // If the gun is a missile launcher, make an explosion on impact
                    if (this.GetType() == typeof(MissileLauncher))
                    {
                        SoundManager.Boom.Play();
                        EffectManager.AddExplosion(b.Position, Vector2.Zero, 15, 20, 4, 6, 40f, 50, new Color(1.0f, 0.3f, 0f, 0.5f), Color.Black * 0f);
                    }
                    // Otherwise, make sparks
                    else
                    {
                        SoundManager.Hit.Play();
                        EffectManager.AddSparksEffect(b.Position, new Vector2(400));
                    }
                }

                if (b.DestroyMe || b.RemoveMe)
                {
                    bullets.Remove(b);
                }
            });
        }