Working on optimising scripts
This commit is contained in:
2
Toy
2
Toy
Submodule Toy updated: e9b347acb6...1064b69d04
@@ -1,4 +1,5 @@
|
|||||||
//this is one layer
|
//this is one layer
|
||||||
|
import standard;
|
||||||
import node;
|
import node;
|
||||||
|
|
||||||
var childCounter: int = 0;
|
var childCounter: int = 0;
|
||||||
@@ -10,6 +11,9 @@ var tileHeight: float const = 100;
|
|||||||
var roomWidth: float const = 10;
|
var roomWidth: float const = 10;
|
||||||
var roomHeight: 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
|
//util to generate and init a child node of a given parent
|
||||||
fn makeChildSprite(parent: opaque, spriteName: string) {
|
fn makeChildSprite(parent: opaque, spriteName: string) {
|
||||||
@@ -31,22 +35,37 @@ fn onInit(node: opaque) {
|
|||||||
node.makeChildSprite("tile-background.png");
|
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 mod: float = float tileWidth / (tileWidth - depth);
|
||||||
|
|
||||||
var tileWidth_mod = tileWidth * mod;
|
var tileWidth_mod = round(tileWidth * mod);
|
||||||
var tileHeight_mod = tileHeight * mod;
|
var tileHeight_mod = round(tileHeight * mod);
|
||||||
var camX_mod = (camX - camW) * mod + camW / 2;
|
var camX_mod = (camX - camW) * mod + camW / 2;
|
||||||
var camY_mod = (camY - camH) * mod + camH / 2;
|
var camY_mod = (camY - camH) * mod + camH / 2;
|
||||||
|
|
||||||
for (var j: int = 0; j < roomHeight; j++) {
|
//calc the region to render
|
||||||
for (var i: int = 0; i < roomWidth; i++) {
|
var lowerX = round((camX - camW/2) / tileWidth);
|
||||||
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 ));
|
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 {
|
fn round(x): int {
|
||||||
var f = floor(x);
|
var f = floor(x);
|
||||||
return x - f >= 0.5 ? f + 1 : f;
|
return x - f >= 0.5 ? f + 1 : f;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
//this is one layer
|
//this is one layer
|
||||||
|
import standard;
|
||||||
import node;
|
import node;
|
||||||
|
|
||||||
var childCounter: int = 0;
|
var childCounter: int = 0;
|
||||||
@@ -10,6 +11,9 @@ var tileHeight: float const = 100;
|
|||||||
var roomWidth: float const = 10;
|
var roomWidth: float const = 10;
|
||||||
var roomHeight: 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
|
//util to generate and init a child node of a given parent
|
||||||
fn makeChildSprite(parent: opaque, spriteName: string) {
|
fn makeChildSprite(parent: opaque, spriteName: string) {
|
||||||
@@ -31,36 +35,42 @@ fn onInit(node: opaque) {
|
|||||||
node.makeChildSprite("tile-wall.png");
|
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;
|
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
||||||
var tileHeight_mod = tileHeight * mod;
|
//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 camX_mod = (camX - camW) * mod + camW / 2;
|
||||||
var camY_mod = (camY - camH) * mod + camH / 2;
|
var camY_mod = (camY - camH) * mod + camH / 2;
|
||||||
|
|
||||||
//top
|
//calc the region to render
|
||||||
for (var i = x * roomWidth; i < (x * roomWidth) + roomWidth; i++) {
|
var lowerX = round((camX - camW/2) / tileWidth);
|
||||||
node.getNodeChild(0).drawNode(round( i * tileWidth_mod + camX_mod ), round(y * roomHeight * tileHeight_mod + camY_mod ), round( tileWidth_mod), round( tileHeight_mod ));
|
var upperX = round((camX - camW*1.5) / tileWidth);
|
||||||
}
|
var lowerY = round((camY - camH/2) / tileHeight);
|
||||||
|
var upperY = round((camY - camH*1.5) / tileHeight);
|
||||||
|
|
||||||
//left
|
//bounds check
|
||||||
for (var j = y * roomHeight; j < (y * roomHeight) + roomHeight; j++) {
|
lowerX = max(0, abs(lowerX));
|
||||||
node.getNodeChild(0).drawNode(round( x * roomWidth * tileWidth_mod + camX_mod ), round( j * tileHeight_mod + camY_mod ), round( tileWidth_mod), round( tileHeight_mod ));
|
upperX = min(upperX < 0 ? abs(upperX) : 0, levelXCount * roomWidth);
|
||||||
}
|
lowerY = max(0, abs(lowerY));
|
||||||
|
upperY = min(upperY < 0 ? abs(upperY) : 0, levelYCount * roomHeight);
|
||||||
|
|
||||||
//bottom
|
//render each tile
|
||||||
for (var i = x * roomWidth; i < (x * roomWidth) + roomWidth; i++) {
|
for (var j = lowerY; j <= upperY; j++) {
|
||||||
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));
|
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
|
node.getNodeChild(0).drawNode(round(camX_mod + i * tileWidth_mod), round(camY_mod + j * tileHeight_mod), tileWidth_mod, tileHeight_mod);
|
||||||
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 ));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//math utils
|
||||||
fn round(x): int {
|
fn round(x): int {
|
||||||
var f = floor(x);
|
var f = floor(x);
|
||||||
return x - f >= 0.5 ? f + 1 : f;
|
return x - f >= 0.5 ? f + 1 : f;
|
||||||
|
|||||||
@@ -47,31 +47,13 @@ fn onDraw(node: opaque) {
|
|||||||
print stepCounter;
|
print stepCounter;
|
||||||
stepCounter = 0;
|
stepCounter = 0;
|
||||||
|
|
||||||
//TODO: reference the camera width & height
|
//iterate over each layer, passing in the screen dimensions
|
||||||
//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);
|
|
||||||
|
|
||||||
for (var c = 0; c < childCounter; c++) {
|
for (var c = 0; c < childCounter; c++) {
|
||||||
for (var j = lowerY; j <= upperY; j++) {
|
node.getNodeChild(c).callNodeFn("drawLayer", camX, camY, screenWidth, screenHeight, c * 2);
|
||||||
for (var i = lowerX; i <= upperX; i++) {
|
|
||||||
node.getNodeChild(c).callNodeFn("drawRoom", i, j, c * 4.0, camX, camY, screenWidth, screenHeight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//math utils
|
||||||
fn round(x): int {
|
fn round(x): int {
|
||||||
var f = floor(x);
|
var f = floor(x);
|
||||||
return x - f >= 0.5 ? f + 1 : f;
|
return x - f >= 0.5 ? f + 1 : f;
|
||||||
|
|||||||
Reference in New Issue
Block a user