using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace GameProject { /// /// A burger /// public class Burger { #region Fields // graphic and drawing info Texture2D sprite; Rectangle drawRectangle; // burger stats int health = 100; // shooting support bool canShoot = true; int elapsedCooldownMilliseconds = 0; // sound effect SoundEffect shootSound; #endregion #region Constructors /// /// Constructs a burger /// /// the content manager for loading content /// the sprite name /// the x location of the center of the burger /// the y location of the center of the burger /// the sound the burger plays when shooting public Burger(ContentManager contentManager, string spriteName, int x, int y, SoundEffect shootSound) { LoadContent(contentManager, spriteName, x, y); this.shootSound = shootSound; } #endregion #region Properties /// /// Gets the collision rectangle for the burger /// public Rectangle CollisionRectangle { get { return drawRectangle; } } #endregion #region Public methods /// /// Updates the burger's location based on mouse. Also fires /// french fries as appropriate /// /// game time /// the current state of the mouse public void Update(GameTime gameTime, MouseState mouse) { // burger should only respond to input if it still has health if (health > 0) { // move burger using mouse drawRectangle.X = mouse.X - drawRectangle.Width / 2; drawRectangle.Y = mouse.Y - drawRectangle.Height / 2; } // clamp burger in window if (drawRectangle.Y < 0) { // bounce off top drawRectangle.Y = 0; } else if ((drawRectangle.Y + drawRectangle.Height) > GameConstants.WindowHeight) { // bounce off bottom drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height; } if (drawRectangle.X < 0) { // bounc off left drawRectangle.X = 0; } else if ((drawRectangle.X + drawRectangle.Width) > GameConstants.WindowWidth) { // bounce off right drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width; } // update shooting allowed if (canShoot == false) { elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds; } if (elapsedCooldownMilliseconds > GameConstants.BurgerTotalCooldownMilliseconds) { canShoot = true; elapsedCooldownMilliseconds = 0; } if (health > 0 && mouse.LeftButton == ButtonState.Pressed && canShoot == true) { canShoot = false; Projectile projectile = new Projectile(ProjectileType.FrenchFries, Game1.GetProjectileSprite(ProjectileType.FrenchFries), drawRectangle.X + drawRectangle.Width / 2, drawRectangle.Y - GameConstants.FrenchFriesProjectileOffset, GameConstants.FrenchFriesProjectileSpeed * Convert.ToSingle(Math.Sin(-90))); Game1.AddProjectile(projectile); } if (mouse.LeftButton == ButtonState.Released) { canShoot = true; } // timer concept (for animations) introduced in Chapter 7 // shoot if appropriate } /// /// Draws the burger /// /// the sprite batch to use public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw( sprite, drawRectangle, Color.White ); } #endregion #region Private methods /// /// Loads the content for the burger /// /// the content manager to use /// the name of the sprite for the burger /// the x location of the center of the burger /// the y location of the center of the burger private void LoadContent(ContentManager contentManager, string spriteName, int x, int y) { // load content and set remainder of draw rectangle sprite = contentManager.Load(spriteName); drawRectangle = new Rectangle(x - sprite.Width / 2, y - sprite.Height / 2, sprite.Width, sprite.Height); } #endregion } }