TiledMapTileLayer.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended.Tiled
  4. {
  5. public class TiledMapTileLayer : TiledMapLayer
  6. {
  7. public TiledMapTileLayer(string name, string type, int width, int height, int tileWidth, int tileHeight, Vector2? offset = null,
  8. Vector2? parallaxFactor = null, float opacity = 1, bool isVisible = true)
  9. : base(name, type, offset, parallaxFactor, opacity, isVisible)
  10. {
  11. Width = width;
  12. Height = height;
  13. TileWidth = tileWidth;
  14. TileHeight = tileHeight;
  15. Tiles = new TiledMapTile[Width * Height];
  16. }
  17. public int Width { get; }
  18. public int Height { get; }
  19. public int TileWidth { get; }
  20. public int TileHeight { get; }
  21. public TiledMapTile[] Tiles { get; }
  22. public int GetTileIndex(ushort x, ushort y)
  23. {
  24. return x + y * Width;
  25. }
  26. public bool TryGetTile(ushort x, ushort y, out TiledMapTile? tile)
  27. {
  28. if (x >= Width)
  29. {
  30. tile = null;
  31. return false;
  32. }
  33. var index = GetTileIndex(x, y);
  34. if (index < 0 || index >= Tiles.Length)
  35. {
  36. tile = null;
  37. return false;
  38. }
  39. tile = Tiles[index];
  40. return true;
  41. }
  42. public TiledMapTile GetTile(ushort x, ushort y)
  43. {
  44. var index = GetTileIndex(x, y);
  45. return Tiles[index];
  46. }
  47. public void SetTile(ushort x, ushort y, uint globalIdentifier)
  48. {
  49. var index = GetTileIndex(x, y);
  50. Tiles[index] = new TiledMapTile(globalIdentifier, x, y);
  51. }
  52. public void RemoveTile(ushort x, ushort y)
  53. {
  54. SetTile(x, y, 0);
  55. }
  56. }
  57. }