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 8.06 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.  
  11. namespace GameProject
  12. {
  13. /// <summary>
  14. /// A class for a teddy bear
  15. /// </summary>
  16. public class TeddyBear
  17. {
  18. #region Fields
  19.  
  20. bool active = true;
  21.  
  22. // drawing support
  23. Texture2D sprite;
  24. Rectangle drawRectangle;
  25.  
  26. // velocity information
  27. Vector2 velocity = new Vector2(0, 0);
  28.  
  29. // shooting support
  30. int elapsedShotMilliseconds = 0;
  31. int firingDelay;
  32.  
  33. // sound effects
  34. SoundEffect bounceSound;
  35. SoundEffect shootSound;
  36.  
  37. #endregion
  38.  
  39. #region Constructors
  40.  
  41. /// <summary>
  42. /// Constructs a teddy bear centered on the given x and y with the
  43. /// given velocity
  44. /// </summary>
  45. /// <param name="contentManager">the content manager for loading content</param>
  46. /// <param name="spriteName">the name of the sprite for the teddy bear</param>
  47. /// <param name="x">the x location of the center of the teddy bear</param>
  48. /// <param name="y">the y location of the center of the teddy bear</param>
  49. /// <param name="bounceSound">the sound the teddy bear plays when bouncing</param>
  50. /// <param name="shootSound">the sound the teddy bear plays when shooting</param>
  51. public TeddyBear(ContentManager contentManager, string spriteName, int x, int y,
  52. Vector2 velocity, SoundEffect bounceSound, SoundEffect shootSound)
  53. {
  54. LoadContent(contentManager, spriteName, x, y);
  55. this.velocity = velocity;
  56. this.bounceSound = bounceSound;
  57. this.shootSound = shootSound;
  58. firingDelay = GetRandomFiringDelay();
  59. }
  60.  
  61. #endregion
  62.  
  63. #region Properties
  64.  
  65. /// <summary>
  66. /// Gets and sets whether or not the teddy bear is active
  67. /// </summary>
  68. public bool Active
  69. {
  70. get { return active; }
  71. set { active = value; }
  72. }
  73.  
  74. /// <summary>
  75. /// Gets the location of the teddy bear
  76. /// </summary>
  77. public Point Location
  78. {
  79. get { return drawRectangle.Center; }
  80. }
  81.  
  82. /// <summary>
  83. /// Sets the x location of the center of the teddy bear
  84. /// </summary>
  85. public int X
  86. {
  87. set { drawRectangle.X = value - drawRectangle.Width / 2; }
  88. }
  89.  
  90. /// <summary>
  91. /// Sets the y location of the center of the teddy bear
  92. /// </summary>
  93. public int Y
  94. {
  95. set { drawRectangle.Y = value - drawRectangle.Height / 2; }
  96. }
  97.  
  98. /// <summary>
  99. /// Gets the collision rectangle for the teddy bear
  100. /// </summary>
  101. public Rectangle CollisionRectangle
  102. {
  103. get { return drawRectangle; }
  104. }
  105.  
  106. /// <summary>
  107. /// Gets and sets the velocity of the teddy bear
  108. /// </summary>
  109. public Vector2 Velocity
  110. {
  111. get { return velocity; }
  112. set { velocity = value; }
  113. }
  114.  
  115. /// <summary>
  116. /// Gets and sets the draw rectangle for the teddy bear
  117. /// </summary>
  118. public Rectangle DrawRectangle
  119. {
  120. get { return drawRectangle; }
  121. set { drawRectangle = value; }
  122. }
  123.  
  124. #endregion
  125.  
  126. #region Public methods
  127.  
  128. /// <summary>
  129. /// Updates the teddy bear's location, bouncing if necessary. Also has
  130. /// the teddy bear fire a projectile when it's time to
  131. /// </summary>
  132. /// <param name="gameTime">game time</param>
  133. public void Update(GameTime gameTime)
  134. {
  135. // move the teddy bear
  136. drawRectangle.X += (int)(velocity.X * gameTime.ElapsedGameTime.Milliseconds);
  137. drawRectangle.Y += (int)(velocity.Y * gameTime.ElapsedGameTime.Milliseconds);
  138.  
  139. // bounce as necessary
  140. BounceTopBottom();
  141. BounceLeftRight();
  142.  
  143. // fire projectile as appropriate
  144.  
  145. elapsedShotMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  146.  
  147. if (elapsedShotMilliseconds >= firingDelay)
  148. {
  149. elapsedShotMilliseconds = 0;
  150. firingDelay = GetRandomFiringDelay();
  151. Projectile projectile = new Projectile(ProjectileType.TeddyBear, Game1.GetProjectileSprite(ProjectileType.TeddyBear), DrawRectangle.X + (drawRectangle.Width / 2), drawRectangle.Bottom + GameConstants.FrenchFriesProjectileOffset, -GetProjectileYVelocity());
  152.  
  153. Game1.AddProjectile(projectile);
  154. }
  155.  
  156.  
  157.  
  158. // timer concept (for animations) introduced in Chapter 7
  159.  
  160. }
  161.  
  162. /// <summary>
  163. /// Draws the teddy bear
  164. /// </summary>
  165. /// <param name="spriteBatch">the sprite batch to use</param>
  166. public void Draw(SpriteBatch spriteBatch)
  167. {
  168. spriteBatch.Draw(sprite, drawRectangle, Color.White);
  169. }
  170.  
  171. #endregion
  172.  
  173. #region Private methods
  174.  
  175. /// <summary>
  176. /// Loads the content for the teddy bear
  177. /// </summary>
  178. /// <param name="contentManager">the content manager to use</param>
  179. /// <param name="spriteName">the name of the sprite for the teddy bear</param>
  180. /// <param name="x">the x location of the center of the teddy bear</param>
  181. /// <param name="y">the y location of the center of the teddy bear</param>
  182. private void LoadContent(ContentManager contentManager, string spriteName,
  183. int x, int y)
  184. {
  185. // load content and set remainder of draw rectangle
  186. sprite = contentManager.Load<Texture2D>(spriteName);
  187. drawRectangle = new Rectangle(x - sprite.Width / 2,
  188. y - sprite.Height / 2, sprite.Width,
  189. sprite.Height);
  190. }
  191.  
  192. /// <summary>
  193. /// Bounces the teddy bear off the top and bottom window borders if necessary
  194. /// </summary>
  195. private void BounceTopBottom()
  196. {
  197. if (drawRectangle.Y < 0)
  198. {
  199. // bounce off top
  200. drawRectangle.Y = 0;
  201. velocity.Y *= -1;
  202. }
  203. else if ((drawRectangle.Y + drawRectangle.Height) > GameConstants.WindowHeight)
  204. {
  205. // bounce off bottom
  206. drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height;
  207. velocity.Y *= -1;
  208. }
  209. }
  210. /// <summary>
  211. /// Bounces the teddy bear off the left and right window borders if necessary
  212. /// </summary>
  213. private void BounceLeftRight()
  214. {
  215. if (drawRectangle.X < 0)
  216. {
  217. // bounc off left
  218. drawRectangle.X = 0;
  219. velocity.X *= -1;
  220. }
  221. else if ((drawRectangle.X + drawRectangle.Width) > GameConstants.WindowWidth)
  222. {
  223. // bounce off right
  224. drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width;
  225. velocity.X *= -1;
  226. }
  227. }
  228.  
  229. /// <summary>
  230. /// Gets a random firing delay between MIN_FIRING_DELAY and
  231. /// MIN_FIRING_DELY + FIRING_RATE_RANGE
  232. /// </summary>
  233. /// <returns>the random firing delay</returns>
  234. private int GetRandomFiringDelay()
  235. {
  236. return GameConstants.BearMinFiringDelay +
  237. RandomNumberGenerator.Next(GameConstants.BearFiringRateRange);
  238. }
  239.  
  240. /// <summary>
  241. /// Gets the y velocity for the projectile being fired
  242. /// </summary>
  243. /// <returns>the projectile y velocity</returns>
  244. private float GetProjectileYVelocity()
  245. {
  246. if (velocity.Y > 0)
  247. {
  248. return velocity.Y + GameConstants.TeddyBearProjectileSpeed;
  249. }
  250. else
  251. {
  252. return GameConstants.TeddyBearProjectileSpeed;
  253. }
  254. }
  255.  
  256. #endregion
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement