Loads a map and draws it: one call per layer, however many cells it has.
A layer's cells go up as one batch of corners, each one already in its place and carrying the piece
of the sheet it shows. That is what makes one call possible: a sprite says "this picture, here",
and a thousand cells showing thirty different pieces cannot be said that way. A sprite per cell
would cost a place, a colour and a picture per tile, and stops being sensible somewhere around the
first map that is not tiny.
What comes back is 'loading' and fills itself in; its layers start drawing once the file and its
sheet have arrived, so nothing has to be awaited. useLoadProgress counts it like any other asset.
A level generated in code has no file: give doc (what a .tilemap holds) and atlas (its
sheet, as an object) instead of src. It is ready at once when the sheet is, and everything else
works the same: useTileBody2d, solidAt, setTile, useCameraFollow(hero, { bounds: map }). It is in
no cache, so each call makes a map of its own, and it goes with what asked for it.
Call it in the body of a scene, like everything else. What it draws goes away with that scene.
Example
export const Level: TSceneFn = () => {
const map = createTilemap({ src: '/maps/level1.tilemap' });
const player = createSprite({ key: 'hero', zIndex: 0 });
useUpdate((delta) => {
// The ground layer is drawn under the player and the treetops over them, because the
// file says 'under' and 'over' and the player never mentioned a number at all.
if (!solidAtPoint(map, player.transform.x + 8, player.transform.y)) {
player.transform.x += 60 * delta;
}
});
return createScene();
};
Example
declare const grass: string[];
declare const ledge: string[];
declare const palette: TPixelPalette;
const tiles = sheetFromRows({ grass, ledge }, palette);
const map = createTilemap({
doc: {
cell: 8, width: 4, height: 2,
tiles: { 1: { frame: tiles.frames.grass, solid: true }, 2: { frame: tiles.frames.ledge, oneWay: true } },
layers: [{ name: 'ground', order: 'under', data: [0, 2, 2, 0, 1, 1, 1, 1] }],
},
atlas: createSpriteAtlas({ texture: createPixelTexture(tiles.pixels), columns: tiles.columns }),
});