Vat.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Vat.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. /// A game component that represents the vat.
  18. /// </summary>
  19. public class Vat : TexturedDrawableGameComponent
  20. {
  21. #region Field/Properties
  22. ScoreBar score;
  23. SpriteFont font14px;
  24. SpriteFont font16px;
  25. SpriteFont font36px;
  26. Vector2 emptyStringSize;
  27. Vector2 fullStringSize;
  28. Vector2 timeDigStringSize;
  29. Vector2 timeleftStringSize;
  30. TimeSpan timeLeft;
  31. const string EmptyString = "Empty";
  32. const string FullString = "Full";
  33. const string TimeLeftString = "Time Left";
  34. string timeLeftString = string.Empty;
  35. public Vector2 Position
  36. {
  37. get
  38. {
  39. return position;
  40. }
  41. }
  42. public Texture2D Texture
  43. {
  44. get
  45. {
  46. return texture;
  47. }
  48. }
  49. public int MaxVatCapacity
  50. {
  51. get
  52. {
  53. return score.MaxValue;
  54. }
  55. }
  56. public int CurrentVatCapacity
  57. {
  58. get
  59. {
  60. return score.CurrentValue;
  61. }
  62. }
  63. public override Rectangle CentralCollisionArea
  64. {
  65. get
  66. {
  67. Rectangle bounds = Bounds;
  68. int height = (int)bounds.Height / 10 * 5;
  69. int width = (int)bounds.Width / 10 * 8;
  70. int offsetY = ((int)bounds.Height - height) / 2;
  71. int offsetX = ((int)bounds.Width - width) / 2;
  72. return new Rectangle((int)bounds.X + offsetX, (int)bounds.Y + offsetY, width, height);
  73. }
  74. }
  75. public Rectangle VatDepositArea
  76. {
  77. get
  78. {
  79. Rectangle bounds = Bounds;
  80. float sizeFactor = 0.75f;
  81. float marginFactor = (1 - sizeFactor) / 2;
  82. int x = bounds.X + (int)(marginFactor * bounds.Width);
  83. int y = bounds.Y + (int)(marginFactor * bounds.Height);
  84. int width = (int)(bounds.Width * sizeFactor);
  85. int height = (int)(bounds.Height * sizeFactor);
  86. return new Rectangle(x, y, width, height);
  87. }
  88. }
  89. #endregion
  90. #region Initialization
  91. /// <summary>
  92. /// Creates a new vat instance.
  93. /// </summary>
  94. /// <param name="game">The associated game object.</param>
  95. /// <param name="gamePlayScreen">Gameplay screen where the vat will be displayed.</param>
  96. /// <param name="texture">The vat's texture.</param>
  97. /// <param name="position">The position of the vat.</param>
  98. /// <param name="score">An associated score bar.</param>
  99. public Vat(Game game, GameplayScreen gamePlayScreen, Texture2D texture, Vector2 position, ScoreBar score)
  100. : base(game, gamePlayScreen)
  101. {
  102. this.texture = texture;
  103. this.position = position;
  104. this.score = score;
  105. DrawOrder = (int)(position.Y + Bounds.Height);
  106. }
  107. /// <summary>
  108. /// Loads the content that will be used by this component.
  109. /// </summary>
  110. protected override void LoadContent()
  111. {
  112. font14px = Game.Content.Load<SpriteFont>("Fonts/GameScreenFont14px");
  113. font16px = Game.Content.Load<SpriteFont>("Fonts/GameScreenFont16px");
  114. font36px = Game.Content.Load<SpriteFont>("Fonts/GameScreenFont36px");
  115. fullStringSize = font14px.MeasureString(FullString) * scaledSpriteBatch.ScaleVector;
  116. emptyStringSize = font14px.MeasureString(EmptyString) * scaledSpriteBatch.ScaleVector;
  117. timeleftStringSize = font16px.MeasureString(TimeLeftString) * scaledSpriteBatch.ScaleVector;
  118. base.LoadContent();
  119. }
  120. #endregion
  121. #region Render
  122. /// <summary>
  123. /// Draws the component.
  124. /// </summary>
  125. /// <param name="gameTime">Game time information.</param>
  126. public override void Draw(GameTime gameTime)
  127. {
  128. if (!gamePlayScreen.IsActive)
  129. {
  130. base.Draw(gameTime);
  131. return;
  132. }
  133. // Draws the texture
  134. scaledSpriteBatch.Begin();
  135. scaledSpriteBatch.Draw(texture, position, Color.White);
  136. // Draws the "time left" text
  137. scaledSpriteBatch.DrawString(font16px, TimeLeftString,
  138. position + new Vector2(Bounds.Width / 2 - timeleftStringSize.X / 2, timeleftStringSize.Y - 8),
  139. Color.White, 0, Vector2.Zero, 0, SpriteEffects.None, 2f);
  140. // Draws how much time is left
  141. timeDigStringSize = font36px.MeasureString(timeLeftString) * scaledSpriteBatch.ScaleVector;
  142. Color colorToDraw = Color.White;
  143. if (timeLeft.Minutes == 0 && (timeLeft.Seconds == 30 || timeLeft.Seconds <= 10))
  144. {
  145. colorToDraw = Color.Red;
  146. }
  147. scaledSpriteBatch.DrawString(font36px, timeLeftString,
  148. position + new Vector2(Bounds.Width / 2 - timeDigStringSize.X / 2,
  149. Bounds.Height / 2 - timeDigStringSize.Y / 2),
  150. colorToDraw);
  151. // Draws the "full" and "empty" strings
  152. scaledSpriteBatch.DrawString(font14px, EmptyString,
  153. new Vector2(position.X, position.Y + Bounds.Height - emptyStringSize.Y), Color.White);
  154. scaledSpriteBatch.DrawString(font14px, FullString,
  155. new Vector2(position.X + Bounds.Width - fullStringSize.X,
  156. position.Y + Bounds.Height - emptyStringSize.Y), Color.White);
  157. scaledSpriteBatch.End();
  158. base.Draw(gameTime);
  159. }
  160. #endregion
  161. #region Public methods
  162. /// <summary>
  163. /// Translates time left in the game to a internal representation string.
  164. /// </summary>
  165. /// <param name="timeLeft">Time left before the current level ends.</param>
  166. public void DrawTimeLeft(TimeSpan timeLeft)
  167. {
  168. this.timeLeft = timeLeft;
  169. timeLeftString = String.Format("{0:00}:{1:00}", timeLeft.Minutes, timeLeft.Seconds);
  170. }
  171. /// <summary>
  172. /// Adds honey to the amount stored in the vat.
  173. /// </summary>
  174. /// <param name="value">Amount of honey to add.</param>
  175. public void IncreaseHoney(int value)
  176. {
  177. score.IncreaseCurrentValue(value);
  178. }
  179. #endregion
  180. }
  181. }