using Microsoft.Xna.Framework;
using MrGameEng.Graphics;
namespace MrGameEng.Tilemaps;
/// One tile kind: a texture region plus a tint multiplier.
/// Texture region the tile is drawn with.
/// Tint, multiplied with the texture. White = unmodified.
public readonly record struct TileDef(Texture2DRegion Region, Color Color)
{
/// Creates an untinted tile.
public TileDef(Texture2DRegion region)
: this(region, Color.White)
{
}
}
///
/// Maps tile ids to s. Built in code: every returns
/// the id to store in a . Id 0 is reserved for "empty" (nothing drawn).
///
public sealed class TileSet
{
private readonly List _tiles = [default];
/// Number of defined tiles including the reserved empty tile 0.
public int Count => _tiles.Count;
/// The tile definition for . Id 0 has no region.
public TileDef this[int id] => _tiles[id];
/// Defines a tile and returns its id (1, 2, …).
public ushort Add(Texture2DRegion region, Color? color = null)
{
ArgumentNullException.ThrowIfNull(region);
if (_tiles.Count > ushort.MaxValue)
{
throw new InvalidOperationException("A TileSet holds at most 65535 tiles.");
}
_tiles.Add(new TileDef(region, color ?? Color.White));
return (ushort)(_tiles.Count - 1);
}
}