TilemapTile.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. namespace MonoGame.Extended.Tilemaps
  3. {
  4. /// <summary>
  5. /// Represents a tile instance in a tile layer.
  6. /// </summary>
  7. /// <remarks>
  8. /// A tile references a tileset and local tile ID through its global ID,
  9. /// and may include flip flags for horizontal, vertical, or diagonal transformations.
  10. /// </remarks>
  11. public readonly struct TilemapTile
  12. {
  13. /// <summary>
  14. /// Gets the global tile ID.
  15. /// </summary>
  16. /// <remarks>
  17. /// The global ID uniquely identifies a tile across all tilesets in the tilemap.
  18. /// It combines the tileset's first global ID with the local tile ID.
  19. /// </remarks>
  20. public int GlobalId { get; }
  21. /// <summary>
  22. /// Gets the flip transformation flags applied to this tile.
  23. /// </summary>
  24. public TilemapTileFlipFlags FlipFlags { get; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="TilemapTile"/> struct.
  27. /// </summary>
  28. /// <param name="globalId">The global tile ID.</param>
  29. /// <param name="flipFlags">The flip transformation flags.</param>
  30. public TilemapTile(int globalId, TilemapTileFlipFlags flipFlags = TilemapTileFlipFlags.None)
  31. {
  32. GlobalId = globalId;
  33. FlipFlags = flipFlags;
  34. }
  35. /// <summary>
  36. /// Gets the tileset that contains this tile.
  37. /// </summary>
  38. /// <param name="tilesets">The collection of tilesets.</param>
  39. /// <returns>The tileset containing this tile, or <see langword="null"/> if not found.</returns>
  40. public TilemapTileset GetTileset(TilemapTilesetCollection tilesets)
  41. {
  42. return tilesets.GetTilesetForGid(GlobalId);
  43. }
  44. /// <summary>
  45. /// Gets the local tile ID within the tileset.
  46. /// </summary>
  47. /// <param name="tilesets">The collection of tilesets.</param>
  48. /// <returns>The local tile ID.</returns>
  49. public int GetLocalId(TilemapTilesetCollection tilesets)
  50. {
  51. return tilesets.GetLocalId(GlobalId, out _);
  52. }
  53. /// <summary>
  54. /// Gets the local tile ID within the specified tileset.
  55. /// </summary>
  56. /// <param name="tilesets">The collection of tilesets.</param>
  57. /// <param name="tileset">When this method returns, contains the tileset that owns this tile.</param>
  58. /// <returns>The local tile ID within the tileset.</returns>
  59. public int GetLocalId(TilemapTilesetCollection tilesets, out TilemapTileset tileset)
  60. {
  61. return tilesets.GetLocalId(GlobalId, out tileset);
  62. }
  63. public TilemapTileData? GetTileData(TilemapTilesetCollection tilesets)
  64. {
  65. var tileset = GetTileset(tilesets);
  66. if (tileset == null)
  67. {
  68. return null;
  69. }
  70. var localId = GlobalId - tileset.FirstGlobalId;
  71. return tileset.GetTileData(localId);
  72. }
  73. }
  74. }