Let behaviors read and set tilemap cells while the game runs (#135)
A behavior could move sprites around but had no way to look at or
change the tilemap it was standing on. Doors, breakable blocks and
"is there ground ahead?" checks all need that.
- New nodes Tile at, Set tile, World to cell and Cell to world. The
conversion honours position, rotation, scale and mirroring of the
placed map. A small Tile literal node lets a graph compare what it
read against a fixed tile.
- The layer is picked by name from all tilemap layers in the project
through a new field kind. "First layer" takes the map's first one.
Because layer is an input, a text can also be wired in.
- Each placement keeps only its edited cells in liveTilemap.ts,
layered over the asset's cells. A second placement of the same map
stays untouched, and the asset itself is never written.
- A read sees a Set tile from earlier in the same flow. Drawing picks
up the edit at once and neighbouring autotile edges adapt.
- Solids see the edits straight away, so a Move in the same frame
already hits the new cell. Rapier rebuilds its shapes on the next
step.
- After Stop the map is back to what was painted. The editor run shows
the edit in the scene view while it lasts and does not touch the
project.
- An unwired tilemap input means no map, like Get position. Cells
outside the grid are refused and done reports false.
Tests:
- ./check.sh: 155 files, 2271 tests, CHECK EXIT 0 (before 2245).
- tests/core/tile-access.test.ts (14), one physics test for a floor
set at runtime, catalog entries for the five nodes.
- e2e 39,40: all passed, 86 checks (before 81), five new in 40s.
- Counter-checks: collision ignoring edits fails 3 unit tests, the
solids cache missing same-tick edits fails only the "at once" test,
drawing ignoring edits fails the 2 drawing checks, the editor run
not reporting a new tile fails only its check.
- 40s uses a still tile. The animated fixture tile redraws every frame
and hid the last counter-check at first.
- Check total 1351 + 5 = 1356, computed. CI confirmed 1351 at cb32b4b.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cb32b4bc23
commit
d444b27fae
22 files changed
+848
-20
No files matched your search
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* A placed tilemap as a running game sees it (#135). A behavior may read and change its cells, but
|
||||
* the cells belong to the asset, and two placements of one map must not paint each other. So a run
|
||||
* keeps what it changed per placement, as edits laid over the asset's cells, and never writes them
|
||||
* back: after Stop the map is the one that was painted.
|
||||
*/
|
||||
import type { TilemapAsset, TilemapLayer } from './Asset';
|
||||
import { apply, determinant, invert, type Mat } from './matrix';
|
||||
import type { PropertyBag, Vec2 } from './Properties';
|
||||
import { type Cell, cellAt, cellAtPoint, cellKey, cellOrigin, inGrid, tilemapTileSize } from './tilemap';
|
||||
|
||||
/**
|
||||
* What a run changed on one placement: per layer id, the cells it set, `''` for one it emptied.
|
||||
* Replaced rather than changed in place, so whoever worked something out from the old one can tell.
|
||||
*/
|
||||
export type TileEdits = Readonly<Record<string, Readonly<Record<string, string>>>>;
|
||||
|
||||
/** How a node's empty layer choice reads: the first layer of whatever map it is given. */
|
||||
export const FIRST_TILE_LAYER = 'First layer';
|
||||
|
||||
/**
|
||||
* The layer a node names. By name, not by index, since the index moves when the layers are
|
||||
* reordered; no name is the first layer, which is all a map of one layer needs. Two layers of one
|
||||
* name are the upper one in the list, the same one the panel shows first.
|
||||
*/
|
||||
export function layerNamed(asset: TilemapAsset, name: string): TilemapLayer | undefined {
|
||||
return name === '' ? asset.layers[0] : asset.layers.find((layer) => layer.name === name);
|
||||
}
|
||||
|
||||
/** The asset's layers with a run's edits laid over them – what is drawn and what blocks. */
|
||||
export function liveLayers(asset: TilemapAsset, edits: TileEdits | undefined): TilemapLayer[] {
|
||||
if (!edits) return asset.layers;
|
||||
return asset.layers.map((layer) => {
|
||||
const changed = edits[layer.id];
|
||||
if (!changed) return layer;
|
||||
const cells: Record<string, string> = { ...layer.cells };
|
||||
for (const [key, tileId] of Object.entries(changed)) {
|
||||
if (tileId) cells[key] = tileId;
|
||||
else delete cells[key];
|
||||
}
|
||||
return { ...layer, cells };
|
||||
});
|
||||
}
|
||||
|
||||
/** The asset as a run sees it: the same one when nothing was changed. */
|
||||
export function liveTilemap(asset: TilemapAsset, edits: TileEdits | undefined): TilemapAsset {
|
||||
return edits ? { ...asset, layers: liveLayers(asset, edits) } : asset;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tile in a cell right now, `''` for an empty one. Null when there is nothing to ask: no layer
|
||||
* of that name, or a cell outside the grid.
|
||||
*/
|
||||
export function liveTileAt(
|
||||
asset: TilemapAsset,
|
||||
edits: TileEdits | undefined,
|
||||
props: PropertyBag,
|
||||
layerName: string,
|
||||
cell: Cell,
|
||||
): string | null {
|
||||
const layer = layerNamed(asset, layerName);
|
||||
if (!layer || !inGrid(props, cell.x, cell.y)) return null;
|
||||
const edited = edits?.[layer.id]?.[cellKey(cell.x, cell.y)];
|
||||
return edited ?? cellAt(layer, cell.x, cell.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* The edits with one more cell set, `''` emptying it; null when the cell cannot be set (see
|
||||
* `liveTileAt`). Setting what is already there gives the same edits back, so nothing is rebuilt.
|
||||
*/
|
||||
export function withTile(
|
||||
asset: TilemapAsset,
|
||||
edits: TileEdits | undefined,
|
||||
props: PropertyBag,
|
||||
layerName: string,
|
||||
cell: Cell,
|
||||
tileId: string,
|
||||
): TileEdits | null {
|
||||
const now = liveTileAt(asset, edits, props, layerName, cell);
|
||||
if (now === null) return null;
|
||||
if (now === tileId) return edits ?? {};
|
||||
const layer = layerNamed(asset, layerName)!;
|
||||
return { ...edits, [layer.id]: { ...edits?.[layer.id], [cellKey(cell.x, cell.y)]: tileId } };
|
||||
}
|
||||
|
||||
/**
|
||||
* The cell a world point falls into, and whether that cell is on the grid. The map may be moved,
|
||||
* turned, scaled and mirrored, so the point is taken into the map's own frame first; a map scaled
|
||||
* to nothing has no cells at all.
|
||||
*/
|
||||
export function worldToCell(props: PropertyBag, m: Mat, point: Vec2): Cell & { inside: boolean } {
|
||||
if (Math.abs(determinant(m)) < 1e-12) return { x: 0, y: 0, inside: false };
|
||||
const local = apply(invert(m), point);
|
||||
const cell = cellAtPoint(props, local.x, local.y);
|
||||
return { ...cell, inside: inGrid(props, cell.x, cell.y) };
|
||||
}
|
||||
|
||||
/** The middle of a cell in world units – where something standing on that cell is put. */
|
||||
export function cellToWorld(props: PropertyBag, m: Mat, cell: Cell): Vec2 {
|
||||
const half = tilemapTileSize(props) / 2;
|
||||
const corner = cellOrigin(props, cell.x, cell.y);
|
||||
return apply(m, { x: corner.x + half, y: corner.y + half });
|
||||
}
|
||||
@@ -151,6 +151,17 @@ export function layerTiles(project: Project, props: PropertyBag, layer: TilemapL
|
||||
return tiles.sort((a, b) => a.y - b.y || a.x - b.x);
|
||||
}
|
||||
|
||||
/**
|
||||
* The layer names of every tilemap in the project, each once, in asset and layer order. A node
|
||||
* picks a layer by name before it knows which map it will be given (#135), so it is offered them all.
|
||||
*/
|
||||
export function tilemapLayerNames(project: Project): string[] {
|
||||
const names = project.assets.flatMap((asset) =>
|
||||
asset.type === 'tilemap' ? asset.layers.map((layer) => layer.name) : [],
|
||||
);
|
||||
return [...new Set(names)];
|
||||
}
|
||||
|
||||
/** The tiles a tilemap paints with, in the order they first appear – its palette in use. */
|
||||
export function tilemapTileIds(asset: { layers: TilemapLayer[] }): string[] {
|
||||
const ids: string[] = [];
|
||||
|
||||
Reference in new issue
Block a user