Added node scaling, and a few utility functions
This commit is contained in:
111
assets/airport-demo/airplane.toy
Normal file
111
assets/airport-demo/airplane.toy
Normal file
@@ -0,0 +1,111 @@
|
||||
import node;
|
||||
|
||||
//constants
|
||||
var SPEED: int const = 10;
|
||||
var WIDTH: int const = 143;
|
||||
var HEIGHT: int const = 75;
|
||||
|
||||
|
||||
|
||||
//lifecycle functions
|
||||
fn onLoad(node: opaque) {
|
||||
print "onLoad() called";
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
print "onInit() called";
|
||||
|
||||
node.loadNodeTexture("sprites:/little_plane.png");
|
||||
}
|
||||
|
||||
fn onStep(node: opaque) {
|
||||
//TODO: move
|
||||
}
|
||||
|
||||
fn onFree(node: opaque) {
|
||||
print "onFree() called";
|
||||
|
||||
node.freeNodeTexture();
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
// print "onDraw() called";
|
||||
var parent: opaque = node.getParentNode();
|
||||
|
||||
var px = 0;
|
||||
var py = 0;
|
||||
|
||||
//TODO: get world position
|
||||
if (parent != null) {
|
||||
px = parent.getNodePositionX();
|
||||
py = parent.getNodePositionY();
|
||||
}
|
||||
|
||||
node.drawNode(node.getNodePositionX() + px, node.getNodePositionY() + py, WIDTH, HEIGHT);
|
||||
}
|
||||
|
||||
//event functions
|
||||
fn onKeyDown(node: opaque, event: string) {
|
||||
if (event == "character_up") {
|
||||
node.setNodeMotionY(-SPEED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_down") {
|
||||
node.setNodeMotionY(SPEED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_left") {
|
||||
node.setNodeMotionX(-SPEED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_right") {
|
||||
node.setNodeMotionX(SPEED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fn onKeyUp(node: opaque, event: string) {
|
||||
if (event == "character_up" && node.getNodeMotionY() < 0) {
|
||||
node.setNodeMotionY(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_down" && node.getNodeMotionY() > 0) {
|
||||
node.setNodeMotionY(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_left" && node.getNodeMotionX() < 0) {
|
||||
node.setNodeMotionX(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event == "character_right" && node.getNodeMotionX() > 0) {
|
||||
node.setNodeMotionX(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
|
||||
// print "entity.toy:onMouseMotion(" + string x + ", " + string y + ", " + string xrel + ", " + string yrel + ")";
|
||||
}
|
||||
|
||||
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
||||
// print "entity.toy:onMouseButtonDown(" + string x + ", " + string y + ", " + button + ")";
|
||||
|
||||
//jump to pos
|
||||
node.setNodePositionX(x - WIDTH / 2);
|
||||
node.setNodePositionY(y - HEIGHT / 2);
|
||||
}
|
||||
|
||||
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
||||
// print "entity.toy:onMouseButtonUp(" + string x + ", " + string y + ", " + button + ")";
|
||||
}
|
||||
|
||||
fn onMouseWheel(node: opaque, xrel: int, yrel: int) {
|
||||
// print "entity.toy:onMouseWheel(" + string xrel + ", " + string yrel + ")";
|
||||
}
|
||||
|
||||
42
assets/airport-demo/init.toy
Normal file
42
assets/airport-demo/init.toy
Normal file
@@ -0,0 +1,42 @@
|
||||
//A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object
|
||||
//To resolve that, the configuration is inside a block scope
|
||||
{
|
||||
import engine;
|
||||
import input;
|
||||
|
||||
|
||||
//input settings, mapping SDL2's virtual keys to event names
|
||||
mapInputEventToKeyDown("character_up", "w"); //event, keysym
|
||||
mapInputEventToKeyDown("character_left", "a"); //event, keysym
|
||||
mapInputEventToKeyDown("character_down", "s"); //event, keysym
|
||||
mapInputEventToKeyDown("character_right", "d"); //event, keysym
|
||||
|
||||
mapInputEventToKeyUp("character_up", "w"); //event, keysym
|
||||
mapInputEventToKeyUp("character_left", "a"); //event, keysym
|
||||
mapInputEventToKeyUp("character_down", "s"); //event, keysym
|
||||
mapInputEventToKeyUp("character_right", "d"); //event, keysym
|
||||
|
||||
mapInputEventToKeyDown("character_up", "up"); //event, keysym
|
||||
mapInputEventToKeyDown("character_left", "left"); //event, keysym
|
||||
mapInputEventToKeyDown("character_down", "down"); //event, keysym
|
||||
mapInputEventToKeyDown("character_right", "right"); //event, keysym
|
||||
|
||||
mapInputEventToKeyUp("character_up", "up"); //event, keysym
|
||||
mapInputEventToKeyUp("character_left", "left"); //event, keysym
|
||||
mapInputEventToKeyUp("character_down", "down"); //event, keysym
|
||||
mapInputEventToKeyUp("character_right", "right"); //event, keysym
|
||||
|
||||
mapInputEventToKeyDown("character_attack", "space"); //event, keysym
|
||||
|
||||
//TODO: escape to kill the game
|
||||
|
||||
|
||||
//this function must always be called, or the engine won't run
|
||||
initWindow("Airport", 800, 600, false); //TODO: custom FPS setting
|
||||
|
||||
//kick off the logic of the scene graph
|
||||
loadRootNode("scripts:/airplane.toy");
|
||||
}
|
||||
|
||||
//Globals go here
|
||||
|
||||
BIN
assets/airport-demo/little_plane.png
Normal file
BIN
assets/airport-demo/little_plane.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Reference in New Issue
Block a user