Got character walking around, fixed leak

This commit is contained in:
2023-03-04 08:19:18 +11:00
parent f152afc9d7
commit 3df788e69a
7 changed files with 164 additions and 6 deletions

View File

@@ -28,6 +28,8 @@
<None Include="assets\scripts\demo\tilemap\tilemap.toy" />
<None Include="assets\scripts\empty.toy" />
<None Include="assets\scripts\airplane.toy" />
<None Include="assets\scripts\gameplay\lejana.toy" />
<None Include="assets\scripts\gameplay\scene.toy" />
<None Include="assets\scripts\init.toy" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@@ -0,0 +1,154 @@
import node;
//constants
var SPEED: int const = 3;
//variables
var parent: opaque = null; //cache the parent for quick access
var posX: int = 50;
var posY: int = 50;
var WIDTH: int const = 128;
var HEIGHT: int const = 128;
var xspeed: int = 0;
var yspeed: int = 0;
//polyfills - animating different cycles on one image
var SPRITE_WIDTH: int const = 32;
var SPRITE_HEIGHT: int const = 32;
var stepCount: int = 0;
var stepDelay: int = 10;
fn faceDown(node: opaque) {
node.setNodeRect(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
node.setNodeFrames(4);
}
fn faceUp(node: opaque) {
node.setNodeRect(32 * 4, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
node.setNodeFrames(4);
}
fn faceLeft(node: opaque) {
node.setNodeRect(32 * 8, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
node.setNodeFrames(4);
}
fn faceRight(node: opaque) {
node.setNodeRect(32 * 8, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
node.setNodeFrames(4);
}
//lifecycle functions
fn onLoad(node: opaque) {
//
}
fn onInit(node: opaque) {
parent = node.getParentNode();
node.loadTexture("sprites:/lejana.png");
node.faceDown();
}
fn onStep(node: opaque) {
posX += xspeed;
posY += yspeed;
//animate after X steps
if (xspeed == 0 && yspeed == 0) {
stepCount = 0;
node.setCurrentNodeFrame(0);
}
else {
if (++stepCount >= stepDelay) {
node.incrementCurrentNodeFrame();
stepCount = 0;
}
}
}
fn onFree(node: opaque) {
node.freeTexture();
}
fn onDraw(node: opaque) {
var px = 0;
var py = 0;
if (parent != null) {
px = parent.callNodeFn("getX");
py = parent.callNodeFn("getY");
}
node.drawNode(posX + px, posY + py, WIDTH, HEIGHT);
}
//event functions
fn onKeyDown(node: opaque, event: string) {
if (event == "character_up") {
yspeed -= SPEED;
faceUp(node);
return;
}
if (event == "character_down") {
yspeed += SPEED;
faceDown(node);
return;
}
if (event == "character_left") {
xspeed -= SPEED;
faceLeft(node);
return;
}
if (event == "character_right") {
xspeed += SPEED;
faceRight(node);
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) {
//
}
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
//jump to pos
posX = x - WIDTH / 2;
posY = y - HEIGHT / 2;
}
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
//
}
fn onMouseWheel(node: opaque, xrel: int, yrel: int) {
//
}

View File

View File

@@ -28,10 +28,10 @@
//this function must always be called, or the engine won't run
initWindow("Airport Game", 1080, 720, false);
initWindow("Skyland", 1080, 720, false);
//kick off the logic of the scene graph
loadRootNode("scripts:/demo/scene.toy");
loadRootNode("scripts:/gameplay/lejana.toy");
}
//Globals go here

BIN
assets/sprites/lejana.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -146,8 +146,10 @@ static inline void execLoadRootNode() {
//immediately call onLoad() after running the script - for loading other nodes
Box_callEngineNode(engine.rootNode, &inner, "onLoad", NULL);
//cache the scope for later freeing
engine.rootNode->scope = inner.scope;
//manual cleanup
Toy_popScope(inner.scope);
Toy_freeLiteralArray(&inner.stack);
Toy_freeLiteralArray(&inner.literalCache);
@@ -207,7 +209,7 @@ static inline void execEvents() {
//call the function
Toy_pushLiteralArray(&args, eventLiteral);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyDown", &args);
Toy_popLiteralArray(&args);
Toy_freeLiteral(Toy_popLiteralArray(&args));
//push to the event list
Toy_freeLiteral(eventLiteral);
@@ -234,7 +236,7 @@ static inline void execEvents() {
//call the function
Toy_pushLiteralArray(&args, eventLiteral);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyUp", &args);
Toy_popLiteralArray(&args);
Toy_freeLiteral(Toy_popLiteralArray(&args));
//push to the event list
Toy_freeLiteral(eventLiteral);

View File

@@ -86,7 +86,7 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
printf("Renderer: %s (HW %s)\n", rendererInfo.name, rendererInfo.flags & SDL_RENDERER_ACCELERATED ? "yes" : "no");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
SDL_RenderSetLogicalSize(engine.renderer, engine.screenWidth, engine.screenHeight);
//only run with a window