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); } }); }
public static void Update(GameTime gameTime) { switch (weather) { // If it's storming, there will be wind and lightning // -------------------------------------------------- case Weather.STORM: { // Add seconds to the lightning timer elapsedLightningTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds; // Add 5 rain drops per update // --------------------------- for (int i = 0; i < 5; i++) { rain.Add(new RainDrop(new Vector2(rand.Next(Game1.GAME_WIDTH) * 1.2f, -4), 0, rainTex)); } // Update each rain drop and destroy it if need be // ----------------------------------------------- for (int i = rain.Count - 1; i >= 0; i--) { rain[i].Update(gameTime); if (rain[i].DestroyMe) { rain.RemoveAt(i); } ApplyWind(rain[i]); } // If the lightning should strike, then strike dammit // -------------------------------------------------- if (elapsedLightningTime >= secondsToStrike) { // The start location of the lightning bolt var start = new Vector2(rand.Next(Game1.GAME_WIDTH), 0); // A strike will occur again in 2 - 20 seconds secondsToStrike = rand.Next(2000, 20000); // There's a 40 percent chance that the nearest // player will get struck by the lightning bolt // -------------------------------------------- if (rand.NextDouble() < .4) { // Find which player is closest to the lightning bolt. // It is electricity after all. Player p = GetNearestPlayer(start); // The end position of the lightning bolt should be on the player var end = p.Position; // Add a new lightning bolt there bolts.Add(new LightningBolt(start, end)); // Damage the player 250 points p.Damage(250); // Add a white and blue explosion EffectManager.AddExplosion(end, Vector2.Zero, 15, 20, 3, 5, 30f, 60, Color.White, Color.LightBlue * 0f); } // If the lightning bolt is not destined to strike the player // ---------------------------------------------------------- else { // The lightning bolt's end point will be a random point that won't hurt anyone var end = new Vector2(rand.Next(Game1.GAME_WIDTH), rand.Next(Game1.GAME_HEIGHT) / 2); // Add the new lightning bolt bolts.Add(new LightningBolt(start, end)); } // Reset the time since strike to be 0 // ----------------------------------- elapsedLightningTime = 0; } // Update each lightning bolt... Feat. Lambda Expressionz // ------------------------------------------------------ bolts.ForEach(b => b.Update()); break; } // If it's rainy, there is only rain, nothing else // ----------------------------------------------- case Weather.RAIN: { // Add 5 rain drops per update // --------------------------- for (int i = 0; i < 5; i++) { rain.Add(new RainDrop(new Vector2(rand.Next(Game1.GAME_WIDTH) * 1.2f, -4), 0, rainTex)); } // Update each rain drop // --------------------- for (int i = rain.Count - 1; i >= 0; i--) { rain[i].Update(gameTime); if (rain[i].DestroyMe) { rain.RemoveAt(i); } } break; } case Weather.SUNNY: { break; } case Weather.NORMAL: { break; } } }