Advertisement
Okarin47

Rock.cs

Jul 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 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.Graphics;
  8.  
  9. namespace ProgrammingAssignment3
  10. {
  11. /// <summary>
  12. /// A rock
  13. /// </summary>
  14. public class Rock
  15. {
  16. #region Fields
  17.  
  18. // drawing support
  19. Texture2D sprite;
  20. Rectangle drawRectangle;
  21.  
  22. // moving support
  23. Vector2 velocity;
  24.  
  25. // window containment support
  26. int windowWidth;
  27. int windowHeight;
  28. bool outsideWindow = false;
  29.  
  30. #endregion
  31.  
  32. #region Constructors
  33.  
  34. /// <summary>
  35. /// Constructor
  36. /// </summary>
  37. /// <param name="sprite">sprite for the rock</param>
  38. /// <param name="location">location of the center of the rock</param>
  39. /// <param name="velocity">velocity of the rock</param>
  40. /// <param name="windowWidth">window width</param>
  41. /// <param name="windowHeight">window height</param>
  42. public Rock(Texture2D sprite, Vector2 location, Vector2 velocity,
  43. int windowWidth, int windowHeight)
  44. {
  45. // save window dimensions
  46. this.windowWidth = windowWidth;
  47. this.windowHeight = windowHeight;
  48.  
  49. // save sprite and set draw rectangle
  50. this.sprite = sprite;
  51. drawRectangle = new Rectangle((int)location.X - sprite.Width / 2,
  52. (int)location.Y - sprite.Height / 2, sprite.Width, sprite.Height);
  53.  
  54. // save velocity
  55. this.velocity = velocity;
  56. }
  57.  
  58. #endregion
  59.  
  60. #region Properties
  61.  
  62. /// <summary>
  63. /// Sets the rock's velocity
  64. /// </summary>
  65. public Vector2 Velocity
  66. {
  67. set
  68. {
  69. velocity.X = value.X;
  70. velocity.Y = value.Y;
  71. }
  72. }
  73.  
  74. /// <summary>
  75. /// Gets whether or not the rock is outside the window
  76. /// </summary>
  77. public bool OutsideWindow
  78. {
  79. get { return outsideWindow; }
  80. }
  81.  
  82. #endregion
  83.  
  84. #region Methods
  85.  
  86. /// <summary>
  87. /// Updates the rock
  88. /// </summary>
  89. /// <param name="gameTime">game time</param>
  90. public void Update(GameTime gameTime)
  91. {
  92. // STUDENTS: Only update the rock if it's inside the window
  93. if (((drawRectangle.Bottom > 0) && (drawRectangle.Top < windowHeight)) && ((drawRectangle.Right < windowWidth) && (drawRectangle.Left > 0)))
  94. // STUDENTS: Update the rock's location
  95. {
  96. drawRectangle.X += (int)velocity.X;
  97. drawRectangle.Y += (int)velocity.Y;
  98. }
  99. else
  100. {
  101. // STUDENTS: Set outsideWindow to true if the rock is outside the window
  102. outsideWindow = true;
  103. }
  104. }
  105.  
  106. /// <summary>
  107. /// Draws the rock
  108. /// </summary>
  109. /// <param name="spriteBatch">sprite batch</param>
  110. public void Draw(SpriteBatch spriteBatch)
  111. {
  112. // STUDENTS: Only draw the rock if it's inside the window
  113. if (outsideWindow == false)
  114. {
  115. spriteBatch.Draw(sprite, drawRectangle, Color.White);
  116. }
  117. // STUDENTS: Draw the rock
  118. // Caution: Don't include spriteBatch.Begin or spriteBatch.End here
  119.  
  120. }
  121.  
  122. #endregion
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement