Marble.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Marble.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace Marblets
  15. {
  16. /// <summary>
  17. /// Marble represents a single marble
  18. /// </summary>
  19. public class Marble
  20. {
  21. /// <summary>
  22. /// Width of a marble in HD resolution screen coordinates
  23. /// </summary>
  24. public const int Width = 45;
  25. /// <summary>
  26. /// Height of a marble in HD resolution screen coordinates
  27. /// </summary>
  28. public const int Height = 45;
  29. /// <summary>
  30. /// How long it takes a marble to break
  31. /// </summary>
  32. public const double BreakTime = 0.5;
  33. /// <summary>
  34. /// Animation fields
  35. /// </summary>
  36. private const int breakFrameCount = 12;
  37. private static float pulseFactor;
  38. private double animationStart;
  39. private int frame;
  40. private Animation animation = Animation.None;
  41. /// <summary>
  42. /// Initialize the random number generator
  43. /// </summary>
  44. private static Random random = new Random();
  45. /// <summary>
  46. /// Marble textures
  47. /// </summary>
  48. private static Texture2D breakTexture;
  49. private static Texture2D glowRing1Texture;
  50. private static Texture2D glowRing2Texture;
  51. private static Texture2D marble;
  52. private Color color;
  53. /// <summary>
  54. /// Marble position
  55. /// </summary>
  56. private Vector2 position2D;
  57. /// <summary>
  58. /// Current state of the animation
  59. /// </summary>
  60. public Animation Animation
  61. {
  62. get
  63. {
  64. return animation;
  65. }
  66. }
  67. /// <summary>
  68. /// Color of the marble
  69. /// </summary>
  70. public Color Color
  71. {
  72. get
  73. {
  74. return color;
  75. }
  76. }
  77. /// <summary>
  78. /// Is this marble currently selected
  79. /// </summary>
  80. public bool Selected;
  81. /// <summary>
  82. /// The HD screen based coordinates of the marble
  83. /// </summary>
  84. public Vector2 Position
  85. {
  86. get
  87. {
  88. return position2D;
  89. }
  90. set
  91. {
  92. position2D = value;
  93. }
  94. }
  95. /// <summary>
  96. /// Constructs a new marble
  97. /// </summary>
  98. public Marble()
  99. {
  100. //Set a random color
  101. color = MarbletsGame.Settings.MarbleColors[
  102. random.Next(MarbletsGame.Settings.MarbleColors.GetLength(0))];
  103. }
  104. /// <summary>
  105. /// Initializes the marble
  106. /// </summary>
  107. /// <param name="game">The game object</param>
  108. public static void Initialize()
  109. {
  110. }
  111. /// <summary>
  112. /// Updates the animation for marbles
  113. /// </summary>
  114. /// <param name="time">Total game time</param>
  115. public void Update(GameTime time)
  116. {
  117. if(time != null)
  118. {
  119. if(Animation == Animation.Breaking)
  120. {
  121. if((time.TotalGameTime.TotalSeconds - animationStart) > BreakTime)
  122. {
  123. animation = Animation.Gone;
  124. frame = breakFrameCount;
  125. }
  126. else
  127. {
  128. frame = (int)((time.TotalGameTime.TotalSeconds - animationStart)
  129. / BreakTime * breakFrameCount);
  130. }
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Updates the static parts of the class - only needs to be done once rather
  136. /// than for 144 marbles each frame
  137. /// </summary>
  138. /// <param name="time">Total game time</param>
  139. public static void UpdateStatic(GameTime time)
  140. {
  141. if(time != null)
  142. pulseFactor = (float)Math.Sin(time.TotalGameTime.TotalSeconds * 6.0);
  143. }
  144. /// <summary>
  145. /// Draws the specified marble
  146. /// </summary>
  147. /// <param name="spriteBatch">The sprite batch to use</param>
  148. public void Draw(RelativeSpriteBatch spriteBatch)
  149. {
  150. Draw2DMarble(spriteBatch);
  151. }
  152. private void Draw2DMarble(RelativeSpriteBatch spriteBatch)
  153. {
  154. //Draw the 2d marble
  155. if(Animation == Animation.Breaking || Animation == Animation.Gone)
  156. {
  157. //Draw the correct frame.
  158. spriteBatch.Draw(breakTexture, Position, new Rectangle(frame * Width, 0,
  159. Width, Height), Color);
  160. }
  161. else
  162. {
  163. spriteBatch.Draw(marble, Position, Color);
  164. //Highlight selected marbles
  165. if(Selected)
  166. {
  167. if(pulseFactor < 0.0)
  168. {
  169. //Make a new color with the correct transparency.
  170. Color fade = Color.White * -pulseFactor;
  171. //pulse the single ring
  172. spriteBatch.Draw(glowRing1Texture, Position, fade);
  173. }
  174. else
  175. {
  176. //pulse the double ring
  177. //Make a new color with the correct transparency.
  178. Color fade = Color.White * pulseFactor;
  179. spriteBatch.Draw(glowRing2Texture, Position, fade);
  180. }
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// Signal a marble to start breaking
  186. /// </summary>
  187. /// <param name="time">Start time for the animation</param>
  188. public void Break(GameTime time)
  189. {
  190. if(time != null)
  191. {
  192. animationStart = time.TotalGameTime.TotalSeconds;
  193. animation = Animation.Breaking;
  194. }
  195. }
  196. /// <summary>
  197. /// Load graphics content.
  198. /// </summary>
  199. public static void LoadContent()
  200. {
  201. breakTexture =
  202. MarbletsGame.Content.Load<Texture2D>("Textures/marble_burst");
  203. glowRing1Texture =
  204. MarbletsGame.Content.Load<Texture2D>("Textures/marble_glow_1ring");
  205. glowRing2Texture =
  206. MarbletsGame.Content.Load<Texture2D>("Textures/marble_glow_2rings");
  207. marble = MarbletsGame.Content.Load<Texture2D>("Textures/marble");
  208. }
  209. }
  210. }