// reset this bullet's properties public void Init() { // if needed, generate pixel-perfect data if (OpaqueData == null) { OpaqueData = PixelPerfectHelper.GetOpaqueData(this); } // assume this bullet didn't originate from a player Shooter = null; }
// actually draw the HUD public void Draw(SpriteBatch batch, Player player) { // top, left of the three health lights Vector2 locHealthHUD = player.HudPosition; // top, left of the three shield lights Vector2 locShieldHUD = player.HudPosition; locShieldHUD.X += 3 * 32; // top, left of the score text and "press start" prompt Vector2 locPressStart = player.HudPosition; locPressStart.X += 16; locPressStart.Y += 32; if (FlashState && !player.IsActive) { // alternate between score and prompt, // as long as player's game is over batch.Draw(Game1.Texture, locPressStart, m_RectPressStart, Color.White); } else { // if player is active, draw their score every time player.NumberSprite.Draw(batch, locPressStart); } // render the health and shield lights for (int i = 0; i < 3; i++) { // render the shield lights if (player.Shield > i && player.IsActive) { // active! draw green light batch.Draw(Game1.Texture, locShieldHUD, m_RectShield, Color.PaleGreen); } else { // inactive, draw dim light batch.Draw(Game1.Texture, locShieldHUD, m_RectShield, Color.LightSlateGray); } // render the health lights if (player.Health > i && player.IsActive) { // active! draw red light batch.Draw(Game1.Texture, locHealthHUD, m_RectHealth, Color.LightCoral); } else { // inactive, draw dim light batch.Draw(Game1.Texture, locHealthHUD, m_RectHealth, Color.LightSlateGray); } // move to the next light's location locShieldHUD.X += 32; locHealthHUD.X += 32; } }
// see if a player was hit by any enemy bullets protected static void CheckForHitPlayer(Player player) { // is this player active? if (player.IsActive) { // for each bullet for (int iBullet = 0; iBullet < m_Bullets.Length; iBullet++) { // is this bullet active and did it originate from an enemy? if (m_Bullets[iBullet].IsActive && !m_Bullets[iBullet].MoveUp) { // is the bullet touching the player? if (PixelPerfectHelper.DetectCollision( player, m_Bullets[iBullet])) { // register the hit with the player player.TakeHit(); // if the player was destroyed by the hit, // display a splat icon at their current // location if (!player.IsActive) { AddSplat(player); } // return this bullet to the pool of available // bullets m_Bullets[iBullet].IsActive = false; } } } // check for a collision between player and enemy aircraft for (int iEnemy = 0; iEnemy < m_Enemies.Length; iEnemy++) { // is this enemy active? if (m_Enemies[iEnemy].IsActive) { // is this enemy touching the player? if (PixelPerfectHelper.DetectCollision( player, m_Enemies[iEnemy])) { // register the hit with the player player.TakeHit(); // add a new splat icon for the enemy plane AddSplat(m_Enemies[iEnemy]); // if the player was destroyed, display a // splat icon at their location as well if (!player.IsActive) { AddSplat(player); } } } } } }
// see if the player is touching any bonus icons protected static void CheckForHitBonuses(Player player) { // is this player active? if (player.IsActive) { // for each bonus icon object for (int iBonus = 0; iBonus < m_Bonuses.Length; iBonus++) { // is this bonus active? if (m_Bonuses[iBonus].IsActive) { // is this bonus touching the player? if (PixelPerfectHelper.DetectCollision( player, m_Bonuses[iBonus])) { // take action based on bonus type switch (m_Bonuses[iBonus].BonusType) { case Bonus.Type.Health: // red // increase player's health player.Health += 1; break; case Bonus.Type.Score: // blue // add bonus points to player score player.Score += 25; break; case Bonus.Type.Shield: // green // increase player's shield player.Shield += 1; break; case Bonus.Type.Weapon: // yellow // reduce delay between shots player.FireDelay -= 0.2 * player.FireDelay; break; } // return this bonus to the pool of available // bonsuses m_Bonuses[iBonus].IsActive = false; } } } } }
// handle player input from the keyboard public void ProcessInput(double elapsed, Player player, KeyboardState key) { if (!player.IsActive && key.IsKeyDown(Keys.Enter)) { // start or join the game player.Init(); player.IsActive = true; } else if (player.IsActive) { // change in location Vector2 delta = Vector2.Zero; // moving left or right? if (key.IsKeyDown(Keys.Left)) { delta.X = (float)(-player.MovePixelsPerSecond * elapsed); } else if (key.IsKeyDown(Keys.Right)) { delta.X = (float)(player.MovePixelsPerSecond * elapsed); } // moving up or down? if (key.IsKeyDown(Keys.Up)) { delta.Y = (float)(-player.MovePixelsPerSecond * elapsed); } else if (key.IsKeyDown(Keys.Down)) { delta.Y = (float)(player.MovePixelsPerSecond * elapsed); } // actually move the player player.Location += delta; // if the player is pressing the action // button, try to fire a bullet if (key.IsKeyDown(Keys.Space) && player.Fire()) { GameObjectManager.AddBullet(player, true); } } }
// handle game pad input public void ProcessInput(double elapsed, Player player, GamePadState pad) { if (!player.IsActive && pad.Buttons.Start == ButtonState.Pressed) { // start or join the game player.Init(); player.IsActive = true; } else if (player.IsActive) { // change in location Vector2 delta = Vector2.Zero; // moving left or right? if (pad.ThumbSticks.Left.X < 0) { delta.X = (float)(-player.MovePixelsPerSecond * elapsed); } else if (pad.ThumbSticks.Left.X > 0) { delta.X = (float)(player.MovePixelsPerSecond * elapsed); } // moving up or down? if (pad.ThumbSticks.Left.Y > 0) { delta.Y = (float)(-player.MovePixelsPerSecond * elapsed); } else if (pad.ThumbSticks.Left.Y < 0) { delta.Y = (float)(player.MovePixelsPerSecond * elapsed); } // actually move the player player.Location += delta; // if the player is pressing the action // button, try to fire a bullet if (pad.Buttons.A == ButtonState.Pressed && player.Fire()) { GameObjectManager.AddBullet(player, true); } } }