TilemapObjectLayer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 containing drawable objects.
  8. /// </summary>
  9. public class TilemapObjectLayer : TilemapLayer
  10. {
  11. private readonly List<TilemapObject> _objects = new();
  12. /// <summary>
  13. /// Gets the collection of objects in this layer.
  14. /// </summary>
  15. public IReadOnlyList<TilemapObject> Objects
  16. {
  17. get
  18. {
  19. return _objects;
  20. }
  21. }
  22. /// <summary>
  23. /// Gets or sets the rendering order for objects in this layer.
  24. /// </summary>
  25. public TilemapObjectDrawOrder DrawOrder { get; set; }
  26. /// <inheritdoc/>
  27. public override Rectangle Bounds
  28. {
  29. get
  30. {
  31. if (_objects.Count == 0)
  32. {
  33. return Rectangle.Empty;
  34. }
  35. // Calculate bounds as the union of all object bounds
  36. RectangleF? unionBounds = null;
  37. foreach (TilemapObject obj in _objects)
  38. {
  39. RectangleF objBounds = obj.Bounds;
  40. if (unionBounds == null)
  41. {
  42. unionBounds = objBounds;
  43. }
  44. else
  45. {
  46. unionBounds = RectangleF.Union(unionBounds.Value, objBounds);
  47. }
  48. }
  49. RectangleF bounds = unionBounds.Value;
  50. return new Rectangle(
  51. (int)bounds.X,
  52. (int)bounds.Y,
  53. (int)Math.Ceiling(bounds.Width),
  54. (int)Math.Ceiling(bounds.Height)
  55. );
  56. }
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="TilemapObjectLayer"/> class.
  60. /// </summary>
  61. /// <param name="name">The name of the layer.</param>
  62. public TilemapObjectLayer(string name) : base(name)
  63. {
  64. DrawOrder = TilemapObjectDrawOrder.TopDown;
  65. }
  66. /// <summary>
  67. /// Adds an object to the layer.
  68. /// </summary>
  69. /// <param name="obj">The object to add.</param>
  70. public void AddObject(TilemapObject obj)
  71. {
  72. _objects.Add(obj);
  73. }
  74. /// <summary>
  75. /// Removes an object from the layer.
  76. /// </summary>
  77. /// <param name="obj">The object to remove.</param>
  78. /// <returns><see langword="true"/> if the object was removed; otherwise, <see langword="false"/>.</returns>
  79. public bool RemoveObject(TilemapObject obj)
  80. {
  81. return _objects.Remove(obj);
  82. }
  83. /// <summary>
  84. /// Gets the object with the specified ID.
  85. /// </summary>
  86. /// <param name="id">The object ID.</param>
  87. /// <returns>The object with the specified ID, or <see langword="null"/> if not found.</returns>
  88. public TilemapObject GetObject(int id)
  89. {
  90. foreach (TilemapObject obj in _objects)
  91. {
  92. if (obj.Id == id)
  93. {
  94. return obj;
  95. }
  96. }
  97. return null;
  98. }
  99. /// <summary>
  100. /// Gets all objects within the specified rectangular region.
  101. /// </summary>
  102. /// <param name="region">The region in world coordinates.</param>
  103. /// <returns>An enumerable of objects within the region.</returns>
  104. public IEnumerable<TilemapObject> GetObjectsInRegion(RectangleF region)
  105. {
  106. foreach (TilemapObject obj in _objects)
  107. {
  108. if (region.Intersects(obj.Bounds))
  109. {
  110. yield return obj;
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Gets all objects of the specified type.
  116. /// </summary>
  117. /// <typeparam name="T">The type of objects to retrieve.</typeparam>
  118. /// <returns>An enumerable of objects of the specified type.</returns>
  119. public IEnumerable<T> GetObjects<T>() where T : TilemapObject
  120. {
  121. foreach (TilemapObject obj in _objects)
  122. {
  123. if (obj is T typedObj)
  124. {
  125. yield return typedObj;
  126. }
  127. }
  128. }
  129. }
  130. }