| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace MonoGame.Extended.Tilemaps
- {
- /// <summary>
- /// A collection of tilesets used by a tilemap.
- /// </summary>
- public class TilemapTilesetCollection : IReadOnlyList<TilemapTileset>
- {
- private readonly List<TilemapTileset> _tilesets = new();
- /// <summary>
- /// Gets the tileset at the specified index.
- /// </summary>
- /// <param name="index">The zero-based index of the tileset to get.</param>
- /// <returns>The tileset at the specified index.</returns>
- public TilemapTileset this[int index]
- {
- get
- {
- return _tilesets[index];
- }
- }
- /// <summary>
- /// Gets the number of tilesets in the collection.
- /// </summary>
- public int Count
- {
- get
- {
- return _tilesets.Count;
- }
- }
- /// <summary>
- /// Adds a tileset to the collection.
- /// </summary>
- /// <param name="tileset">The tileset to add.</param>
- /// <remarks>
- /// This method is not thread-safe. If multiple threads are accessing the collection,
- /// external synchronization is required.
- /// </remarks>
- public void Add(TilemapTileset tileset)
- {
- if (tileset == null)
- throw new ArgumentNullException(nameof(tileset));
- _tilesets.Add(tileset);
- }
- /// <summary>
- /// Removes a tileset from the collection.
- /// </summary>
- /// <param name="tileset">The tileset to remove.</param>
- /// <returns><see langword="true"/> if the tileset was removed; otherwise, <see langword="false"/>.</returns>
- /// <remarks>
- /// This is an O(n) operation. This method is not thread-safe.
- /// </remarks>
- public bool Remove(TilemapTileset tileset)
- {
- return _tilesets.Remove(tileset);
- }
- /// <summary>
- /// Removes all tilesets from the collection.
- /// </summary>
- /// <remarks>
- /// <strong>Warning:</strong> This operation cannot be undone and will remove all tileset references.
- /// This method is not thread-safe.
- /// </remarks>
- public void Clear()
- {
- _tilesets.Clear();
- }
- /// <summary>
- /// Gets the tileset that contains the specified global tile ID.
- /// </summary>
- /// <param name="globalTileId">The global tile ID.</param>
- /// <returns>The tileset containing the tile, or <see langword="null"/> if not found.</returns>
- public TilemapTileset GetTilesetForGid(int globalTileId)
- {
- // Tilesets are ordered by FirstGlobalId
- // Find the tileset where: globalTileId >= FirstGlobalId
- // and globalTileId < next tileset's FirstGlobalId
- TilemapTileset result = null;
- for (int i = 0; i < _tilesets.Count; i++)
- {
- var tileset = _tilesets[i];
- // Check if this tile ID belongs to this tileset
- if (globalTileId >= tileset.FirstGlobalId)
- {
- // Check if this GID is within the tileset's range
- int localId = globalTileId - tileset.FirstGlobalId;
- if(localId < tileset.TileCount)
- {
- result = tileset;
- break;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// Gets the local tile ID within a tileset from a global tile ID.
- /// </summary>
- /// <param name="globalTileId">The global tile ID.</param>
- /// <param name="tileset">When this method returns, contains the tileset that owns the tile.</param>
- /// <returns>The local tile ID within the tileset.</returns>
- /// <exception cref="InvalidOperationException">No tileset contains the specified global tile ID.</exception>
- public int GetLocalId(int globalTileId, out TilemapTileset tileset)
- {
- var foundTileset = GetTilesetForGid(globalTileId);
- if (foundTileset == null)
- {
- throw new InvalidOperationException($"No tileset found for global tile ID {globalTileId}.");
- }
- tileset = foundTileset;
- return globalTileId - foundTileset.FirstGlobalId;
- }
- public IEnumerator<TilemapTileset> GetEnumerator()
- {
- return _tilesets.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
|