Files
mrgameeng/src/MrGameEng.Tilemaps/TileSet.cs
T
Leonid Pershin b3415120c3
CI / build-test (push) Failing after 1m4s
Add MrGameEng.Tilemaps: code-built tile grids rendered through the batcher
TileSet maps ids to texture regions with tints (0 = empty), TileGrid is a
bounds-checked dense ushort grid, and the Tilemap component places a grid
in the world per render layer. UseTilemaps() inserts TilemapRenderSystem
before the flush; it submits only the camera-visible cell range
(TilemapMath.VisibleCells), so frame cost scales with the screen, not the
grid. Demonstrated in the sample as a checkerboard floor; Tiled loading
stays on the roadmap on top of this API.
2026-06-11 10:24:47 +03:00

45 lines
1.6 KiB
C#

using Microsoft.Xna.Framework;
using MrGameEng.Graphics;
namespace MrGameEng.Tilemaps;
/// <summary>One tile kind: a texture region plus a tint multiplier.</summary>
/// <param name="Region">Texture region the tile is drawn with.</param>
/// <param name="Color">Tint, multiplied with the texture. White = unmodified.</param>
public readonly record struct TileDef(Texture2DRegion Region, Color Color)
{
/// <summary>Creates an untinted tile.</summary>
public TileDef(Texture2DRegion region)
: this(region, Color.White)
{
}
}
/// <summary>
/// Maps tile ids to <see cref="TileDef"/>s. Built in code: every <see cref="Add"/> returns
/// the id to store in a <see cref="TileGrid"/>. Id 0 is reserved for "empty" (nothing drawn).
/// </summary>
public sealed class TileSet
{
private readonly List<TileDef> _tiles = [default];
/// <summary>Number of defined tiles including the reserved empty tile 0.</summary>
public int Count => _tiles.Count;
/// <summary>The tile definition for <paramref name="id"/>. Id 0 has no region.</summary>
public TileDef this[int id] => _tiles[id];
/// <summary>Defines a tile and returns its id (1, 2, …).</summary>
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);
}
}