Moved the demo scripts into a directory
This commit is contained in:
65
assets/scripts/demo/scene.toy
Normal file
65
assets/scripts/demo/scene.toy
Normal file
@@ -0,0 +1,65 @@
|
||||
//the overarching scene
|
||||
import engine;
|
||||
import node;
|
||||
|
||||
//debugging tools
|
||||
var stepCounter: int = 0;
|
||||
var stepTracker: [int] = [0, 0, 0];
|
||||
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
parent.pushNode(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
//lifecycle functions
|
||||
fn onLoad(node: opaque) {
|
||||
//load the tile map node
|
||||
var tilemapNode = node.makeChild("scripts:/demo/tilemap/tilemap.toy");
|
||||
|
||||
tilemapNode.callNodeFn("loadLayer", "layer-background.toy");
|
||||
tilemapNode.callNodeFn("loadLayer", "layer-walls.toy");
|
||||
tilemapNode.callNodeFn("loadLayer", "layer-walls.toy");
|
||||
//tilemapNode.callNodeFn("loadLayer", "layer-walls.toy"); //TODO: remove this
|
||||
//tilemapNode.callNodeFn("loadLayer", "layer-maze.toy");
|
||||
}
|
||||
|
||||
//debugging code
|
||||
fn onStep(node: opaque) {
|
||||
stepCounter++;
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
//round off - too many skips means it's crappy anyway
|
||||
if (stepCounter > stepTracker.length() - 1) {
|
||||
stepCounter = stepTracker.length() - 1;
|
||||
}
|
||||
|
||||
//This logic is just a debugging tool
|
||||
stepTracker[stepCounter] = stepTracker[stepCounter] + 1;
|
||||
print stepTracker;
|
||||
stepCounter = 0;
|
||||
}
|
||||
|
||||
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
||||
//reload the scene on click
|
||||
if (button == "left") {
|
||||
loadRootNode("scripts:/demo/scene.toy");
|
||||
}
|
||||
}
|
||||
|
||||
//global accessors
|
||||
fn getTileWidth(node: opaque): int { return 64; }
|
||||
fn getTileHeight(node: opaque): int { return 64; }
|
||||
|
||||
fn getRoomWidth(node: opaque): int { return 10; }
|
||||
fn getRoomHeight(node: opaque): int { return 10; }
|
||||
|
||||
fn getLevelXCount(node: opaque): int { return 9; }
|
||||
fn getLevelYCount(node: opaque): int { return 9; }
|
||||
|
||||
//TODO: move this into the engine
|
||||
fn getScreenWidth(node: opaque): int { return 1080; }
|
||||
fn getScreenHeight(node: opaque): int { return 720; }
|
||||
65
assets/scripts/demo/tilemap/layer-background.toy
Normal file
65
assets/scripts/demo/tilemap/layer-background.toy
Normal file
@@ -0,0 +1,65 @@
|
||||
//this is one layer
|
||||
import standard;
|
||||
import engine;
|
||||
import node;
|
||||
|
||||
|
||||
//TODO: replace with an empty node function (polyfill)
|
||||
fn loadChildEmpty(parent: opaque) {
|
||||
var child: opaque = loadNode("scripts:/empty.toy");
|
||||
parent.pushNode(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
fn onLoad(node: opaque) {
|
||||
//load the child node, with the tiling back image
|
||||
node.loadChildEmpty();
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
node.getChildNode(0).loadTexture("sprites:/tile-background.png");
|
||||
}
|
||||
|
||||
|
||||
//drawing util
|
||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
||||
//get the constants from root
|
||||
var root: opaque = getRootNode();
|
||||
|
||||
var tileWidth: int const = root.callNodeFn("getTileWidth");
|
||||
var tileHeight: int const = root.callNodeFn("getTileHeight");
|
||||
|
||||
var roomWidth: int const = root.callNodeFn("getRoomWidth");
|
||||
var roomHeight: int const = root.callNodeFn("getRoomHeight");
|
||||
|
||||
var levelXCount: int const = root.callNodeFn("getLevelXCount");
|
||||
var levelYCount: int const = root.callNodeFn("getLevelYCount");
|
||||
|
||||
//calc the modifier ratio to offset things
|
||||
var mod: float = float tileWidth / (tileWidth - depth);
|
||||
|
||||
var tileWidth_mod: float = tileWidth * mod;
|
||||
var tileHeight_mod: float = tileHeight * mod;
|
||||
var camX_mod: float = (camX - camW) * mod + camW/2;
|
||||
var camY_mod: float = (camY - camH) * mod + camH/2;
|
||||
|
||||
//calc the region to render
|
||||
var lowerX: int = round((camX - camW/2.0) / tileWidth);
|
||||
var upperX: int = round((camX - camW*1.5) / tileWidth);
|
||||
var lowerY: int = round((camY - camH/2.0) / tileHeight);
|
||||
var upperY: int = 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.getChildNode(0).drawNode(round(camX_mod + i * tileWidth_mod), round(camY_mod + j * tileHeight_mod), round(tileWidth_mod), round(tileHeight_mod));
|
||||
}
|
||||
}
|
||||
}
|
||||
69
assets/scripts/demo/tilemap/layer-walls.toy
Normal file
69
assets/scripts/demo/tilemap/layer-walls.toy
Normal file
@@ -0,0 +1,69 @@
|
||||
//this is one layer
|
||||
import standard;
|
||||
import engine;
|
||||
import node;
|
||||
|
||||
|
||||
//TODO: replace with an empty node function (polyfill)
|
||||
fn loadChildEmpty(parent: opaque) {
|
||||
var child: opaque = loadNode("scripts:/empty.toy");
|
||||
parent.pushNode(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
fn onLoad(node: opaque) {
|
||||
//load the child node, with the tiling back image
|
||||
node.loadChildEmpty();
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
node.getChildNode(0).loadTexture("sprites:/tile-wall.png");
|
||||
}
|
||||
|
||||
|
||||
//drawing util
|
||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
||||
//get the constants from root
|
||||
var root: opaque = getRootNode();
|
||||
|
||||
var tileWidth: int const = root.callNodeFn("getTileWidth");
|
||||
var tileHeight: int const = root.callNodeFn("getTileHeight");
|
||||
|
||||
var roomWidth: int const = root.callNodeFn("getRoomWidth");
|
||||
var roomHeight: int const = root.callNodeFn("getRoomHeight");
|
||||
|
||||
var levelXCount: int const = root.callNodeFn("getLevelXCount");
|
||||
var levelYCount: int const = root.callNodeFn("getLevelYCount");
|
||||
|
||||
//calc the modifier ratio to offset things
|
||||
var mod: float = float tileWidth / (tileWidth - depth);
|
||||
|
||||
var tileWidth_mod: int = round(tileWidth * mod);
|
||||
var tileHeight_mod: int = round(tileHeight * mod);
|
||||
var camX_mod: int = round((camX - camW) * mod + camW/2);
|
||||
var camY_mod: int = round((camY - camH) * mod + camH/2);
|
||||
|
||||
//calc the region to render
|
||||
var lowerX: int = round((camX - camW/2.0) / tileWidth);
|
||||
var upperX: int = round((camX - camW*1.5) / tileWidth);
|
||||
var lowerY: int = round((camY - camH/2.0) / tileHeight);
|
||||
var upperY: int = 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++) {
|
||||
if ( !(int i % int roomWidth == 0 || int i % int roomWidth == roomWidth - 1) && !(int j % int roomHeight == 0 || int j % int roomHeight == roomHeight - 1) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
node.getChildNode(0).drawNode(camX_mod + i * tileWidth_mod, camY_mod + j * tileHeight_mod, tileWidth_mod, tileHeight_mod);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
assets/scripts/demo/tilemap/tilemap.toy
Normal file
35
assets/scripts/demo/tilemap/tilemap.toy
Normal file
@@ -0,0 +1,35 @@
|
||||
//this file manages the tilemap-related utilities
|
||||
import standard;
|
||||
import engine;
|
||||
import node;
|
||||
|
||||
var camX: float = 0;
|
||||
var camY: float = 0;
|
||||
|
||||
//util to generate a child node of a given parent
|
||||
fn loadChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
parent.pushNode(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
fn loadLayer(node: opaque, layerName: string) {
|
||||
//load the given layer as a child
|
||||
var layerNode = node.loadChild("scripts:/demo/tilemap/" + layerName);
|
||||
}
|
||||
|
||||
//lifecycle functions
|
||||
fn onStep(node: opaque) {
|
||||
camX--;
|
||||
camY--;
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
var screenWidth: int const = getRootNode().callNodeFn("getScreenWidth");
|
||||
var screenHeight: int const = getRootNode().callNodeFn("getScreenHeight");
|
||||
|
||||
//iterate over each layer, passing in the screen dimensions
|
||||
for (var c = 0; c < node.getChildNodeCount(); c++) {
|
||||
node.getChildNode(c).callNodeFn("drawLayer", camX, camY, screenWidth, screenHeight, c * 4.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user