// take an inactive bonus icon from the pool and add it to the screen public static void AddBonus(GameSprite source) { // for each bonus in the pool for (int i = 0; i < m_Bonuses.Length; i++) { // is this bonus inactive? if (!m_Bonuses[i].IsActive) { // set the bonus' location, based on originator Vector2 pos = Vector2.Zero; pos.X = source.Location.X + source.TextureRect.Width / 2; pos.X -= m_Bonuses[i].TextureRect.Width / 2; pos.Y = source.Location.Y + source.TextureRect.Height / 2; pos.Y -= m_Bonuses[i].TextureRect.Height / 2; // add bonus icon to the screen m_Bonuses[i].Location = pos; m_Bonuses[i].Init(); m_Bonuses[i].IsActive = true; // we're done, exit the loop break; } } }
// take an inactive bullet from the pool and add it to the screen public static void AddBullet(GameSprite source, bool up) { // for each bullet for (int i = 0; i < m_Bullets.Length; i++) { // is this bullet inactive? if (!m_Bullets[i].IsActive) { // set bullet's direction m_Bullets[i].MoveUp = up; // set bullet's position, based on originator Vector2 pos = Vector2.Zero; pos.X = source.Location.X + source.TextureRect.Width / 2; pos.X -= m_Bullets[i].TextureRect.Width / 2; pos.Y = source.Location.Y + source.TextureRect.Height / 2; pos.Y -= m_Bullets[i].TextureRect.Height / 2; // add bullet to the screen m_Bullets[i].Init(); m_Bullets[i].Location = pos; m_Bullets[i].IsActive = true; // if this bullet came from a player, remember // who shot it so that they will get credit if it // hits an enemy if (source.GetType() == typeof(Player)) { m_Bullets[i].Shooter = (Player)source; } // we're done, exit the loop break; } } }
// this method provides a quick and dirty way to tell // when a game object has left the screen. when it does, // we can return it to the pool of available game objects public static void ValidateRoughBounds(GameSprite sprite) { if (sprite.Location.X < -32) // too far left { sprite.IsActive = false; } else if (sprite.Location.X > 640) // too far right { sprite.IsActive = false; } if (sprite.Location.Y < -32) // too far up { sprite.IsActive = false; } else if (sprite.Location.Y > 480) // too far down { sprite.IsActive = false; } }
// take an inactive splat icon from the pool and add it to the screen public static void AddSplat(GameSprite source) { // for each splat icon in the pool for (int i = 0; i < m_Splats.Length; i++) { // is this splat inactive? if (!m_Splats[i].IsActive) { // set the splat's location, based on originator Vector2 pos = Vector2.Zero; pos.X = source.Location.X + source.TextureRect.Width / 2; pos.X -= m_Splats[i].TextureRect.Width / 2; pos.Y = source.Location.Y + source.TextureRect.Height / 2; pos.Y -= m_Splats[i].TextureRect.Height / 2; // add splat icon to the screen m_Splats[i].Location = pos; m_Splats[i].Init(); m_Splats[i].IsActive = true; // return originator to the inactive pool source.IsActive = false; // we're done, exit the loop break; } } }