Playing around with scripts

This commit is contained in:
2023-02-24 22:20:40 +11:00
parent 3fca594209
commit aabbf41dd6
19 changed files with 597 additions and 44 deletions

View File

@@ -25,7 +25,7 @@ mapInputEventToKeyUp("character_right", "right"); //event, keysym
//this function must always be called, or the engine won't run
initWindow("Airport Game", 800, 600, false);
initWindow("Airport Game", 1080, 720, false);
//kick off the logic of the scene graph
loadRootNode("scripts:/root.toy");
loadRootNode("scripts:/scene.toy");

View File

@@ -1,29 +0,0 @@
//import standard;
import engine;
import node;
//util to generate and init a child node of a parent
fn makeChild(parent: opaque, fname: string) {
var child: opaque = loadNode(fname);
parent.pushNode(child);
child.initNode();
}
//NOTE: root node can load the whole scene, and essentially act as the scene object
fn onInit(node: opaque) {
print "root.toy:onInit() called";
//make a child
node.makeChild("scripts:/entity.toy");
//give the child a child
// node.getNodeChild(0).makeChild("scripts:/entity.toy");
}
fn onStep(node: opaque) {
//print clock();
}
fn onFree(node: opaque) {
print "root.toy:onFree() called";
}

23
assets/scripts/scene.toy Normal file
View File

@@ -0,0 +1,23 @@
//the overarching scene
import node;
//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);
child.initNode();
return child;
}
fn onInit(node: opaque) {
//load the tile map node
var tilemapNode = node.makeChild("scripts:/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");
}

View File

@@ -0,0 +1,70 @@
//this is one layer
import node;
var childCounter: int = 0;
//TODO: reference these from a global source (root?)
var tileWidth: float const = 100;
var tileHeight: float const = 100;
var roomWidth: float const = 10;
var roomHeight: float const = 10;
//util to generate and init a child node of a given parent
fn makeChildSprite(parent: opaque, spriteName: string) {
var child: opaque = loadNode("scripts:/tilemap/tile.toy");
parent.pushNode(child);
child.initNode();
child.loadTexture("sprites:/" + spriteName);
//BUGFIX
childCounter++;
return child;
}
fn onInit(node: opaque) {
//load the child node, with the tiling back image
node.makeChildSprite("tile-background.png");
}
fn drawRoom(node: opaque, x, y, depth, camX, camY, camW, camH) {
//the modifier ratio to move things
var mod: float = float tileWidth / (tileWidth - depth);
var tileWidth_mod = tileWidth * mod;
var tileHeight_mod = 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 ));
}
}
}
fn round(x): int {
var f = floor(x);
return x - f >= 0.5 ? f + 1 : f;
}
fn floor(x): int {
return int x;
}
fn ceil(x): int {
var f = floor(x);
return x - f != 0 ? f + 1 : f;
}
fn min(a, b) {
return a < b ? a : b;
}
fn max(a, b) {
return a > b ? a : b;
}

View File

@@ -0,0 +1,84 @@
//this is one layer
import node;
var childCounter: int = 0;
//TODO: reference these from a global source (root?)
var tileWidth: float const = 100;
var tileHeight: float const = 100;
var roomWidth: float const = 10;
var roomHeight: float const = 10;
//util to generate and init a child node of a given parent
fn makeChildSprite(parent: opaque, spriteName: string) {
var child: opaque = loadNode("scripts:/tilemap/tile.toy");
parent.pushNode(child);
child.initNode();
child.loadTexture("sprites:/" + spriteName);
//BUGFIX
childCounter++;
return child;
}
fn onInit(node: opaque) {
//load the child node, with the tiling back image
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;
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 ));
}
//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 ));
}
//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));
}
//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 ));
}
}
fn round(x): int {
var f = floor(x);
return x - f >= 0.5 ? f + 1 : f;
}
fn floor(x): int {
return int x;
}
fn ceil(x): int {
var f = floor(x);
return x - f != 0 ? f + 1 : f;
}
fn min(a, b) {
return a < b ? a : b;
}
fn max(a, b) {
return a > b ? a : b;
}

View File

@@ -0,0 +1,2 @@
//EMPTY

View File

@@ -0,0 +1,95 @@
//this file manages the tilemap-related utilities
import standard;
import node;
var childCounter: int = 0;
var levelXCount: int const = 4;
var levelYCount: int const = 4;
var camX: float = 0;
var camY: float = 0;
//TODO: reference these from a global source (root?)
var tileWidth: float const = 100;
var tileHeight: float const = 100;
var roomWidth: float const = 10;
var roomHeight: float const = 10;
var screenWidth: float = 1080;
var screenHeight: float = 720;
//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);
child.initNode();
return child;
}
fn loadLayer(node: opaque, layerName: string) {
//load the given layer as a child
var layerNode = node.makeChild("scripts:/tilemap/" + layerName);
childCounter++;
}
var stepCounter = 0;
fn onStep(node: opaque) {
stepCounter++;
camX--;
camY--;
}
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);
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);
}
}
}
}
fn round(x): int {
var f = floor(x);
return x - f >= 0.5 ? f + 1 : f;
}
fn floor(x): int {
return int x;
}
fn ceil(x): int {
var f = floor(x);
return x - f != 0 ? f + 1 : f;
}
fn min(a, b) {
return a < b ? a : b;
}
fn max(a, b) {
return a > b ? a : b;
}