/// <summary> /// Returns an instance of a usable alien bullet. Prefers reusing an existing (dead) /// bullet over creating a new instance. /// </summary> /// <returns>A bullet ready to place into the world.</returns> Bullet CreateAlienBullet() { Bullet b = null; for (int i = 0; i < alienBullets.Count; ++i) { if (alienBullets[i].IsAlive == false) { b = alienBullets[i]; break; } } if (b == null) { b = new Bullet(); alienBullets.Add(b); } b.IsAlive = true; return b; }
/// <summary> /// Returns an instance of a usable player bullet. Prefers reusing an existing (dead) /// bullet over creating a new instance. /// </summary> /// <returns>A bullet ready to place into the world.</returns> Bullet CreatePlayerBullet() { Bullet b = null; for (int i = 0; i < playerBullets.Count; ++i) { if (playerBullets[i].IsAlive == false) { b = playerBullets[i]; break; } } if (b == null) { b = new Bullet(); playerBullets.Add(b); } b.IsAlive = true; return b; }
public void Update(GameTime gameTime, GraphicsDeviceManager graphics) { if (!alive) { if (gameTime.TotalGameTime.CompareTo(deadTime + respawnTime) == 1) { alive = true; } } else { Vector2 leftStick = GamePad.GetState(playerIndex).ThumbSticks.Left; Vector2 rightStick = GamePad.GetState(playerIndex).ThumbSticks.Right; position += new Vector2(leftStick.X * 5, -leftStick.Y * 5); if (rightStick != new Vector2(0,0)) { if (lastShot == null || gameTime.TotalGameTime.CompareTo(lastShot + reload) == 1) { Bullet b = new Bullet(graphics, position.X, position.Y, this, spriteBatch, rightStick); lastShot = gameTime.TotalGameTime; bullets.Add(b); shotFX.Play(); } } } if ((position.Y + size.Y) > graphics.PreferredBackBufferHeight) { position.Y = (graphics.PreferredBackBufferHeight - size.Y); } else if (position.Y < 0) { position.Y = 0; } if ((position.X + size.X) > graphics.PreferredBackBufferWidth) { position.X = (graphics.PreferredBackBufferWidth - size.X); } else if (position.X < 0) { position.X = 0; } foreach (Bullet b in bullets) { if (b.GetX > graphics.PreferredBackBufferWidth || b.GetX < 0 || b.GetY > graphics.PreferredBackBufferHeight || b.GetY < 0) { bullets.Remove(b); Hit2FX.Play(); break; } } foreach (Bullet b in bullets) { b.Update(gameTime); } }