AnimatedHandGameComponent.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AnimatedHandGameComponent.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 System.Collections.Generic;
  12. using System.Text;
  13. using CardsFramework;
  14. using Microsoft.Xna.Framework;
  15. #endregion
  16. namespace CardsFramework
  17. {
  18. public class AnimatedHandGameComponent : AnimatedGameComponent
  19. {
  20. #region Fields and Properties
  21. public int Place { get; private set; }
  22. public readonly Hand Hand;
  23. List<AnimatedCardsGameComponent> heldAnimatedCards = new List<AnimatedCardsGameComponent>();
  24. public override bool IsAnimating
  25. {
  26. get
  27. {
  28. for (int animationIndex = 0; animationIndex < heldAnimatedCards.Count; animationIndex++)
  29. {
  30. if (heldAnimatedCards[animationIndex].IsAnimating)
  31. {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. }
  38. /// <summary>
  39. /// Returns the animated cards contained in the hand.
  40. /// </summary>
  41. public IEnumerable<AnimatedCardsGameComponent> AnimatedCards
  42. {
  43. get
  44. {
  45. return heldAnimatedCards.AsReadOnly();
  46. }
  47. }
  48. #endregion
  49. #region Initiaizations
  50. /// <summary>
  51. /// Initializes a new instance of the animated hand component. This means
  52. /// setting the hand's position and initializing all animated cards and their
  53. /// respective positions. Also, registrations are performed to the associated
  54. /// <paramref name="hand"/> events to update the animated hand as cards are
  55. /// added or removed.
  56. /// </summary>
  57. /// <param name="place">The player's place index (-1 for the dealer).</param>
  58. /// <param name="hand">The hand represented by this instance.</param>
  59. /// <param name="cardGame">The associated card game.</param>
  60. public AnimatedHandGameComponent(int place, Hand hand, CardsGame cardGame)
  61. : base(cardGame, null)
  62. {
  63. Place = place;
  64. Hand = hand;
  65. hand.ReceivedCard += Hand_ReceivedCard;
  66. hand.LostCard += Hand_LostCard;
  67. // Set the component's position
  68. if (place == -1)
  69. {
  70. CurrentPosition = CardGame.GameTable.DealerPosition;
  71. }
  72. else
  73. {
  74. CurrentPosition = CardGame.GameTable[place];
  75. }
  76. // Create and initialize animated cards according to the cards in the
  77. // associated hand
  78. for (int cardIndex = 0; cardIndex < hand.Count; cardIndex++)
  79. {
  80. AnimatedCardsGameComponent animatedCardGameComponent =
  81. new AnimatedCardsGameComponent(hand[cardIndex], CardGame)
  82. {
  83. CurrentPosition = CurrentPosition + new Vector2(30 * cardIndex, 0)
  84. };
  85. heldAnimatedCards.Add(animatedCardGameComponent);
  86. Game.Components.Add(animatedCardGameComponent);
  87. }
  88. Game.Components.ComponentRemoved += Components_ComponentRemoved;
  89. }
  90. #endregion
  91. #region Update
  92. /// <summary>
  93. /// Updates the component.
  94. /// </summary>
  95. /// <param name="gameTime">The time which elapsed since this method was last
  96. /// called.</param>
  97. public override void Update(GameTime gameTime)
  98. {
  99. // Arrange the hand's animated cards' positions
  100. for (int animationIndex = 0; animationIndex < heldAnimatedCards.Count; animationIndex++)
  101. {
  102. if (!heldAnimatedCards[animationIndex].IsAnimating)
  103. {
  104. heldAnimatedCards[animationIndex].CurrentPosition = CurrentPosition +
  105. GetCardRelativePosition(animationIndex);
  106. }
  107. }
  108. base.Update(gameTime);
  109. }
  110. #endregion
  111. #region Public Methods
  112. /// <summary>
  113. /// Gets the card's offset from the hand position according to its index
  114. /// in the hand.
  115. /// </summary>
  116. /// <param name="cardLocationInHand">The card index in the hand.</param>
  117. /// <returns></returns>
  118. public virtual Vector2 GetCardRelativePosition(int cardLocationInHand)
  119. {
  120. return default(Vector2);
  121. }
  122. /// <summary>
  123. /// Finds the index of a specified card in the hand.
  124. /// </summary>
  125. /// <param name="card">The card to locate.</param>
  126. /// <returns>The card's index inside the hand, or -1 if it cannot be
  127. /// found.</returns>
  128. public int GetCardLocationInHand(TraditionalCard card)
  129. {
  130. for (int animationIndex = 0; animationIndex < heldAnimatedCards.Count; animationIndex++)
  131. {
  132. if (heldAnimatedCards[animationIndex].Card == card)
  133. {
  134. return animationIndex;
  135. }
  136. }
  137. return -1;
  138. }
  139. /// <summary>
  140. /// Gets the animated game component associated with a specified card.
  141. /// </summary>
  142. /// <param name="card">The card for which to get the animation
  143. /// component.</param>
  144. /// <returns>The card's animation component, or null if such a card cannot
  145. /// be found in the hand.</returns>
  146. public AnimatedCardsGameComponent GetCardGameComponent(TraditionalCard card)
  147. {
  148. int location = GetCardLocationInHand(card);
  149. if (location == -1)
  150. return null;
  151. return heldAnimatedCards[location];
  152. }
  153. /// <summary>
  154. /// Gets the animated game component associated with a specified card.
  155. /// </summary>
  156. /// <param name="location">The location where the desired card is
  157. /// in the hand.</param>
  158. /// <returns>The card's animation component.</return>s
  159. public AnimatedCardsGameComponent GetCardGameComponent(int location)
  160. {
  161. if (location == -1 || location >= heldAnimatedCards.Count)
  162. return null;
  163. return heldAnimatedCards[location];
  164. }
  165. #endregion
  166. #region Event Handlers
  167. /// <summary>
  168. /// Handles the ComponentRemoved event of the Components control.
  169. /// </summary>
  170. /// <param name="sender">The source of the event.</param>
  171. /// <param name="e">The
  172. /// <see cref="Microsoft.Xna.Framework.GameComponentCollectionEventArgs"/>
  173. /// instance containing the event data.</param>
  174. void Components_ComponentRemoved(object sender, GameComponentCollectionEventArgs e)
  175. {
  176. if (e.GameComponent == this)
  177. {
  178. Dispose();
  179. }
  180. }
  181. /// <summary>
  182. /// Handles the hand's LostCard event be removing the corresponding animated
  183. /// card.
  184. /// </summary>
  185. /// <param name="sender">The source of the event.</param>
  186. /// <param name="e">The
  187. /// <see cref="CardsFramework.CardEventArgs"/>
  188. /// instance containing the event data.</param>
  189. void Hand_LostCard(object sender, CardEventArgs e)
  190. {
  191. // Remove the card from screen
  192. for (int animationIndex = 0; animationIndex < heldAnimatedCards.Count; animationIndex++)
  193. {
  194. if (heldAnimatedCards[animationIndex].Card == e.Card)
  195. {
  196. Game.Components.Remove(heldAnimatedCards[animationIndex]);
  197. heldAnimatedCards.RemoveAt(animationIndex);
  198. return;
  199. }
  200. }
  201. }
  202. /// <summary>
  203. /// Handles the hand's ReceivedCard event be adding a corresponding
  204. /// animated card.
  205. /// </summary>
  206. /// <param name="sender">The source of the event.</param>
  207. /// <param name="e">The
  208. /// <see cref="CardsFramework.CardEventArgs"/>
  209. /// instance containing the event data.</param>
  210. void Hand_ReceivedCard(object sender, CardEventArgs e)
  211. {
  212. // Add the card to the screen
  213. AnimatedCardsGameComponent animatedCardGameComponent =
  214. new AnimatedCardsGameComponent(e.Card, CardGame) { Visible = false };
  215. heldAnimatedCards.Add(animatedCardGameComponent);
  216. Game.Components.Add(animatedCardGameComponent);
  217. }
  218. #endregion
  219. /// <summary>
  220. /// Calculate the estimated time at which the longest lasting animation currently managed
  221. /// will complete.
  222. /// </summary>
  223. /// <returns>The estimated time for animation complete </returns>
  224. public override TimeSpan EstimatedTimeForAnimationsCompletion()
  225. {
  226. TimeSpan result = TimeSpan.Zero;
  227. if (IsAnimating)
  228. {
  229. for (int animationIndex = 0; animationIndex < heldAnimatedCards.Count; animationIndex++)
  230. {
  231. if (heldAnimatedCards[animationIndex].EstimatedTimeForAnimationsCompletion() > result)
  232. {
  233. result = heldAnimatedCards[animationIndex].EstimatedTimeForAnimationsCompletion();
  234. }
  235. }
  236. }
  237. return result;
  238. }
  239. /// <summary>
  240. /// Properly disposes of the component when it is removed.
  241. /// </summary>
  242. /// <param name="disposing"></param>
  243. protected override void Dispose(bool disposing)
  244. {
  245. // Remove the registrations to the event to make this
  246. // instance collectable by gc
  247. Hand.ReceivedCard -= Hand_ReceivedCard;
  248. Hand.LostCard -= Hand_LostCard;
  249. base.Dispose(disposing);
  250. }
  251. }
  252. }