AnimatedCardsGameComponent.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /// <summary>
  25. /// Initializes a new instance of the class.
  26. /// </summary>
  27. /// <param name="card">The card associated with the animation component.</param>
  28. /// <param name="cardGame">The associated game.</param>
  29. public AnimatedCardsGameComponent(TraditionalCard card, CardsGame cardGame, SpriteBatch? sharedSpriteBatch = null, Matrix? globalTransformation = null)
  30. : base(cardGame, null, sharedSpriteBatch, globalTransformation)
  31. {
  32. Card = card;
  33. this.spriteBatch = sharedSpriteBatch;
  34. this.globalTransformation = globalTransformation ?? Matrix.Identity;
  35. }
  36. /// <summary>
  37. /// Updates the component.
  38. /// </summary>
  39. /// <param name="gameTime">The game time.</param>
  40. public override void Update(GameTime gameTime)
  41. {
  42. base.Update(gameTime);
  43. CurrentFrame = IsFaceDown ? CardGame.cardsAssets["CardBack_" + CardGame.Theme] :
  44. CardGame.cardsAssets[UIUtilty.GetCardAssetName(Card)];
  45. }
  46. /// <summary>
  47. /// Draws the component.
  48. /// </summary>
  49. /// <param name="gameTime">The game time.</param>
  50. public override void Draw(GameTime gameTime)
  51. {
  52. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, globalTransformation);
  53. // Draw the current at the designated destination, or at the initial
  54. // position if a destination has not been set
  55. if (CurrentFrame != null)
  56. {
  57. if (CurrentDestination.HasValue)
  58. {
  59. spriteBatch.Draw(CurrentFrame,
  60. CurrentDestination.Value, Color.White);
  61. }
  62. else
  63. {
  64. spriteBatch.Draw(CurrentFrame,
  65. CurrentPosition, Color.White);
  66. }
  67. }
  68. spriteBatch.End();
  69. base.Draw(gameTime);
  70. }
  71. }
  72. }