//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; }
