Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 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. // move burger using mouse
  80. // clamp burger in window
  81.  
  82.  
  83. if (health > 0)
  84. {
  85.  
  86. drawRectangle.X = mouse.X - drawRectangle.Width / 2;
  87. drawRectangle.Y = mouse.Y - drawRectangle.Height / 2;
  88.  
  89. if (drawRectangle.Left < 0)
  90. {
  91. drawRectangle.X = 0;
  92. }
  93.  
  94. if (drawRectangle.Right > GameConstants.WindowWidth)
  95. {
  96. drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width;
  97. }
  98.  
  99. if (drawRectangle.Top < 0)
  100. {
  101. drawRectangle.Y = 0;
  102. }
  103.  
  104. if (drawRectangle.Bottom > GameConstants.WindowHeight)
  105. {
  106. drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height;
  107. }
  108.  
  109.  
  110. if (mouse.LeftButton == ButtonState.Pressed && canShoot)
  111. {
  112. Projectile projectile = new Projectile(ProjectileType.FrenchFries, Game1.GetProjectileSprite(ProjectileType.FrenchFries), drawRectangle.Center.X, drawRectangle.Center.Y - GameConstants.FrenchFriesProjectileOffset, GameConstants.FrenchFriesProjectileSpeed);
  113. Game1.AddProjectile(projectile);
  114. canShoot = false;
  115. }
  116.  
  117. if (!canShoot)
  118. {
  119. elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  120.  
  121. if (elapsedCooldownMilliseconds >= GameConstants.BurgerTotalCooldownMilliseconds || mouse.LeftButton == ButtonState.Released)
  122. {
  123. canShoot = true;
  124. elapsedCooldownMilliseconds = 0;
  125. }
  126. }
  127. }
  128.  
  129. // update shooting allowed
  130. // timer concept (for animations) introduced in Chapter 7
  131.  
  132. // shoot if appropriate
  133.  
  134. }
  135.  
  136. /// <summary>
  137. /// Draws the burger
  138. /// </summary>
  139. /// <param name="spriteBatch">the sprite batch to use</param>
  140. public void Draw(SpriteBatch spriteBatch)
  141. {
  142. spriteBatch.Draw(sprite, drawRectangle, Color.White);
  143. }
  144.  
  145. #endregion
  146.  
  147. #region Private methods
  148.  
  149. /// <summary>
  150. /// Loads the content for the burger
  151. /// </summary>
  152. /// <param name="contentManager">the content manager to use</param>
  153. /// <param name="spriteName">the name of the sprite for the burger</param>
  154. /// <param name="x">the x location of the center of the burger</param>
  155. /// <param name="y">the y location of the center of the burger</param>
  156. private void LoadContent(ContentManager contentManager, string spriteName,
  157. int x, int y)
  158. {
  159. // load content and set remainder of draw rectangle
  160. sprite = contentManager.Load<Texture2D>(spriteName);
  161. drawRectangle = new Rectangle(x - sprite.Width / 2,
  162. y - sprite.Height / 2, sprite.Width,
  163. sprite.Height);
  164. }
  165.  
  166. #endregion
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement