AnimatedCardsGameComponent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //-----------------------------------------------------------------------------
  2. // AnimatedCardsGameComponent.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Microsoft.Xna.Framework;
  11. using CardsFramework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using System.Diagnostics;
  14. namespace CardsFramework
  15. {
  16. /// <summary>
  17. /// An <see cref="AnimatedGameComponent"/> implemented for a card game
  18. /// </summary>
  19. public class AnimatedCardsGameComponent : AnimatedGameComponent
  20. {
  21. public TraditionalCard Card { get; private set; }
  22. private SpriteBatch spriteBatch;
  23. private Matrix globalTransformation;
  24. private Texture2D cachedFaceUpTexture;
  25. private Texture2D cachedFaceDownTexture;
  26. /// <summary>
  27. /// When true, assumes SpriteBatch.Begin() has already been called by parent.
  28. /// When false, manages its own Begin/End (legacy behavior for Game.Components).
  29. /// </summary>
  30. public bool UseManagedBatch { get; set; } = false;
  31. /// <summary>
  32. /// Initializes a new instance of the class.
  33. /// </summary>
  34. /// <param name="card">The card associated with the animation component.</param>
  35. /// <param name="cardGame">The associated game.</param>
  36. public AnimatedCardsGameComponent(TraditionalCard card, CardsGame cardGame, SpriteBatch sharedSpriteBatch = null, Matrix? globalTransformation = null)
  37. : base(cardGame, null, sharedSpriteBatch, globalTransformation)
  38. {
  39. Card = card;
  40. this.spriteBatch = sharedSpriteBatch;
  41. this.globalTransformation = globalTransformation ?? Matrix.Identity;
  42. }
  43. /// <summary>
  44. /// Updates the component.
  45. /// </summary>
  46. /// <param name="gameTime">The game time.</param>
  47. public override void Update(GameTime gameTime)
  48. {
  49. base.Update(gameTime);
  50. // Cache both face-up and face-down textures on first access
  51. // Then switch between them based on IsFaceDown state
  52. if (cachedFaceDownTexture == null)
  53. {
  54. cachedFaceDownTexture = CardGame.cardsAssets["CardBack_" + CardGame.Theme];
  55. }
  56. if (cachedFaceUpTexture == null)
  57. {
  58. cachedFaceUpTexture = CardGame.cardsAssets[UIUtility.GetCardAssetName(Card)];
  59. }
  60. // Use the appropriate cached texture based on current state
  61. CurrentFrame = IsFaceDown ? cachedFaceDownTexture : cachedFaceUpTexture;
  62. }
  63. /// <summary>
  64. /// Draws the component.
  65. /// </summary>
  66. /// <param name="gameTime">The game time.</param>
  67. public override void Draw(GameTime gameTime)
  68. {
  69. if (UseManagedBatch)
  70. {
  71. // Optimized path: Parent manages the batch (e.g., ShuffleAnimationComponent)
  72. // Draw directly without Begin/End for better performance
  73. if (CurrentFrame != null)
  74. {
  75. // Calculate origin point (center of texture for rotation)
  76. Vector2 origin = new Vector2(CurrentFrame.Width / 2f, CurrentFrame.Height / 2f);
  77. if (CurrentDestination.HasValue)
  78. {
  79. // When using destination rectangle with rotation, we need center position
  80. Vector2 centerPosition = new Vector2(
  81. CurrentDestination.Value.X + CurrentDestination.Value.Width / 2f,
  82. CurrentDestination.Value.Y + CurrentDestination.Value.Height / 2f);
  83. // Calculate scale to match destination size
  84. Vector2 scale = new Vector2(
  85. CurrentDestination.Value.Width / (float)CurrentFrame.Width,
  86. CurrentDestination.Value.Height / (float)CurrentFrame.Height);
  87. spriteBatch.Draw(CurrentFrame, centerPosition, null, Color,
  88. CurrentRotation, origin, scale, SpriteEffects.None, 0f);
  89. }
  90. else
  91. {
  92. // Position + origin to center the rotation point
  93. Vector2 centerPosition = CurrentPosition + origin;
  94. spriteBatch.Draw(CurrentFrame, centerPosition, null, Color,
  95. CurrentRotation, origin, 1f, SpriteEffects.None, 0f);
  96. }
  97. }
  98. }
  99. else
  100. {
  101. // Legacy path: Manage our own batch (for Game.Components usage)
  102. base.Draw(gameTime);
  103. }
  104. }
  105. }
  106. }