Beehive.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Beehive.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. /// Represent a single beehive
  18. /// </summary>
  19. public class Beehive : TexturedDrawableGameComponent
  20. {
  21. #region Fields/Properties
  22. ScoreBar score;
  23. TimeSpan intervalToAddHoney = TimeSpan.FromMilliseconds(600);
  24. TimeSpan lastTimeHoneyAdded;
  25. bool allowBeesToGenerate = true;
  26. public bool AllowBeesToGenerate
  27. {
  28. get
  29. {
  30. return allowBeesToGenerate;
  31. }
  32. set
  33. {
  34. allowBeesToGenerate = value;
  35. }
  36. }
  37. public Boolean HasHoney
  38. {
  39. get
  40. {
  41. return score.CurrentValue > score.MinValue;
  42. }
  43. }
  44. public override Rectangle Bounds
  45. {
  46. get
  47. {
  48. Rectangle baseBounds = base.Bounds;
  49. int widthMargin = baseBounds.Width / 10;
  50. int width = baseBounds.Width - widthMargin;
  51. int height = baseBounds.Height / 3;
  52. return new Rectangle(baseBounds.X + widthMargin, baseBounds.Y + height, width - widthMargin, height);
  53. }
  54. }
  55. public override Rectangle CentralCollisionArea
  56. {
  57. get
  58. {
  59. Rectangle bounds = Bounds;
  60. int height = (int)Bounds.Height / 10 * 5;
  61. int width = (int)Bounds.Width / 10 * 4;
  62. int offsetY = ((int)Bounds.Height - height) / 2;
  63. int offsetX = ((int)Bounds.Width - width) / 2;
  64. return new Rectangle((int)Bounds.X + offsetX, (int)Bounds.Y + offsetY, width, height);
  65. }
  66. }
  67. #endregion
  68. #region Initialization
  69. /// <summary>
  70. /// Creates a new beehive instance.
  71. /// </summary>
  72. /// <param name="game">The game object.</param>
  73. /// <param name="gamePlayScreen">The gameplay screen.</param>
  74. /// <param name="texture">The texture representing the beehive.</param>
  75. /// <param name="score">Score object representing the amount of honey in the
  76. /// hive.</param>
  77. /// <param name="position">The beehive's position.</param>
  78. public Beehive(Game game, GameplayScreen gamePlayScreen, Texture2D texture, ScoreBar score, Vector2 position)
  79. : base(game, gamePlayScreen)
  80. {
  81. this.texture = texture;
  82. this.score = score;
  83. this.position = position;
  84. AllowBeesToGenerate = true;
  85. DrawOrder = (int)position.Y;
  86. }
  87. #endregion
  88. #region Update
  89. /// <summary>
  90. /// Updates the beehive's status.
  91. /// </summary>
  92. /// <param name="gameTime">Game time information.</param>
  93. public override void Update(GameTime gameTime)
  94. {
  95. if (!gamePlayScreen.IsActive)
  96. {
  97. base.Update(gameTime);
  98. return;
  99. }
  100. // Initialize the first time honey was added
  101. if (lastTimeHoneyAdded == TimeSpan.Zero)
  102. {
  103. lastTimeHoneyAdded = gameTime.TotalGameTime;
  104. score.IncreaseCurrentValue(1);
  105. }
  106. else
  107. {
  108. // If enough time has passed add more honey
  109. if (lastTimeHoneyAdded + intervalToAddHoney < gameTime.TotalGameTime)
  110. {
  111. lastTimeHoneyAdded = gameTime.TotalGameTime;
  112. score.IncreaseCurrentValue(1);
  113. }
  114. }
  115. base.Update(gameTime);
  116. }
  117. #endregion
  118. #region Render
  119. /// <summary>
  120. /// Render the beehive.
  121. /// </summary>
  122. /// <param name="gameTime">Game time information.</param>
  123. public override void Draw(GameTime gameTime)
  124. {
  125. if (!gamePlayScreen.IsActive)
  126. {
  127. base.Draw(gameTime);
  128. return;
  129. }
  130. scaledSpriteBatch.Begin();
  131. scaledSpriteBatch.Draw(texture, position, Color.White);
  132. scaledSpriteBatch.End();
  133. base.Draw(gameTime);
  134. }
  135. #endregion
  136. #region Public methods
  137. public void DecreaseHoney(int amount)
  138. {
  139. score.DecreaseCurrentValue(amount);
  140. }
  141. #endregion
  142. }
  143. }