import node;

//members
var maxSteps: int = 0;
var remainingSteps: int = 0;

//accessors
fn getMaxSteps(node: opaque) {
	return maxSteps;
}

fn getRemainingSteps(node: opaque) {
	return remainingSteps;
}

//mutators
fn setMaxSteps(node: opaque, steps: int) {
	maxSteps = steps;
	remainingSteps = steps;
}

fn setRemainingSteps(node: opaque, steps: int) {
	remainingSteps = steps;
}

//utility mutators
fn alterRemainingSteps(node: opaque, increment: int) {
	remainingSteps += increment;
	if (remainingSteps > maxSteps) {
		remainingSteps = maxSteps;
	}

	if (remainingSteps < 0) {
		remainingSteps = 0;
	}

	return remainingSteps;
}

//utils - polyfills
fn loadChild(parent: opaque, fname: string) {
	var child: opaque = loadNode(fname);
	parent.pushNode(child);
	child.setNodeLayer(parent.getNodeLayer());
	return child;
}

//lifecycle functions
fn onLoad(node: opaque) {
	node.loadNodeTexture("sprites:/stepcounter.png");

	node
		.loadChild("scripts:/gameplay/text.toy")
		.setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255); //rgba
}

fn onFree(node: opaque) {
	node.freeNodeTexture();
}

fn onDraw(node: opaque) {
	if (remainingSteps > 0 && maxSteps > 0) {
		var tmp = float remainingSteps / maxSteps * node.getNodeRectW();
		var h = node.getNodeRectH();

		node.drawNode(node.getNodePositionX(), node.getNodePositionY(), int tmp, h);
	}
}