AnimatedGameComponent.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AnimatedGameComponent.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 Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using CardsFramework;
  16. #endregion
  17. namespace CardsFramework
  18. {
  19. /// <summary>
  20. /// A game component.
  21. /// Enable variable display while managing and displaying a set of
  22. /// <see cref="AnimatedGameComponentAnimation">Animations</see>
  23. /// </summary>
  24. public class AnimatedGameComponent : DrawableGameComponent
  25. {
  26. #region Fields and Properties
  27. public Texture2D CurrentFrame { get; set; }
  28. public Rectangle? CurrentSegment { get; set; }
  29. public string Text { get; set; }
  30. public Color TextColor { get; set; }
  31. public bool IsFaceDown = true;
  32. public Vector2 CurrentPosition { get; set; }
  33. public Rectangle? CurrentDestination { get; set; }
  34. List<AnimatedGameComponentAnimation> runningAnimations =
  35. new List<AnimatedGameComponentAnimation>();
  36. /// <summary>
  37. /// Whether or not an animation belonging to the component is running.
  38. /// </summary>
  39. public virtual bool IsAnimating { get { return runningAnimations.Count > 0; } }
  40. public CardsGame CardGame { get; private set; }
  41. #endregion
  42. #region Initializatios
  43. /// <summary>
  44. /// Initializes a new instance of the class, using black text color.
  45. /// </summary>
  46. /// <param name="game">The associated game class.</param>
  47. public AnimatedGameComponent(Game game)
  48. : base(game)
  49. {
  50. TextColor = Color.Black;
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the class, using black text color.
  54. /// </summary>
  55. /// <param name="game">The associated game class.</param>
  56. /// <param name="currentFrame">The texture serving as the current frame
  57. /// to display as the component.</param>
  58. public AnimatedGameComponent(Game game, Texture2D currentFrame)
  59. : this(game)
  60. {
  61. CurrentFrame = currentFrame;
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the class, using black text color.
  65. /// </summary>
  66. /// <param name="cardGame">The associated card game.</param>
  67. /// <param name="currentFrame">The texture serving as the current frame
  68. /// to display as the component.</param>
  69. public AnimatedGameComponent(CardsGame cardGame, Texture2D currentFrame)
  70. : this(cardGame.Game)
  71. {
  72. CardGame = cardGame;
  73. CurrentFrame = currentFrame;
  74. }
  75. #endregion
  76. #region Update and Render
  77. /// <summary>
  78. /// Keeps track of the component's animations.
  79. /// </summary>
  80. /// <param name="gameTime">The time which as elapsed since the last call
  81. /// to this method.</param>
  82. public override void Update(GameTime gameTime)
  83. {
  84. base.Update(gameTime);
  85. for (int animationIndex = 0; animationIndex < runningAnimations.Count; animationIndex++)
  86. {
  87. runningAnimations[animationIndex].AccumulateElapsedTime(gameTime.ElapsedGameTime);
  88. runningAnimations[animationIndex].Run(gameTime);
  89. if (runningAnimations[animationIndex].IsDone())
  90. {
  91. runningAnimations.RemoveAt(animationIndex);
  92. animationIndex--;
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Draws the animated component and its associated text, if it exists, at
  98. /// the object's set destination. If a destination is not set, its initial
  99. /// position is used.
  100. /// </summary>
  101. /// <param name="gameTime">The time which as elapsed since the last call
  102. /// to this method.</param>
  103. public override void Draw(GameTime gameTime)
  104. {
  105. base.Draw(gameTime);
  106. SpriteBatch spriteBatch;
  107. if (CardGame != null)
  108. {
  109. spriteBatch = CardGame.SpriteBatch;
  110. }
  111. else
  112. {
  113. spriteBatch = new SpriteBatch(Game.GraphicsDevice);
  114. }
  115. spriteBatch.Begin();
  116. // Draw at the destination if one is set
  117. if (CurrentDestination.HasValue)
  118. {
  119. if (CurrentFrame != null)
  120. {
  121. spriteBatch.Draw(CurrentFrame, CurrentDestination.Value, CurrentSegment, Color.White);
  122. if (Text != null)
  123. {
  124. Vector2 size = CardGame.Font.MeasureString(Text);
  125. Vector2 textPosition = new Vector2(CurrentDestination.Value.X +
  126. CurrentDestination.Value.Width / 2 - size.X / 2,
  127. CurrentDestination.Value.Y + CurrentDestination.Value.Height / 2 - size.Y / 2);
  128. spriteBatch.DrawString(CardGame.Font, Text, textPosition, TextColor);
  129. }
  130. }
  131. }
  132. // Draw at the component's position if there is no destination
  133. else
  134. {
  135. if (CurrentFrame != null)
  136. {
  137. spriteBatch.Draw(CurrentFrame, CurrentPosition, CurrentSegment, Color.White);
  138. if (Text != null)
  139. {
  140. Vector2 size = CardGame.Font.MeasureString(Text);
  141. Vector2 textPosition = new Vector2(CurrentPosition.X +
  142. CurrentFrame.Bounds.Width / 2 - size.X / 2,
  143. CurrentPosition.Y + CurrentFrame.Bounds.Height / 2 - size.Y / 2);
  144. spriteBatch.DrawString(CardGame.Font, Text, textPosition, TextColor);
  145. }
  146. }
  147. }
  148. spriteBatch.End();
  149. }
  150. #endregion
  151. /// <summary>
  152. /// Adds an animation to the animated component.
  153. /// </summary>
  154. /// <param name="animation">The animation to add.</param>
  155. public void AddAnimation(AnimatedGameComponentAnimation animation)
  156. {
  157. animation.Component = this;
  158. runningAnimations.Add(animation);
  159. }
  160. /// <summary>
  161. /// Calculate the estimated time at which the longest lasting animation currently managed
  162. /// will complete.
  163. /// </summary>
  164. /// <returns>The estimated time for animation complete </returns>
  165. public virtual TimeSpan EstimatedTimeForAnimationsCompletion()
  166. {
  167. TimeSpan result = TimeSpan.Zero;
  168. if (IsAnimating)
  169. {
  170. for (int animationIndex = 0; animationIndex < runningAnimations.Count; animationIndex++)
  171. {
  172. if (runningAnimations[animationIndex].EstimatedTimeForAnimationCompletion > result)
  173. {
  174. result = runningAnimations[animationIndex].EstimatedTimeForAnimationCompletion;
  175. }
  176. }
  177. }
  178. return result;
  179. }
  180. }
  181. }