using System; using System.Collections.Generic; using Microsoft.Xna.Framework; namespace MonoGame.Extended.Tilemaps { /// /// Represents a layer that contains other layers as children. /// /// /// Group layers allow for hierarchical organization of layers and can apply /// transformations and effects to all child layers collectively. /// public class TilemapGroupLayer : TilemapLayer { private readonly List _childLayers = new List(); /// /// Gets the collection of child layers. /// public IReadOnlyList ChildLayers { get { return _childLayers; } } /// public override Rectangle Bounds { get { if (_childLayers.Count == 0) { return Rectangle.Empty; } // Calculate bounds as the union of all child layer bounds Rectangle bounds = _childLayers[0].Bounds; for (int i = 1; i < _childLayers.Count; i++) { bounds = Rectangle.Union(bounds, _childLayers[i].Bounds); } return bounds; } } /// /// Initializes a new instance of the class. /// /// The name of the layer. public TilemapGroupLayer(string name) : base(name) { } /// /// Adds a child layer to this group. /// /// The layer to add. public void AddLayer(TilemapLayer layer) { _childLayers.Add(layer); } /// /// Removes a child layer from this group. /// /// The layer to remove. /// if the layer was removed; otherwise, . public bool RemoveLayer(TilemapLayer layer) { return _childLayers.Remove(layer); } /// /// Gets the child layer with the specified name. /// /// The name of the layer. /// The layer with the specified name, or if not found. /// /// This method searches recursively through all child layers and their descendants. /// public TilemapLayer GetLayer(string name) { if (string.IsNullOrEmpty(name)) { return null; } // Search immediate children first foreach (TilemapLayer layer in _childLayers) { if (layer.Name == name) { return layer; } } // Recursively search in child group layers foreach (TilemapLayer layer in _childLayers) { if (layer is TilemapGroupLayer groupLayer) { TilemapLayer found = groupLayer.GetLayer(name); if (found != null) { return found; } } } return null; } } }