HoneyJar.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // HoneyJar.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 represent the Honey Jar
  18. /// </summary>
  19. public class HoneyJar : TexturedDrawableGameComponent
  20. {
  21. #region Fields/Properties
  22. const string HoneyText = "Honey";
  23. ScoreBar score;
  24. SpriteFont font16px;
  25. Vector2 honeyTextSize;
  26. public bool CanCarryMore
  27. {
  28. get
  29. {
  30. return score.CurrentValue < score.MaxValue;
  31. }
  32. }
  33. public bool HasHoney
  34. {
  35. get
  36. {
  37. return score.CurrentValue > score.MinValue;
  38. }
  39. }
  40. #endregion
  41. #region Initialization
  42. /// <summary>
  43. /// Creates a new instance of the component.
  44. /// </summary>
  45. /// <param name="game">The associated game object.</param>
  46. /// <param name="gamePlayScreen">The gameplay screen where the component will be rendered.</param>
  47. /// <param name="position">The position of the component.</param>
  48. /// <param name="score">Scorebar representing the amount of honey in the jar.</param>
  49. public HoneyJar(Game game, GameplayScreen gamePlayScreen, Vector2 position, ScoreBar score)
  50. : base(game, gamePlayScreen)
  51. {
  52. this.position = position;
  53. this.score = score;
  54. }
  55. /// <summary>
  56. /// Loads the content used by the component.
  57. /// </summary>
  58. protected override void LoadContent()
  59. {
  60. font16px = Game.Content.Load<SpriteFont>("Fonts/GameScreenFont16px");
  61. texture = Game.Content.Load<Texture2D>("Textures/HoneyJar");
  62. honeyTextSize = font16px.MeasureString(HoneyText) * scaledSpriteBatch.ScaleVector;
  63. base.LoadContent();
  64. }
  65. #endregion
  66. #region Render
  67. /// <summary>
  68. /// Draws the component.
  69. /// </summary>
  70. /// <param name="gameTime">Game time information.</param>
  71. public override void Draw(GameTime gameTime)
  72. {
  73. if (!gamePlayScreen.IsActive)
  74. {
  75. base.Draw(gameTime);
  76. return;
  77. }
  78. scaledSpriteBatch.Begin();
  79. scaledSpriteBatch.Draw(texture, position, Color.White);
  80. scaledSpriteBatch.DrawString(font16px, HoneyText, position +
  81. new Vector2(Bounds.Width / 2 - honeyTextSize.X / 2, Bounds.Height * 4 / 3), Color.White);
  82. scaledSpriteBatch.End();
  83. base.Draw(gameTime);
  84. }
  85. #endregion
  86. #region Public Methods
  87. /// <summary>
  88. /// Increases honey stored in the jar by the specified amount.
  89. /// </summary>
  90. /// <param name="value">The amount of honey to add to the jar.</param>
  91. public void IncreaseHoney(int value)
  92. {
  93. score.IncreaseCurrentValue(value);
  94. }
  95. /// <summary>
  96. /// Decreases honey stored in the jar by the specified amount.
  97. /// </summary>
  98. /// <param name="value">The amount of honey to remove from the jar.</param>
  99. public void DecreaseHoney(int value)
  100. {
  101. score.DecreaseCurrentValue(value);
  102. }
  103. /// <summary>
  104. /// Decrease the amount of honey in the jar by a specified percent of the jar's total capacity.
  105. /// </summary>
  106. /// <param name="percent">The percent of the jar's capacity by which to decrease the current amount
  107. /// of honey. If the jar's capacity is 100 and this value is 20, then the amount of honey will be reduced
  108. /// by 20.</param>
  109. public int DecreaseHoneyByPercent(int percent)
  110. {
  111. return score.DecreaseCurrentValue(percent * score.MaxValue / 100, true);
  112. }
  113. #endregion
  114. }
  115. }