Updated and testing Box
This commit is contained in:
2
Box
2
Box
Submodule Box updated: 94d9915520...85bcf5f8f8
@@ -1,26 +1,11 @@
|
|||||||
import node;
|
import node;
|
||||||
|
|
||||||
//constants
|
//constants
|
||||||
var SPEED: int const = 5;
|
var SPEED: int const = 10;
|
||||||
|
|
||||||
//variables
|
|
||||||
var parent: opaque = null; //cache the parent for quick access
|
|
||||||
var posX: int = 50;
|
|
||||||
var posY: int = 50;
|
|
||||||
var WIDTH: int const = 143;
|
var WIDTH: int const = 143;
|
||||||
var HEIGHT: int const = 75;
|
var HEIGHT: int const = 75;
|
||||||
|
|
||||||
var xspeed: int = 0;
|
|
||||||
var yspeed: int = 0;
|
|
||||||
|
|
||||||
//accessors - variables are private, functions are public
|
|
||||||
fn getX(node: opaque) {
|
|
||||||
return posX;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn getY(node: opaque) {
|
|
||||||
return posY;
|
|
||||||
}
|
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onLoad(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
@@ -30,15 +15,11 @@ fn onLoad(node: opaque) {
|
|||||||
fn onInit(node: opaque) {
|
fn onInit(node: opaque) {
|
||||||
print "onInit() called";
|
print "onInit() called";
|
||||||
|
|
||||||
parent = node.getParentNode();
|
|
||||||
node.loadNodeTexture("sprites:/little_plane.png");
|
node.loadNodeTexture("sprites:/little_plane.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onStep(node: opaque) {
|
fn onStep(node: opaque) {
|
||||||
// print "onStep() called";
|
//TODO: move
|
||||||
|
|
||||||
posX += xspeed;
|
|
||||||
posY += yspeed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
@@ -49,59 +30,61 @@ fn onFree(node: opaque) {
|
|||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
// print "onDraw() called";
|
// print "onDraw() called";
|
||||||
|
var parent: opaque = node.getParentNode();
|
||||||
|
|
||||||
var px = 0;
|
var px = 0;
|
||||||
var py = 0;
|
var py = 0;
|
||||||
|
|
||||||
|
//TODO: get world position
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
px = parent.callNodeFn("getX");
|
px = parent.getNodePositionX();
|
||||||
py = parent.callNodeFn("getY");
|
py = parent.getNodePositionY();
|
||||||
}
|
}
|
||||||
|
|
||||||
node.drawNode(posX + px, posY + py, WIDTH, HEIGHT);
|
node.drawNode(node.getNodePositionX() + px, node.getNodePositionY() + py, WIDTH, HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
//event functions
|
//event functions
|
||||||
fn onKeyDown(node: opaque, event: string) {
|
fn onKeyDown(node: opaque, event: string) {
|
||||||
if (event == "character_up") {
|
if (event == "character_up") {
|
||||||
yspeed -= SPEED;
|
node.setNodeMotionY(-SPEED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_down") {
|
if (event == "character_down") {
|
||||||
yspeed += SPEED;
|
node.setNodeMotionY(SPEED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_left") {
|
if (event == "character_left") {
|
||||||
xspeed -= SPEED;
|
node.setNodeMotionX(-SPEED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_right") {
|
if (event == "character_right") {
|
||||||
xspeed += SPEED;
|
node.setNodeMotionX(SPEED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onKeyUp(node: opaque, event: string) {
|
fn onKeyUp(node: opaque, event: string) {
|
||||||
if (event == "character_up" && yspeed < 0) {
|
if (event == "character_up" && node.getNodeMotionY() < 0) {
|
||||||
yspeed = 0;
|
node.setNodeMotionY(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_down" && yspeed > 0) {
|
if (event == "character_down" && node.getNodeMotionY() > 0) {
|
||||||
yspeed = 0;
|
node.setNodeMotionY(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_left" && xspeed < 0) {
|
if (event == "character_left" && node.getNodeMotionX() < 0) {
|
||||||
xspeed = 0;
|
node.setNodeMotionX(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_right" && xspeed > 0) {
|
if (event == "character_right" && node.getNodeMotionX() > 0) {
|
||||||
xspeed = 0;
|
node.setNodeMotionX(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,8 +97,8 @@ fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|||||||
// print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")";
|
// print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")";
|
||||||
|
|
||||||
//jump to pos
|
//jump to pos
|
||||||
posX = x - WIDTH / 2;
|
node.setNodePositionX(x - WIDTH / 2);
|
||||||
posY = y - HEIGHT / 2;
|
node.setNodePositionY(y - HEIGHT / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
||||||
|
|||||||
@@ -32,10 +32,10 @@
|
|||||||
|
|
||||||
|
|
||||||
//this function must always be called, or the engine won't run
|
//this function must always be called, or the engine won't run
|
||||||
initWindow("Skyland", 32*16, 32*16 + 32, false); //TODO: custom FPS setting
|
initWindow("Airport", 800, 600, false); //TODO: custom FPS setting
|
||||||
|
|
||||||
//kick off the logic of the scene graph
|
//kick off the logic of the scene graph
|
||||||
loadRootNode("scripts:/gameplay/scene.toy");
|
loadRootNode("scripts:/airplane.toy");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Globals go here
|
//Globals go here
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
|
|
||||||
import node;
|
|
||||||
|
|
||||||
//generate a number of child nodes
|
|
||||||
fn onInit(node: opaque) {
|
|
||||||
node.loadChild("scripts:/empty.toy", 3);
|
|
||||||
node.loadChild("scripts:/empty.toy", 2);
|
|
||||||
node.loadChild("scripts:/empty.toy", 1);
|
|
||||||
node.loadChild("scripts:/empty.toy", 4);
|
|
||||||
node.loadChild("scripts:/empty.toy", 5);
|
|
||||||
|
|
||||||
node.freeChildNode(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onStep(node) {
|
|
||||||
node.sortChildrenNode(lessThan);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lessThan(lhs, rhs) {
|
|
||||||
var a = lhs.callNodeFn("getValue");
|
|
||||||
var b = rhs.callNodeFn("getValue");
|
|
||||||
|
|
||||||
return a < b;
|
|
||||||
}
|
|
||||||
|
|
||||||
//utils - polyfills
|
|
||||||
fn loadChild(parent: opaque, fname: string, value) {
|
|
||||||
var child: opaque = loadNode(fname);
|
|
||||||
|
|
||||||
child.callNodeFn("setValue", value);
|
|
||||||
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user