124 lines
2.0 KiB
Plaintext
124 lines
2.0 KiB
Plaintext
import node;
|
|
|
|
//constants
|
|
var SPEED: int const = 10;
|
|
|
|
//variables
|
|
var parent: opaque = null;
|
|
var x: int = 50;
|
|
var y: int = 50;
|
|
|
|
var xspeed: int = 0;
|
|
var yspeed: int = 0;
|
|
|
|
//accessors - variables are private, functions are public
|
|
fn getX(node: opaque) {
|
|
return x;
|
|
}
|
|
|
|
fn getY(node: opaque) {
|
|
return y;
|
|
}
|
|
|
|
//lifecycle functions
|
|
fn onInit(node: opaque) {
|
|
print "render.toy:onInit() called";
|
|
|
|
node.loadTexture("sprites:/character.png");
|
|
parent = node.getNodeParent();
|
|
}
|
|
|
|
fn onStep(node: opaque) {
|
|
x += xspeed;
|
|
y += yspeed;
|
|
}
|
|
|
|
fn onFree(node: opaque) {
|
|
print "render.toy:onFree() called";
|
|
|
|
node.freeTexture();
|
|
}
|
|
|
|
fn onDraw(node: opaque) {
|
|
// print "render.toy:onDraw() called";
|
|
|
|
var px = parent.callNode("getX");
|
|
var py = parent.callNode("getY");
|
|
|
|
if (px == null) {
|
|
px = 0;
|
|
}
|
|
|
|
if (py == null) {
|
|
py = 0;
|
|
}
|
|
|
|
node.drawNode(x + px, y + py, 100, 100);
|
|
}
|
|
|
|
//event functions
|
|
fn onKeyDown(node: opaque, event: string) {
|
|
if (event == "character_up") {
|
|
yspeed -= SPEED;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_down") {
|
|
yspeed += SPEED;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_left") {
|
|
xspeed -= SPEED;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_right") {
|
|
xspeed += SPEED;
|
|
return;
|
|
}
|
|
}
|
|
|
|
fn onKeyUp(node: opaque, event: string) {
|
|
if (event == "character_up" && yspeed < 0) {
|
|
yspeed = 0;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_down" && yspeed > 0) {
|
|
yspeed = 0;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_left" && xspeed < 0) {
|
|
xspeed = 0;
|
|
return;
|
|
}
|
|
|
|
if (event == "character_right" && xspeed > 0) {
|
|
xspeed = 0;
|
|
return;
|
|
}
|
|
}
|
|
|
|
fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
|
|
print "entity.toy:onMouseMotion()";
|
|
//TODO: mouse motion
|
|
}
|
|
|
|
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|
print "entity.toy:onMouseButtonDown()";
|
|
//TODO: mouse button down
|
|
}
|
|
|
|
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
|
print "entity.toy:onMouseButtonUp()";
|
|
//TODO: mouse button up
|
|
}
|
|
|
|
fn onMouseWheel(node: opaque, x: int, y: int) {
|
|
print "entity.toy:onMouseWheel()";
|
|
//TODO: mouse wheel
|
|
}
|
|
|