2
0

ScoreBar.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ScoreBar.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 HoneycombRush
  15. {
  16. /// <summary>
  17. /// Used by other components to display their status.
  18. /// </summary>
  19. public class ScoreBar : DrawableGameComponent
  20. {
  21. #region Enums
  22. /// <summary>
  23. /// Used to determine the component's orientation.
  24. /// </summary>
  25. public enum ScoreBarOrientation
  26. {
  27. Vertical,
  28. Horizontal
  29. }
  30. #endregion
  31. #region Fields/Properties
  32. public int MinValue { get; set; }
  33. public int MaxValue { get; set; }
  34. public Vector2 Position { get; set; }
  35. public Color ScoreBarColor { get; set; }
  36. public int CurrentValue
  37. {
  38. get
  39. {
  40. return currentValue;
  41. }
  42. }
  43. ScoreBarOrientation scoreBarOrientation;
  44. int height;
  45. int width;
  46. ScaledSpriteBatch scaledSpriteBatch;
  47. int currentValue;
  48. Texture2D backgroundTexture;
  49. Texture2D redTexture;
  50. Texture2D greenTexture;
  51. Texture2D yellowTexture;
  52. GameplayScreen gameplayScreen;
  53. bool isAppearAtCountDown;
  54. #endregion
  55. #region Initialization
  56. /// <summary>
  57. /// Creates a new score bar instance.
  58. /// </summary>
  59. /// <param name="game">The associated game object.</param>
  60. /// <param name="minValue">The score bar's minimal value.</param>
  61. /// <param name="maxValue">The score bar's maximal value.</param>
  62. /// <param name="position">The score bar's position.</param>
  63. /// <param name="height">The score bar's height.</param>
  64. /// <param name="width">The score bar's width.</param>
  65. /// <param name="scoreBarColor">Color to tint the scorebar's background with.</param>
  66. /// <param name="scoreBarOrientation">The score bar's orientation.</param>
  67. /// <param name="initialValue">The score bar's initial value.</param>
  68. /// <param name="screen">Gameplay screen where the score bar will appear.</param>
  69. /// <param name="isAppearAtCountDown">Whether or not the score bar will appear during the game's initial
  70. /// countdown phase.</param>
  71. public ScoreBar(Game game, int minValue, int maxValue, Vector2 position, int height, int width,
  72. Color scoreBarColor, ScoreBarOrientation scoreBarOrientation, int initialValue, GameplayScreen screen,
  73. bool isAppearAtCountDown)
  74. : base(game)
  75. {
  76. this.MinValue = minValue;
  77. this.MaxValue = maxValue;
  78. this.Position = position;
  79. this.ScoreBarColor = scoreBarColor;
  80. this.scoreBarOrientation = scoreBarOrientation;
  81. this.currentValue = initialValue;
  82. this.width = width;
  83. this.height = height;
  84. this.gameplayScreen = screen;
  85. this.isAppearAtCountDown = isAppearAtCountDown;
  86. scaledSpriteBatch = (ScaledSpriteBatch)Game.Services.GetService(typeof(ScaledSpriteBatch));
  87. GetSpaceFromBorder();
  88. }
  89. /// <summary>
  90. /// Loads the content that this component will use.
  91. /// </summary>
  92. protected override void LoadContent()
  93. {
  94. backgroundTexture = Game.Content.Load<Texture2D>("Textures/barBlackBorder");
  95. greenTexture = Game.Content.Load<Texture2D>("Textures/barGreen");
  96. yellowTexture = Game.Content.Load<Texture2D>("Textures/barYellow");
  97. redTexture = Game.Content.Load<Texture2D>("Textures/barRed");
  98. base.LoadContent();
  99. }
  100. #endregion
  101. #region Render
  102. /// <summary>
  103. /// Draws the component.
  104. /// </summary>
  105. /// <param name="gameTime"></param>
  106. public override void Draw(GameTime gameTime)
  107. {
  108. if (!gameplayScreen.IsActive)
  109. {
  110. base.Draw(gameTime);
  111. return;
  112. }
  113. if (!isAppearAtCountDown && !gameplayScreen.IsStarted)
  114. {
  115. base.Draw(gameTime);
  116. return;
  117. }
  118. float rotation;
  119. // Determine the orientation of the component
  120. if (scoreBarOrientation == ScoreBar.ScoreBarOrientation.Horizontal)
  121. {
  122. rotation = 0f;
  123. }
  124. else
  125. {
  126. rotation = 1.57f;
  127. }
  128. scaledSpriteBatch.Begin();
  129. // Draws the background of the score bar
  130. scaledSpriteBatch.Draw(backgroundTexture, new Rectangle((int)Position.X, (int)Position.Y, width, height),
  131. null, ScoreBarColor, rotation, new Vector2(0, 0), SpriteEffects.None, 0);
  132. // Gets the margin from the border
  133. decimal spaceFromBorder = GetSpaceFromBorder();
  134. spaceFromBorder += 4;
  135. Texture2D coloredTexture = GetTextureByCurrentValue(currentValue);
  136. if (scoreBarOrientation == ScoreBarOrientation.Horizontal)
  137. {
  138. scaledSpriteBatch.Draw(coloredTexture, new Rectangle((int)Position.X + 2, (int)Position.Y + 2,
  139. width - (int)spaceFromBorder, height - 4), null, Color.White, rotation, new Vector2(0, 0),
  140. SpriteEffects.None, 0);
  141. }
  142. else
  143. {
  144. scaledSpriteBatch.Draw(coloredTexture, new Rectangle((int)Position.X + 2 - height,
  145. (int)Position.Y + width + -2, width - (int)spaceFromBorder, height - 4), null, ScoreBarColor,
  146. -rotation, new Vector2(0, 0), SpriteEffects.None, 0);
  147. }
  148. scaledSpriteBatch.End();
  149. base.Draw(gameTime);
  150. }
  151. #endregion
  152. #region Public Methods
  153. /// <summary>
  154. /// Increases the current value of the score bar.
  155. /// </summary>
  156. /// <param name="valueToIncrease">Number to increase the value by</param>
  157. /// <remarks>Negative numbers will have no effect.</remarks>
  158. public void IncreaseCurrentValue(int valueToIncrease)
  159. {
  160. // Make sure that the target value does not exceed the max value
  161. if (valueToIncrease >= 0 && currentValue < MaxValue && currentValue + valueToIncrease <= MaxValue)
  162. {
  163. currentValue += valueToIncrease;
  164. }
  165. // If the target value exceeds the max value, clamp the value to the maximum
  166. else if (currentValue + valueToIncrease > MaxValue)
  167. {
  168. currentValue = MaxValue;
  169. }
  170. }
  171. /// <summary>
  172. /// Decreases the current value of the score bar.
  173. /// </summary>
  174. /// <param name="valueToDecrease">Number to decrease the value by.</param>
  175. public void DecreaseCurrentValue(int valueToDecrease)
  176. {
  177. DecreaseCurrentValue(valueToDecrease, false);
  178. }
  179. /// <summary>
  180. /// Decreases the current value of the score bar.
  181. /// </summary>
  182. /// <param name="valueToDecrease">Number to decrease the value by.</param>
  183. /// <param name="clampToMinimum">If true, then decreasing by an amount which will cause the bar's value
  184. /// to go under its minimum will set the bar to its minimal value. If false, performing such an operation
  185. /// as just described will leave the bar's value unchanged.</param>
  186. /// <returns>The actual amount which was subtracted from the bar's value.</returns>
  187. public int DecreaseCurrentValue(int valueToDecrease, bool clampToMinimum)
  188. {
  189. // Make sure that the target value does not exceed the min value
  190. int valueThatWasDecreased = 0;
  191. if (valueToDecrease >= 0 && currentValue > MinValue && currentValue - valueToDecrease >= MinValue)
  192. {
  193. currentValue -= valueToDecrease;
  194. valueThatWasDecreased = valueToDecrease;
  195. }
  196. // If the target value exceeds the min value, clamp the value to the minimum (or do nothing)
  197. else if (currentValue - valueToDecrease < MinValue && clampToMinimum)
  198. {
  199. valueThatWasDecreased = currentValue - MinValue;
  200. currentValue = MinValue;
  201. }
  202. return valueThatWasDecreased;
  203. }
  204. #endregion
  205. #region Private Methods
  206. /// <summary>
  207. /// Calculate the empty portion of the score bar according to its current value.
  208. /// </summary>
  209. /// <returns>Width of the bar portion which should be empty to represent the bar's current score.</returns>
  210. private decimal GetSpaceFromBorder()
  211. {
  212. int textureSize;
  213. textureSize = width;
  214. decimal valuePercent = Decimal.Divide(currentValue, MaxValue) * 100;
  215. return textureSize - ((decimal)textureSize * valuePercent / (decimal)100);
  216. }
  217. /// <summary>
  218. /// Returns a texture for the score bar's "fill" according to its value.
  219. /// </summary>
  220. /// <param name="value">Current value of the score bar.</param>
  221. /// <returns>A texture the color of which indicates how close to the bar's maximum the value is.</returns>
  222. private Texture2D GetTextureByCurrentValue(int value)
  223. {
  224. Texture2D selectedTexture;
  225. decimal valuePercent = Decimal.Divide(currentValue, MaxValue) * 100;
  226. if (valuePercent > 50)
  227. {
  228. selectedTexture = greenTexture;
  229. }
  230. else if (valuePercent > 25)
  231. {
  232. selectedTexture = yellowTexture;
  233. }
  234. else
  235. {
  236. selectedTexture = redTexture;
  237. }
  238. return selectedTexture;
  239. }
  240. #endregion
  241. }
  242. }