TilemapTileLayer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. namespace MonoGame.Extended.Tilemaps
  5. {
  6. /// <summary>
  7. /// Represents a layer composed of a grid of tiles.
  8. /// </summary>
  9. public class TilemapTileLayer : TilemapLayer
  10. {
  11. private readonly TilemapTile?[,] _tiles;
  12. /// <summary>
  13. /// Gets the width of the layer in tiles.
  14. /// </summary>
  15. public int Width { get; }
  16. /// <summary>
  17. /// Gets the height of the layer in tiles.
  18. /// </summary>
  19. public int Height { get; }
  20. /// <summary>
  21. /// Gets the width of each tile in pixels.
  22. /// </summary>
  23. public int TileWidth { get; }
  24. /// <summary>
  25. /// Gets the height of each tile in pixels.
  26. /// </summary>
  27. public int TileHeight { get; }
  28. /// <inheritdoc/>
  29. public override Rectangle Bounds
  30. {
  31. get
  32. {
  33. return new Rectangle(0, 0, Width * TileWidth, Height * TileHeight);
  34. }
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="TilemapTileLayer"/> class.
  38. /// </summary>
  39. /// <param name="name">The name of the layer.</param>
  40. /// <param name="width">The width of the layer in tiles.</param>
  41. /// <param name="height">The height of the layer in tiles.</param>
  42. /// <param name="tileWidth">The width of each tile in pixels.</param>
  43. /// <param name="tileHeight">The height of each tile in pixels.</param>
  44. public TilemapTileLayer(string name, int width, int height, int tileWidth, int tileHeight)
  45. : base(name)
  46. {
  47. Width = width;
  48. Height = height;
  49. TileWidth = tileWidth;
  50. TileHeight = tileHeight;
  51. _tiles = new TilemapTile?[width, height];
  52. }
  53. /// <summary>
  54. /// Gets the tile at the specified tile coordinates.
  55. /// </summary>
  56. /// <param name="x">The X coordinate of the tile.</param>
  57. /// <param name="y">The Y coordinate of the tile.</param>
  58. /// <returns>The tile at the specified coordinates, or <see langword="null"/> if empty.</returns>
  59. public TilemapTile? GetTile(int x, int y)
  60. {
  61. if (x < 0 || x >= Width || y < 0 || y >= Height)
  62. {
  63. return null;
  64. }
  65. return _tiles[x, y];
  66. }
  67. /// <summary>
  68. /// Sets the tile at the specified tile coordinates.
  69. /// </summary>
  70. /// <param name="x">The X coordinate of the tile.</param>
  71. /// <param name="y">The Y coordinate of the tile.</param>
  72. /// <param name="tile">The tile to set, or <see langword="null"/> to clear.</param>
  73. public void SetTile(int x, int y, TilemapTile? tile)
  74. {
  75. if (x < 0 || x >= Width || y < 0 || y >= Height)
  76. {
  77. throw new ArgumentOutOfRangeException();
  78. }
  79. _tiles[x, y] = tile;
  80. }
  81. /// <summary>
  82. /// Gets all non-empty tiles in the layer.
  83. /// </summary>
  84. /// <returns>An enumerable of tuples containing tile coordinates and tile data.</returns>
  85. public IEnumerable<(int x, int y, TilemapTile tile)> GetTiles()
  86. {
  87. for (int y = 0; y < Height; y++)
  88. {
  89. for (int x = 0; x < Width; x++)
  90. {
  91. TilemapTile? tile = _tiles[x, y];
  92. if (tile.HasValue)
  93. {
  94. yield return (x, y, tile.Value);
  95. }
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Gets all tiles within the specified rectangular region.
  101. /// </summary>
  102. /// <param name="region">The region in tile coordinates.</param>
  103. /// <returns>An enumerable of tuples containing tile coordinates and tile data.</returns>
  104. public IEnumerable<(int x, int y, TilemapTile tile)> GetTilesInRegion(Rectangle region)
  105. {
  106. // Clamp region to layer bounds
  107. int startX = Math.Max(0, region.X);
  108. int startY = Math.Max(0, region.Y);
  109. int endX = Math.Min(Width, region.X + region.Width);
  110. int endY = Math.Min(Height, region.Y + region.Height);
  111. for (int y = startY; y < endY; y++)
  112. {
  113. for (int x = startX; x < endX; x++)
  114. {
  115. TilemapTile? tile = _tiles[x, y];
  116. if (tile.HasValue)
  117. {
  118. yield return (x, y, tile.Value);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }