diff --git a/Toy b/Toy index e9b347a..1064b69 160000 --- a/Toy +++ b/Toy @@ -1 +1 @@ -Subproject commit e9b347acb68914a05707f1388d18d645b0a18515 +Subproject commit 1064b69d04aed5e5bb95ce5670a2def8b6d30c6a diff --git a/assets/scripts/tilemap/layer-background.toy b/assets/scripts/tilemap/layer-background.toy index 585adb5..16c0d6e 100644 --- a/assets/scripts/tilemap/layer-background.toy +++ b/assets/scripts/tilemap/layer-background.toy @@ -1,4 +1,5 @@ //this is one layer +import standard; import node; var childCounter: int = 0; @@ -10,6 +11,9 @@ var tileHeight: float const = 100; var roomWidth: float const = 10; var roomHeight: float const = 10; +var levelXCount: int const = 4; +var levelYCount: int const = 4; + //util to generate and init a child node of a given parent fn makeChildSprite(parent: opaque, spriteName: string) { @@ -31,22 +35,37 @@ fn onInit(node: opaque) { node.makeChildSprite("tile-background.png"); } -fn drawRoom(node: opaque, x, y, depth, camX, camY, camW, camH) { - //the modifier ratio to move things + +fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { + //calc the modifier ratio to offset things var mod: float = float tileWidth / (tileWidth - depth); - var tileWidth_mod = tileWidth * mod; - var tileHeight_mod = tileHeight * mod; + var tileWidth_mod = round(tileWidth * mod); + var tileHeight_mod = round(tileHeight * mod); var camX_mod = (camX - camW) * mod + camW / 2; var camY_mod = (camY - camH) * mod + camH / 2; - for (var j: int = 0; j < roomHeight; j++) { - for (var i: int = 0; i < roomWidth; i++) { - node.getNodeChild(0).drawNode(round( (x * roomWidth + i) * tileWidth_mod + camX_mod ),round( (y * roomHeight + j) * tileHeight_mod + camY_mod ), round( tileWidth_mod ), round( tileHeight_mod )); + //calc the region to render + var lowerX = round((camX - camW/2) / tileWidth); + var upperX = round((camX - camW*1.5) / tileWidth); + var lowerY = round((camY - camH/2) / tileHeight); + var upperY = round((camY - camH*1.5) / tileHeight); + + //bounds check + lowerX = max(0, abs(lowerX)); + upperX = min(upperX < 0 ? abs(upperX) : 0, levelXCount * roomWidth); + lowerY = max(0, abs(lowerY)); + upperY = min(upperY < 0 ? abs(upperY) : 0, levelYCount * roomHeight); + + //render each tile + for (var j = lowerY; j <= upperY; j++) { + for (var i = lowerX; i <= upperX; i++) { + node.getNodeChild(0).drawNode(round(camX_mod + i * tileWidth_mod), round(camY_mod + j * tileHeight_mod), tileWidth_mod, tileHeight_mod); } } } +//math utils fn round(x): int { var f = floor(x); return x - f >= 0.5 ? f + 1 : f; diff --git a/assets/scripts/tilemap/layer-walls.toy b/assets/scripts/tilemap/layer-walls.toy index 47404a7..9262ac1 100644 --- a/assets/scripts/tilemap/layer-walls.toy +++ b/assets/scripts/tilemap/layer-walls.toy @@ -1,4 +1,5 @@ //this is one layer +import standard; import node; var childCounter: int = 0; @@ -10,6 +11,9 @@ var tileHeight: float const = 100; var roomWidth: float const = 10; var roomHeight: float const = 10; +var levelXCount: int const = 4; +var levelYCount: int const = 4; + //util to generate and init a child node of a given parent fn makeChildSprite(parent: opaque, spriteName: string) { @@ -31,36 +35,42 @@ fn onInit(node: opaque) { node.makeChildSprite("tile-wall.png"); } -//draw the parallax-style walls -fn drawRoom(node: opaque, x, y, depth, camX, camY, camW, camH) { - var mod: float = float tileWidth / (tileWidth - depth); - var tileWidth_mod = tileWidth * mod; - var tileHeight_mod = tileHeight * mod; +fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { + //calc the modifier ratio to offset things + var mod: float = float tileWidth / (tileWidth - depth); + + var tileWidth_mod = round(tileWidth * mod); + var tileHeight_mod = round(tileHeight * mod); var camX_mod = (camX - camW) * mod + camW / 2; var camY_mod = (camY - camH) * mod + camH / 2; - //top - for (var i = x * roomWidth; i < (x * roomWidth) + roomWidth; i++) { - node.getNodeChild(0).drawNode(round( i * tileWidth_mod + camX_mod ), round(y * roomHeight * tileHeight_mod + camY_mod ), round( tileWidth_mod), round( tileHeight_mod )); - } + //calc the region to render + var lowerX = round((camX - camW/2) / tileWidth); + var upperX = round((camX - camW*1.5) / tileWidth); + var lowerY = round((camY - camH/2) / tileHeight); + var upperY = round((camY - camH*1.5) / tileHeight); - //left - for (var j = y * roomHeight; j < (y * roomHeight) + roomHeight; j++) { - node.getNodeChild(0).drawNode(round( x * roomWidth * tileWidth_mod + camX_mod ), round( j * tileHeight_mod + camY_mod ), round( tileWidth_mod), round( tileHeight_mod )); - } + //bounds check + lowerX = max(0, abs(lowerX)); + upperX = min(upperX < 0 ? abs(upperX) : 0, levelXCount * roomWidth); + lowerY = max(0, abs(lowerY)); + upperY = min(upperY < 0 ? abs(upperY) : 0, levelYCount * roomHeight); - //bottom - for (var i = x * roomWidth; i < (x * roomWidth) + roomWidth; i++) { - node.getNodeChild(0).drawNode(round( i * tileWidth_mod + camX_mod), round( ((y+1) * roomHeight -1) * tileHeight_mod + camY_mod), round( tileWidth_mod), round( tileHeight_mod)); - } + //render each tile + for (var j = lowerY; j <= upperY; j++) { + for (var i = lowerX; i <= upperX; i++) { + if ( !(int i % int roomWidth == 0 || int i % int roomWidth == roomWidth - 1) && !(int j % int roomHeight == 0 || int j % int roomHeight == roomHeight - 1) ) { + continue; + } - //right - for (var j = y * roomHeight; j < (y * roomHeight) + roomHeight; j++) { - node.getNodeChild(0).drawNode(round( ((x + 1) * roomWidth -1) * tileWidth_mod + camX_mod ), round( j * tileHeight_mod + camY_mod ), round( tileWidth_mod ), round( tileHeight_mod )); + node.getNodeChild(0).drawNode(round(camX_mod + i * tileWidth_mod), round(camY_mod + j * tileHeight_mod), tileWidth_mod, tileHeight_mod); + } } } + +//math utils fn round(x): int { var f = floor(x); return x - f >= 0.5 ? f + 1 : f; diff --git a/assets/scripts/tilemap/tilemap.toy b/assets/scripts/tilemap/tilemap.toy index d6b6e16..0c7876f 100644 --- a/assets/scripts/tilemap/tilemap.toy +++ b/assets/scripts/tilemap/tilemap.toy @@ -47,31 +47,13 @@ fn onDraw(node: opaque) { print stepCounter; stepCounter = 0; - //TODO: reference the camera width & height - //TODO: render parallax - - //cull out-of-bounds regions - var lowerX = abs(floor(floor((camX-screenWidth/2) / tileWidth) / float roomWidth)); - var upperX = abs(ceil(floor((camX-screenWidth*1.5) / float tileWidth) / float roomWidth)); - - var lowerY = abs(floor(floor((camY-screenHeight/2) / tileHeight) / float roomHeight)); - var upperY = abs(ceil(floor((camY-screenHeight*1.5) / float tileHeight) / float roomHeight)); - - //bounds check - lowerX = max(lowerX, 0); - upperX = min(upperX + 1, levelXCount); - lowerY = max(lowerY, 0); - upperY = min(upperY + 1, levelYCount); - + //iterate over each layer, passing in the screen dimensions for (var c = 0; c < childCounter; c++) { - for (var j = lowerY; j <= upperY; j++) { - for (var i = lowerX; i <= upperX; i++) { - node.getNodeChild(c).callNodeFn("drawRoom", i, j, c * 4.0, camX, camY, screenWidth, screenHeight); - } - } + node.getNodeChild(c).callNodeFn("drawLayer", camX, camY, screenWidth, screenHeight, c * 2); } } +//math utils fn round(x): int { var f = floor(x); return x - f >= 0.5 ? f + 1 : f;