DeckDisplayComponent.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // DeckDisplayComponent.cs
  3. //
  4. // Displays a visual representation of the card deck during gameplay
  5. //-----------------------------------------------------------------------------
  6. using System;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace CardsFramework
  10. {
  11. /// <summary>
  12. /// Component that displays a visual representation of the deck at the dealer position.
  13. /// Shows a stack of card backs to indicate where cards are being dealt from.
  14. /// </summary>
  15. public class DeckDisplayComponent : DrawableGameComponent
  16. {
  17. private readonly CardsGame cardGame;
  18. private readonly SpriteBatch spriteBatch;
  19. private readonly Matrix globalTransformation;
  20. private readonly Vector2 position;
  21. private Texture2D cardBackTexture;
  22. /// <summary>
  23. /// Number of card back layers to draw for the stacked effect
  24. /// </summary>
  25. public int StackLayers { get; set; } = 3;
  26. /// <summary>
  27. /// Offset between each layer to create depth
  28. /// </summary>
  29. public Vector2 LayerOffset { get; set; } = new Vector2(1, 1);
  30. /// <summary>
  31. /// Rotation angle in radians (default -45 degrees / clockwise for casino look)
  32. /// </summary>
  33. public float Rotation { get; set; } = MathHelper.ToRadians(-45);
  34. /// <summary>
  35. /// Whether the deck should be visible
  36. /// </summary>
  37. public new bool Visible { get; set; } = true;
  38. /// <summary>
  39. /// Creates a new deck display component
  40. /// </summary>
  41. /// <param name="game">The game instance</param>
  42. /// <param name="cardGame">The card game instance</param>
  43. /// <param name="position">Position where the deck should be displayed</param>
  44. /// <param name="spriteBatch">Shared sprite batch for rendering</param>
  45. /// <param name="globalTransformation">Transformation matrix for scaling</param>
  46. public DeckDisplayComponent(
  47. Game game,
  48. CardsGame cardGame,
  49. Vector2 position,
  50. SpriteBatch spriteBatch,
  51. Matrix globalTransformation)
  52. : base(game)
  53. {
  54. this.cardGame = cardGame;
  55. this.position = position;
  56. this.spriteBatch = spriteBatch;
  57. this.globalTransformation = globalTransformation;
  58. DrawOrder = -5000; // Draw behind dealt cards but in front of table
  59. }
  60. /// <summary>
  61. /// Load the card back texture
  62. /// </summary>
  63. protected override void LoadContent()
  64. {
  65. base.LoadContent();
  66. // Get the card back texture from the card game's assets
  67. if (cardGame.cardsAssets.ContainsKey("CardBack_" + cardGame.Theme))
  68. {
  69. cardBackTexture = cardGame.cardsAssets["CardBack_" + cardGame.Theme];
  70. }
  71. }
  72. /// <summary>
  73. /// Draw the deck as a stack of card backs
  74. /// </summary>
  75. public override void Draw(GameTime gameTime)
  76. {
  77. if (!Visible || cardBackTexture == null)
  78. return;
  79. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, globalTransformation);
  80. // Calculate origin point for rotation (center of the card)
  81. Vector2 origin = new Vector2(cardBackTexture.Width / 2f, cardBackTexture.Height / 2f);
  82. // Draw multiple layers to create a stacked deck effect
  83. for (int i = 0; i < StackLayers; i++)
  84. {
  85. Vector2 layerPosition = position + (LayerOffset * i);
  86. // Draw with slight transparency on lower layers for depth
  87. float alpha = 1.0f - (i * 0.1f);
  88. Color color = Color.White * alpha;
  89. // Draw with rotation around the center origin
  90. spriteBatch.Draw(cardBackTexture, layerPosition, null, color, Rotation, origin, 1.0f, SpriteEffects.None, 0f);
  91. }
  92. spriteBatch.End();
  93. base.Draw(gameTime);
  94. }
  95. }
  96. }