using System;
namespace MonoGame.Extended.Tilemaps
{
///
/// Represents a tile instance in a tile layer.
///
///
/// A tile references a tileset and local tile ID through its global ID,
/// and may include flip flags for horizontal, vertical, or diagonal transformations.
///
public readonly struct TilemapTile
{
///
/// Gets the global tile ID.
///
///
/// The global ID uniquely identifies a tile across all tilesets in the tilemap.
/// It combines the tileset's first global ID with the local tile ID.
///
public int GlobalId { get; }
///
/// Gets the flip transformation flags applied to this tile.
///
public TilemapTileFlipFlags FlipFlags { get; }
///
/// Initializes a new instance of the struct.
///
/// The global tile ID.
/// The flip transformation flags.
public TilemapTile(int globalId, TilemapTileFlipFlags flipFlags = TilemapTileFlipFlags.None)
{
GlobalId = globalId;
FlipFlags = flipFlags;
}
///
/// Gets the tileset that contains this tile.
///
/// The collection of tilesets.
/// The tileset containing this tile, or if not found.
public TilemapTileset GetTileset(TilemapTilesetCollection tilesets)
{
return tilesets.GetTilesetForGid(GlobalId);
}
///
/// Gets the local tile ID within the tileset.
///
/// The collection of tilesets.
/// The local tile ID.
public int GetLocalId(TilemapTilesetCollection tilesets)
{
return tilesets.GetLocalId(GlobalId, out _);
}
///
/// Gets the local tile ID within the specified tileset.
///
/// The collection of tilesets.
/// When this method returns, contains the tileset that owns this tile.
/// The local tile ID within the tileset.
public int GetLocalId(TilemapTilesetCollection tilesets, out TilemapTileset tileset)
{
return tilesets.GetLocalId(GlobalId, out tileset);
}
public TilemapTileData? GetTileData(TilemapTilesetCollection tilesets)
{
var tileset = GetTileset(tilesets);
if (tileset == null)
{
return null;
}
var localId = GlobalId - tileset.FirstGlobalId;
return tileset.GetTileData(localId);
}
}
}