public void ResetMeteors() { while (meteorList.Count < 10) { var angle = rnd.Next() * MathHelper.TwoPi; var m = new Meteor(MeteorType.Big) { Position = new Vector2(Globals.GameArea.Left + (float)rnd.NextDouble() * Globals.GameArea.Width, Globals.GameArea.Top + (float)rnd.NextDouble() * Globals.GameArea.Height), Rotation = angle, Speed = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * rnd.Next(20, 60) / 30.0f }; if (!Globals.RespawnArea.Contains(m.Position)) { meteorList.Add(m); } } }
public static IEnumerable <Meteor> BreakMeteor(Meteor meteor) { List <Meteor> meteorList = new List <Meteor>(); if (meteor.Type == MeteorType.Small) { return(meteorList); } for (int i = 0; i < 3; i++) { var angle = (float)Math.Atan2(meteor.Speed.Y, meteor.Speed.X) - MathHelper.PiOver4 + MathHelper.PiOver4 * i; meteorList.Add(new Meteor(meteor.Type + 1) { Position = meteor.Position, Rotation = angle, Speed = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * meteor.Speed.Length() }); } return(meteorList); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Up)) { player.Accelerate(); } if (state.IsKeyDown(Keys.Left)) { player.Rotation -= 0.05f; } if (state.IsKeyDown(Keys.Down)) { player.Break(); } else if (state.IsKeyDown(Keys.Right)) { player.Rotation += 0.05f; } if (state.IsKeyDown(Keys.Space)) { Shot s = player.Shoot(); if (s != null) { laserSound.Play(); shotList.Add(s); } } foreach (Shot shot in shotList) { shot.Update(gameTime); Meteor meteor = meteorList.FirstOrDefault(m => m.CollidesWith(shot)); if (meteor != null) { meteorList.Remove(meteor); meteorList.AddRange(Meteor.BreakMeteor(meteor)); explosionList.Add(new Explosion() { Position = meteor.Position, Scale = meteor.ExplosionScale }); shot.IsDead = true; explosionSound.Play(); } } foreach (Explosion explosion in explosionList) { explosion.Update(gameTime); } foreach (Meteor meteor in meteorList) { meteor.Update(gameTime); } shotList.RemoveAll(s => s.IsDead || !Globals.GameArea.Contains(s.Position)); explosionList.RemoveAll(e => e.IsDead); player.Update(gameTime); prevousKbState = state; // TODO: Add your update logic here base.Update(gameTime); }