ShuffleAnimation.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // ShuffleAnimation.cs
  3. //
  4. // Abstract base class for card shuffle animations
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using System.Collections.Generic;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. namespace CardsFramework
  11. {
  12. /// <summary>
  13. /// Abstract base class for shuffle animations. Defines the interface for
  14. /// creating different types of card shuffles (riffle, overhand, Hindu, etc.)
  15. /// </summary>
  16. public abstract class ShuffleAnimation
  17. {
  18. /// <summary>
  19. /// The position where the shuffle animation takes place (typically dealer position)
  20. /// </summary>
  21. public Vector2 Position { get; set; }
  22. /// <summary>
  23. /// Total duration of the shuffle animation
  24. /// </summary>
  25. public TimeSpan Duration { get; set; }
  26. /// <summary>
  27. /// The card game this animation belongs to
  28. /// </summary>
  29. protected CardsGame CardGame { get; private set; }
  30. /// <summary>
  31. /// Random number generator for animation variations
  32. /// </summary>
  33. protected Random Random { get; private set; }
  34. /// <summary>
  35. /// Size of a single card
  36. /// </summary>
  37. protected Vector2 CardSize { get; private set; }
  38. /// <summary>
  39. /// Callback to invoke when the animation starts
  40. /// </summary>
  41. public Action OnAnimationStart { get; set; }
  42. /// <summary>
  43. /// Callback to invoke when the animation completes
  44. /// </summary>
  45. public Action OnAnimationComplete { get; set; }
  46. /// <summary>
  47. /// Creates a new shuffle animation
  48. /// </summary>
  49. /// <param name="cardGame">The card game instance</param>
  50. /// <param name="position">Position where shuffle occurs</param>
  51. /// <param name="duration">How long the animation should last</param>
  52. /// <param name="cardSize">Size of each card</param>
  53. protected ShuffleAnimation(CardsGame cardGame, Vector2 position, TimeSpan duration, Vector2 cardSize)
  54. {
  55. CardGame = cardGame;
  56. Position = position;
  57. Duration = duration;
  58. CardSize = cardSize;
  59. Random = new Random();
  60. }
  61. /// <summary>
  62. /// Creates the animated card components for this shuffle animation.
  63. /// Each shuffle type implements this differently.
  64. /// </summary>
  65. /// <param name="deck">The list of cards to shuffle</param>
  66. /// <param name="spriteBatch">Shared sprite batch for rendering</param>
  67. /// <param name="globalTransformation">Transformation matrix for scaling</param>
  68. /// <returns>List of animated card components with animations applied</returns>
  69. public abstract List<AnimatedCardsGameComponent> CreateAnimatedCards(
  70. List<TraditionalCard> deck,
  71. SpriteBatch spriteBatch,
  72. Matrix globalTransformation);
  73. /// <summary>
  74. /// Helper method to create a standard card component
  75. /// </summary>
  76. protected AnimatedCardsGameComponent CreateCardComponent(
  77. TraditionalCard card,
  78. SpriteBatch spriteBatch,
  79. Matrix globalTransformation,
  80. Vector2 startPosition,
  81. bool faceDown = true)
  82. {
  83. var cardComponent = new AnimatedCardsGameComponent(card, CardGame, spriteBatch, globalTransformation)
  84. {
  85. CurrentPosition = startPosition,
  86. IsFaceDown = faceDown,
  87. Visible = true
  88. };
  89. return cardComponent;
  90. }
  91. /// <summary>
  92. /// Helper to add a transition animation to a card
  93. /// </summary>
  94. protected void AddTransition(
  95. AnimatedCardsGameComponent card,
  96. Vector2 destination,
  97. TimeSpan startDelay,
  98. TimeSpan duration)
  99. {
  100. var transition = new TransitionGameComponentAnimation(card.CurrentPosition, destination)
  101. {
  102. StartTime = DateTime.Now + startDelay,
  103. Duration = duration
  104. };
  105. card.AddAnimation(transition);
  106. }
  107. /// <summary>
  108. /// Helper to add a scale animation to a card
  109. /// </summary>
  110. protected void AddScale(
  111. AnimatedCardsGameComponent card,
  112. float startScale,
  113. float endScale,
  114. TimeSpan startDelay,
  115. TimeSpan duration)
  116. {
  117. var scale = new ScaleGameComponentAnimation(startScale, endScale)
  118. {
  119. StartTime = DateTime.Now + startDelay,
  120. Duration = duration
  121. };
  122. card.AddAnimation(scale);
  123. }
  124. }
  125. }