TilemapTileset.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace MonoGame.Extended.Tilemaps
  5. {
  6. /// <summary>
  7. /// Represents a collection of tiles from a single texture atlas.
  8. /// </summary>
  9. public class TilemapTileset
  10. {
  11. private readonly Dictionary<int, TilemapTileData> _tileData = new();
  12. /// <summary>
  13. /// Gets the name of the tileset.
  14. /// </summary>
  15. public string Name { get; }
  16. /// <summary>
  17. /// Gets the width of each tile in pixels.
  18. /// </summary>
  19. public int TileWidth { get; }
  20. /// <summary>
  21. /// Gets the height of each tile in pixels.
  22. /// </summary>
  23. public int TileHeight { get; }
  24. /// <summary>
  25. /// Gets the total number of tiles in the tileset.
  26. /// </summary>
  27. public int TileCount { get; }
  28. /// <summary>
  29. /// Gets the number of columns in the tileset.
  30. /// </summary>
  31. public int Columns { get; }
  32. /// <summary>
  33. /// Gets the spacing between tiles in pixels.
  34. /// </summary>
  35. public int Spacing { get; }
  36. /// <summary>
  37. /// Gets the margin around the tileset in pixels.
  38. /// </summary>
  39. public int Margin { get; }
  40. /// <summary>
  41. /// Gets the first global tile ID for tiles in this tileset.
  42. /// </summary>
  43. /// <remarks>
  44. /// This value determines the range of global IDs this tileset covers.
  45. /// Tiles in this tileset have IDs from FirstGlobalId to FirstGlobalId + TileCount - 1.
  46. /// </remarks>
  47. public int FirstGlobalId { get; internal set; }
  48. /// <summary>
  49. /// Gets the texture atlas.
  50. /// </summary>
  51. public Texture2D Texture { get; }
  52. /// <summary>
  53. /// Gets or sets the offset applied when rendering tiles from this tileset.
  54. /// </summary>
  55. public Vector2 TileOffset { get; set; }
  56. /// <summary>
  57. /// Gets the custom properties of the tileset.
  58. /// </summary>
  59. public TilemapProperties Properties { get; }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="TilemapTileset"/> class.
  62. /// </summary>
  63. /// <param name="name">The name of the tileset.</param>
  64. /// <param name="texture">The texture atlas.</param>
  65. /// <param name="tileWidth">The width of each tile in pixels.</param>
  66. /// <param name="tileHeight">The height of each tile in pixels.</param>
  67. /// <param name="tileCount">The total number of tiles.</param>
  68. /// <param name="columns">The number of columns in the tileset.</param>
  69. /// <param name="spacing">The spacing between tiles in pixels.</param>
  70. /// <param name="margin">The margin around the tileset in pixels.</param>
  71. public TilemapTileset(string name, Texture2D texture, int tileWidth, int tileHeight, int tileCount, int columns, int spacing = 0, int margin = 0)
  72. {
  73. Name = name;
  74. Texture = texture;
  75. TileWidth = tileWidth;
  76. TileHeight = tileHeight;
  77. TileCount = tileCount;
  78. Columns = columns;
  79. Spacing = spacing;
  80. Margin = margin;
  81. Properties = new TilemapProperties();
  82. }
  83. /// <summary>
  84. /// Adds tile data for a specified local tile ID.
  85. /// </summary>
  86. /// <param name="tileData">The tile data to add.</param>
  87. internal void AddTileData(TilemapTileData tileData)
  88. {
  89. _tileData[tileData.LocalId] = tileData;
  90. }
  91. /// <summary>
  92. /// Gets the tile data for a specific local tile ID.
  93. /// </summary>
  94. /// <param name="localId">The local tile ID within this tileset.</param>
  95. /// <returns>The tile data, or <see langword="null"/> if not found.</returns>
  96. public TilemapTileData GetTileData(int localId)
  97. {
  98. if (_tileData.TryGetValue(localId, out var data))
  99. {
  100. return data;
  101. }
  102. return null;
  103. }
  104. /// <summary>
  105. /// Determines whether this tileset contains the specified global tile ID.
  106. /// </summary>
  107. /// <param name="globalTileId">The global tile ID to check.</param>
  108. /// <returns>
  109. /// <see langword="true"/> if the ID is within this tileset's range; otherwise, <see langword="false"/>.
  110. /// </returns>
  111. public bool ContainsGlobalId(int globalTileId)
  112. {
  113. return globalTileId >= FirstGlobalId &&
  114. globalTileId < FirstGlobalId + TileCount;
  115. }
  116. /// <summary>
  117. /// Gets the source rectangle for a tile within the texture atlas.
  118. /// </summary>
  119. /// <param name="localId">The local tile ID within this tileset.</param>
  120. /// <returns>The source rectangle for the tile.</returns>
  121. /// <remarks>
  122. /// This method accounts for spacing and margin when calculating the tile position.
  123. /// </remarks>
  124. public Rectangle GetTileRegion(int localId)
  125. {
  126. int column = localId % Columns;
  127. int row = localId / Columns;
  128. int x = Margin + column * (TileWidth + Spacing);
  129. int y = Margin + row * (TileHeight + Spacing);
  130. return new Rectangle(x, y, TileWidth, TileHeight);
  131. }
  132. }
  133. }