GameTable.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //-----------------------------------------------------------------------------
  2. // GameTable.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.IO;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. namespace CardsFramework
  14. {
  15. /// <summary>
  16. /// The UI representation of the table where the game is played.
  17. /// </summary>
  18. public class GameTable : DrawableGameComponent
  19. {
  20. public string Theme { get; private set; }
  21. public Texture2D TableTexture { get; private set; }
  22. public Vector2 DealerPosition { get; private set; }
  23. public SpriteBatch SpriteBatch { get; private set; }
  24. protected Matrix globalTransformation;
  25. public Func<int, Vector2> PlaceOrder { get; private set; }
  26. public Rectangle TableBounds { get; private set; }
  27. public int Places { get; private set; }
  28. /// <summary>
  29. /// Updates the number of places/spots to display on the table.
  30. /// </summary>
  31. /// <param name="places">The new number of places.</param>
  32. public void SetPlaces(int places)
  33. {
  34. Places = places;
  35. }
  36. /// <summary>
  37. /// Returns the player position on the table according to the player index.
  38. /// </summary>
  39. /// <param name="index">Player's index.</param>
  40. /// <returns>The position of the player corrsponding to the
  41. /// supplied index.</returns>
  42. /// <remarks>The location's are relative to the entire game area, even
  43. /// if the table only occupies part of it.</remarks>
  44. public Vector2 this[int index]
  45. {
  46. get
  47. {
  48. return new Vector2(TableBounds.Left, TableBounds.Top) +
  49. PlaceOrder(index);
  50. }
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the class.
  54. /// </summary>
  55. /// <param name="tableBounds">The table bounds.</param>
  56. /// <param name="dealerPosition">The dealer's position.</param>
  57. /// <param name="places">Amount of places on the table</param>
  58. /// <param name="placeOrder">A method to convert player indices to their
  59. /// respective location on the table.</param>
  60. /// <param name="theme">The theme used to display UI elements.</param>
  61. /// <param name="game">The associated game object.</param>
  62. public GameTable(Rectangle tableBounds, Vector2 dealerPosition, int places,
  63. Func<int, Vector2> placeOrder, string theme, Game game, SpriteBatch sharedSpriteBatch, Matrix? globalTransformation = null)
  64. : base(game)
  65. {
  66. TableBounds = tableBounds;
  67. DealerPosition = dealerPosition +
  68. new Vector2(tableBounds.Left, tableBounds.Top);
  69. Places = places;
  70. PlaceOrder = placeOrder;
  71. Theme = theme;
  72. this.SpriteBatch = sharedSpriteBatch;
  73. this.globalTransformation = globalTransformation ?? Matrix.Identity;
  74. }
  75. /// <summary>
  76. /// Load the table texture.
  77. /// </summary>
  78. protected override void LoadContent()
  79. {
  80. string assetName = Path.Combine("Images", "UI", "table");
  81. TableTexture = Game.Content.Load<Texture2D>(assetName);
  82. base.LoadContent();
  83. }
  84. /// <summary>
  85. /// Render the table.
  86. /// </summary>
  87. /// <param name="gameTime">Time passed since the last call to
  88. /// this method.</param>
  89. public override void Draw(GameTime gameTime)
  90. {
  91. SpriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, globalTransformation);
  92. // Draw the table texture
  93. SpriteBatch.Draw(TableTexture, TableBounds, Color.White);
  94. SpriteBatch.End();
  95. base.Draw(gameTime);
  96. }
  97. }
  98. }