Advertisement
diliupg

GameProject Project Increment2 Burger.cs

Sep 21st, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11.  
  12. namespace GameProject
  13. {
  14.     /// <summary>
  15.     /// A burger
  16.     /// </summary>
  17.     public class Burger
  18.     {
  19.         #region Fields
  20.  
  21.         // graphic and drawing info
  22.         Texture2D sprite;
  23.         Rectangle drawRectangle;
  24.  
  25.         // burger stats
  26.         int health = 100;
  27.  
  28.         // shooting support
  29.         bool canShoot = true;
  30.         int elapsedCooldownMilliseconds = 0;
  31.  
  32.         // sound effect
  33.         SoundEffect shootSound;
  34.  
  35.         #endregion
  36.  
  37.         #region Constructors
  38.  
  39.         /// <summary>
  40.         ///  Constructs a burger
  41.         /// </summary>
  42.         /// <param name="contentManager">the content manager for loading content</param>
  43.         /// <param name="spriteName">the sprite name</param>
  44.         /// <param name="x">the x location of the center of the burger</param>
  45.         /// <param name="y">the y location of the center of the burger</param>
  46.         /// <param name="shootSound">the sound the burger plays when shooting</param>
  47.         public Burger(ContentManager contentManager, string spriteName, int x, int y,
  48.             SoundEffect shootSound)
  49.         {
  50.             LoadContent(contentManager, spriteName, x, y);
  51.             this.shootSound = shootSound;
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         #region Properties
  57.  
  58.         /// <summary>
  59.         /// Gets the collision rectangle for the burger
  60.         /// </summary>
  61.         public Rectangle CollisionRectangle
  62.         {
  63.             get { return drawRectangle; }
  64.         }
  65.  
  66.         #endregion
  67.  
  68.         #region Public methods
  69.  
  70.         /// <summary>
  71.         /// Updates the burger's location based on mouse. Also fires
  72.         /// french fries as appropriate
  73.         /// </summary>
  74.         /// <param name="gameTime">game time</param>
  75.         /// <param name="mouse">the current state of the mouse</param>
  76.         public void Update(GameTime gameTime, MouseState mouse)
  77.         {
  78.             // burger should only respond to input if it still has health
  79.             if (health > 0)
  80.             {
  81.                 // move burger using mouse
  82.                 drawRectangle.X = mouse.X - drawRectangle.Width / 2;
  83.                 drawRectangle.Y = mouse.Y - drawRectangle.Height / 2;
  84.             }
  85.             // clamp burger in window
  86.             if (drawRectangle.Y < 0)
  87.             {
  88.                 // bounce off top
  89.                 drawRectangle.Y = 0;
  90.             }
  91.             else if ((drawRectangle.Y + drawRectangle.Height) > GameConstants.WindowHeight)
  92.             {
  93.                 // bounce off bottom
  94.                 drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height;
  95.             }
  96.             if (drawRectangle.X < 0)
  97.             {
  98.                 // bounc off left
  99.                 drawRectangle.X = 0;
  100.             }
  101.             else if ((drawRectangle.X + drawRectangle.Width) > GameConstants.WindowWidth)
  102.             {
  103.                 // bounce off right
  104.                 drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width;
  105.             }
  106.  
  107.             // update shooting allowed
  108.             if (canShoot == false)
  109.             {
  110.                 elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  111.             }
  112.             if (elapsedCooldownMilliseconds > GameConstants.BurgerTotalCooldownMilliseconds)
  113.             {
  114.                 canShoot = true;
  115.                 elapsedCooldownMilliseconds = 0;
  116.             }
  117.  
  118.             if (health > 0 && mouse.LeftButton == ButtonState.Pressed && canShoot == true)
  119.             {
  120.                 canShoot = false;
  121.  
  122.                 Projectile projectile = new Projectile(ProjectileType.FrenchFries,
  123.                     Game1.GetProjectileSprite(ProjectileType.FrenchFries),
  124.                     drawRectangle.X + drawRectangle.Width / 2,
  125.                     drawRectangle.Y - GameConstants.FrenchFriesProjectileOffset,
  126.                     GameConstants.FrenchFriesProjectileSpeed * Convert.ToSingle(Math.Sin(-90)));
  127.  
  128.                 Game1.AddProjectile(projectile);
  129.             }
  130.  
  131.             if (mouse.LeftButton == ButtonState.Released)
  132.             {
  133.                 canShoot = true;
  134.             }
  135.             // timer concept (for animations) introduced in Chapter 7
  136.  
  137.             // shoot if appropriate
  138.  
  139.         }
  140.  
  141.         /// <summary>
  142.         /// Draws the burger
  143.         /// </summary>
  144.         /// <param name="spriteBatch">the sprite batch to use</param>
  145.         public void Draw(SpriteBatch spriteBatch)
  146.         {
  147.             spriteBatch.Draw( sprite, drawRectangle, Color.White );
  148.         }
  149.  
  150.         #endregion
  151.  
  152.         #region Private methods
  153.  
  154.         /// <summary>
  155.         /// Loads the content for the burger
  156.         /// </summary>
  157.         /// <param name="contentManager">the content manager to use</param>
  158.         /// <param name="spriteName">the name of the sprite for the burger</param>
  159.         /// <param name="x">the x location of the center of the burger</param>
  160.         /// <param name="y">the y location of the center of the burger</param>
  161.         private void LoadContent(ContentManager contentManager, string spriteName,
  162.             int x, int y)
  163.         {
  164.             // load content and set remainder of draw rectangle
  165.             sprite = contentManager.Load<Texture2D>(spriteName);
  166.             drawRectangle = new Rectangle(x - sprite.Width / 2,
  167.                 y - sprite.Height / 2, sprite.Width,
  168.                 sprite.Height);
  169.         }
  170.  
  171.         #endregion
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement