From 1287556e55c361473ee1eb61f373d56080203f1b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 3 Jul 2023 20:00:01 +1000 Subject: [PATCH] Where there's a will there's a way --- Box | 2 +- assets/scripts/scene.toy | 2 +- assets/scripts/tilemap/renderer-child.toy | 28 +++++++++++ .../tilemap/{rendernode.toy => renderer.toy} | 47 +++++++++++-------- 4 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 assets/scripts/tilemap/renderer-child.toy rename assets/scripts/tilemap/{rendernode.toy => renderer.toy} (59%) diff --git a/Box b/Box index dcbb2d1..8b543dc 160000 --- a/Box +++ b/Box @@ -1 +1 @@ -Subproject commit dcbb2d12f224b38ae7fdbcb943dacfefe94d08ab +Subproject commit 8b543dcb821c50ad29d10d9d39fb8cadc8ccd6ab diff --git a/assets/scripts/scene.toy b/assets/scripts/scene.toy index e1d05d2..b74657b 100644 --- a/assets/scripts/scene.toy +++ b/assets/scripts/scene.toy @@ -22,7 +22,7 @@ fn onInit(node: opaque) { generateLevel(rng); //generate the child node to render the map - var child: opaque = node.loadChild("scripts:/tilemap/rendernode.toy"); + var child: opaque = node.loadChild("scripts:/tilemap/renderer.toy"); child.callNodeFn("setTilemap", tilemap); } diff --git a/assets/scripts/tilemap/renderer-child.toy b/assets/scripts/tilemap/renderer-child.toy new file mode 100644 index 0000000..b2106d2 --- /dev/null +++ b/assets/scripts/tilemap/renderer-child.toy @@ -0,0 +1,28 @@ +//use this dummy child to actually draw to the screen +import standard; +import node; + +//lifecycle functions +fn onLoad(node: opaque) { + node.setNodeScaleX(CAMERA_SCALE_X); + node.setNodeScaleY(CAMERA_SCALE_Y); +} + +fn onDraw(node: opaque) { + var posX: int const = node.getNodeWorldPositionX(); + var posY: int const = node.getNodeWorldPositionY(); + + //draw everything at twice the original size + var scaleX: float const = node.getNodeWorldScaleX(); + var scaleY: float const = node.getNodeWorldScaleY(); + + var width: int const = node.getNodeRectW(); + var height: int const = node.getNodeRectH(); + + node.drawNode( + floor(posX * scaleX) + globalCameraX, + floor(posY * scaleY) + globalCameraY, + floor(width * scaleX), + floor(height * scaleY) + ); +} \ No newline at end of file diff --git a/assets/scripts/tilemap/rendernode.toy b/assets/scripts/tilemap/renderer.toy similarity index 59% rename from assets/scripts/tilemap/rendernode.toy rename to assets/scripts/tilemap/renderer.toy index 2c83c68..83635cc 100644 --- a/assets/scripts/tilemap/rendernode.toy +++ b/assets/scripts/tilemap/renderer.toy @@ -1,3 +1,7 @@ +import standard; +import engine; +import node; + //constants for generating maps var ROOM_MIN_WIDTH: int const = 4; //minimum safe value 4 var ROOM_MIN_HEIGHT: int const = 4; @@ -13,29 +17,23 @@ var CELL_COUNT_Y: int const = 3; var tilemap: [int] = null; -fn setTilemap(node: opaque, t: [int]) { - tilemap = t; -} - //lifecycle functions fn onLoad(node: opaque) { + //load the atlas into this node node.loadNodeTexture("sprites:/tileset.png"); - - node.setNodeScaleX(CAMERA_SCALE_X); - node.setNodeScaleY(CAMERA_SCALE_Y); } -fn onDraw(node: opaque) { - assert tilemap, "tilemap is null"; +fn setTilemap(node: opaque, t: [int]) { + assert t, "provided tilemap is null (in setTilemap)"; - var posX: int const = node.getNodeWorldPositionX(); - var posY: int const = node.getNodeWorldPositionY(); + tilemap = t; - //draw everything at twice the original size - var scaleX: float const = node.getNodeWorldScaleX(); - var scaleY: float const = node.getNodeWorldScaleY(); + //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 + //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 @@ -52,11 +50,22 @@ fn onDraw(node: opaque) { //draw to the screen node.drawNode( - floor((i * TILE_PIXEL_WIDTH + posX) * scaleX) + globalCameraX, - floor((j * TILE_PIXEL_HEIGHT + posY) * scaleY) + globalCameraY, - floor(TILE_PIXEL_WIDTH * scaleX), - floor(TILE_PIXEL_HEIGHT * scaleY) + (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; } \ No newline at end of file