Files
Airport/assets/scripts/tilemap/renderer.toy
2023-07-03 20:40:30 +10:00

58 lines
1.5 KiB
Plaintext

import standard;
import engine;
import node;
var tilemap: [int] = null;
//lifecycle functions
fn onLoad(node: opaque) {
//load the atlas into this node
node.loadNodeTexture("sprites:/tileset.png");
}
fn setTilemap(node: opaque, t: [int]) {
assert t, "provided tilemap is null (in setTilemap)";
tilemap = t;
//create a child as a render target
var child = node.loadChild("scripts:/tilemap/renderer-child.toy");
child.createNodeTexture(CELL_WIDTH * CELL_COUNT_X * TILE_PIXEL_WIDTH, CELL_HEIGHT * CELL_COUNT_Y * TILE_PIXEL_HEIGHT);
setRenderTarget(child);
//draw the tilemap to the child
for (var j = 0; j < CELL_HEIGHT * CELL_COUNT_Y; j++) {
for (var i = 0; i < CELL_WIDTH * CELL_COUNT_X; i++) {
//don't render empty tiles
if (tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3] < 0) {
continue;
}
//set the rect of the node on the tilesheet - the "tilemap" var is a single blob of data
node.setNodeRect(
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3] * TILE_PIXEL_WIDTH,
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 1] * TILE_PIXEL_HEIGHT,
TILE_PIXEL_WIDTH, TILE_PIXEL_HEIGHT
);
//draw to the screen
node.drawNode(
(i * TILE_PIXEL_WIDTH),
(j * TILE_PIXEL_HEIGHT),
TILE_PIXEL_WIDTH,
TILE_PIXEL_HEIGHT
);
}
}
//reset the render target to the screen
setRenderTarget(null);
}
//polyfills
fn loadChild(parent: opaque, fname: string) {
//TODO: add this to the API proper
var child: opaque = loadNode(fname);
parent.pushNode(child);
return child;
}