Fix animations from not deleting gameobjects

This commit is contained in:
John Alanbrook 2024-04-02 07:41:46 -05:00
parent 32333c32ad
commit 3f73a808d8
9 changed files with 63 additions and 62 deletions

View file

@ -58,14 +58,14 @@ actor.interval = function(fn, seconds) {
} }
actor.delay = function(fn, seconds) { actor.delay = function(fn, seconds) {
var that = this; var timers = this.timers;
var stop = function() { var stop = function() {
that.timers.remove(stop); timers.remove(stop);
rm(); rm();
} }
function execute() { function execute() {
fn.call(that); fn();
stop(); stop();
} }
@ -80,7 +80,7 @@ actor.delay = function(fn, seconds) {
var rm = Register.appupdate.register(update); var rm = Register.appupdate.register(update);
this.timers.push(stop); timers.push(stop);
return stop; return stop;
}; };

View file

@ -89,33 +89,33 @@ Object.mixin(os.sprite(true), {
anim:{}, anim:{},
playing: 0, playing: 0,
play(str) { play(str) {
console.trace();
this.del_anim?.(); this.del_anim?.();
var sp = this; var self = this;
var stop; var stop;
this.del_anim = function() { self.del_anim = function() {
sp = undefined; self.del_anim = undefined;
self = undefined;
advance = undefined; advance = undefined;
this.del_anim = undefined;
stop(); stop();
} }
str ??= 0; str ??= 0;
var playing = this.anim[str]; var playing = self.anim[str];
if (!playing) return; if (!playing) return;
var f = 0; var f = 0;
sp.path = playing.path; self.path = playing.path;
function advance() { function advance() {
if (!sp) this.del_anim(); if (!self) return;
if (!sp.gameobject) return; if (!self.gameobject) return;
//sp.path = playing.path; //self.path = playing.path;
sp.frame = playing.frames[f].rect; self.frame = playing.frames[f].rect;
f = (f+1)%playing.frames.length; f = (f+1)%playing.frames.length;
if (f === 0) { if (f === 0) {
sp.anim_done?.(); self.anim_done?.();
if (!sp.loop) { sp.stop(); return; } self.anim_done = undefined;
// if (!self.loop) { self.stop(); return; }
} }
stop = sp.gameobject.delay(advance, playing.frames[f].time); stop = self.gameobject.delay(advance, playing.frames[f].time);
} }
this.tex(game.texture(playing.path)); this.tex(game.texture(playing.path));
advance(); advance();
@ -124,7 +124,6 @@ Object.mixin(os.sprite(true), {
this.del_anim?.(); this.del_anim?.();
}, },
set path(p) { set path(p) {
console.info(`setting path to ${p}`);
p = Resources.find_image(p); p = Resources.find_image(p);
if (!p) { if (!p) {
console.warn(`Could not find image ${p}.`); console.warn(`Could not find image ${p}.`);
@ -347,7 +346,6 @@ var SpriteAnim = {
find(path) { find(path) {
if (!io.exists(path + ".asset")) return; if (!io.exists(path + ".asset")) return;
var asset = JSON.parse(io.slurp(path + ".asset")); var asset = JSON.parse(io.slurp(path + ".asset"));
}, },
}; };

View file

@ -180,10 +180,9 @@ var gameobject = {
}, },
delay(fn, seconds) { delay(fn, seconds) {
var that = this; var timers = this.timers;
var stop = function() { var stop = function() {
that.timers.remove(stop); timers.remove(stop);
execute = undefined; execute = undefined;
stop = undefined; stop = undefined;
rm?.(); rm?.();
@ -192,7 +191,7 @@ var gameobject = {
} }
function execute() { function execute() {
fn.call(that); fn();
stop?.(); stop?.();
} }
@ -205,7 +204,7 @@ var gameobject = {
} }
var rm = Register.update.register(update); var rm = Register.update.register(update);
this.timers.push(stop); timers.push(stop);
return stop; return stop;
}, },
@ -256,8 +255,8 @@ var gameobject = {
spawn(text, config, callback) { spawn(text, config, callback) {
var ent = os.make_gameobject(); var ent = os.make_gameobject();
ent.setref(ent); ent.setref(ent);
ent.components ??= {}; ent.components = {};
ent.objects ??= {}; ent.objects = {};
ent.timers = []; ent.timers = [];
if (typeof text === 'object') // assume it's an ur if (typeof text === 'object') // assume it's an ur
{ {
@ -301,8 +300,6 @@ var gameobject = {
if (sim.playing()) if (sim.playing())
if (typeof ent.start === 'function') ent.start(); if (typeof ent.start === 'function') ent.start();
ent.objects ??= {};
ent.components ??= {};
Object.hide(ent, 'ur', 'components', 'objects', '_ed', 'timers', 'master'); Object.hide(ent, 'ur', 'components', 'objects', '_ed', 'timers', 'master');
var mur = ent.get_ur(); var mur = ent.get_ur();
@ -315,11 +312,12 @@ var gameobject = {
var o = ent.objects; var o = ent.objects;
delete ent.objects; delete ent.objects;
for (var i in o) { for (var i in o) {
say(`MAKING ${i}`); console.info(`MAKING ${i}`);
var n = ent.spawn(ur[o[i].ur]); var n = ent.spawn(ur[o[i].ur]);
ent.rename_obj(n.toString(), i); ent.rename_obj(n.toString(), i);
delete o[i].ur; delete o[i].ur;
Object.assign(n, o[i]); Object.assign(n, o[i]);
n.sync();
} }
} }
@ -537,14 +535,19 @@ var gameobject = {
for (var key in this.components) { for (var key in this.components) {
this.components[key].kill?.(); this.components[key].kill?.();
this.components[key].gameobject = undefined; this.components[key].gameobject = undefined;
this[key].enabled = false;
delete this.components[key]; delete this.components[key];
delete this[key]; delete this[key];
} }
delete this.components;
this.clear(); this.clear();
this.objects = undefined;
if (typeof this.stop === 'function') this.stop(); if (typeof this.stop === 'function') this.stop();
for (var i in this) {
if (typeof this[i] === 'object') delete this[i];
if (typeof this[i] === 'function') delete this[i];
}
}, },
up() { return [0, 1].rotate(this.angle); }, up() { return [0, 1].rotate(this.angle); },
@ -581,7 +584,6 @@ var gameobject = {
}, },
add_component(comp, data, name) { add_component(comp, data, name) {
data ??= undefined;
if (typeof comp.make !== 'function') return; if (typeof comp.make !== 'function') return;
name ??= comp.toString(); name ??= comp.toString();
name = obj_unique_name(name, this); name = obj_unique_name(name, this);
@ -607,7 +609,7 @@ function go_init() {
gop.sync = function() { gop.sync = function() {
gsync.call(this); gsync.call(this);
this.components.forEach(function(x) { x.sync?.(); }); this.components.forEach(function(x) { x.sync?.(); });
this.objects.forEach(function(x) { x.sync(); }); this.objects.forEach(function(x) { x.sync?.(); });
} }
} }

View file

@ -11,6 +11,7 @@ appy.inputs = {};
if (os.sys() === 'macos') { if (os.sys() === 'macos') {
appy.inputs['S-q'] = os.quit; appy.inputs['S-q'] = os.quit;
appy.inputs['S-h'] = function() { }; appy.inputs['S-h'] = function() { };
appy.inputs['S-g'] = os.gc;
} }
player[0].control(appy); player[0].control(appy);

View file

@ -87,7 +87,6 @@ void mYughLog(int category, int priority, int line, const char *file, const char
vprintf(message, args); vprintf(message, args);
va_end(args); va_end(args);
printf("\n"); printf("\n");
fflush(stdout);
} }
//if (priority >= LOG_ERROR) //if (priority >= LOG_ERROR)

View file

@ -43,9 +43,18 @@ static JSValue globalThis;
static JSClassID js_ptr_id; static JSClassID js_ptr_id;
static JSClassDef js_ptr_class = { "POINTER" }; static JSClassDef js_ptr_class = { "POINTER" };
JSValue str2js(const char *c) { JSValue str2js(const char *c, ...) {
if (!c) return JS_UNDEFINED; if (!c) return JS_UNDEFINED;
return JS_NewString(js, c); char *result = NULL;
va_list args;
va_start(args, c);
va_list args2;
va_copy(args2,args);
char buf[1+vsnprintf(NULL, 0, c, args)];
va_end(args);
vsnprintf(buf, sizeof buf, c, args2);
va_end(args2);
return JS_NewString(js, buf);
} }
const char *js2str(JSValue v) { const char *js2str(JSValue v) {
@ -67,25 +76,7 @@ QJSCLASS(material)
QJSCLASS(mesh) QJSCLASS(mesh)
QJSCLASS(window) QJSCLASS(window)
QJSCLASS(drawmodel) QJSCLASS(drawmodel)
QJSCLASS(constraint)
/* qjs class colliders and constraints */
/* constraint works for all constraints - 2d or 3d */
static JSClassID js_constraint_id;
static void js_constraint_finalizer(JSRuntime *rt, JSValue val) {
constraint *c = JS_GetOpaque(val, js_constraint_id);
constraint_free(c);
}
static JSClassDef js_constraint_class = {
"constraint",
.finalizer = js_constraint_finalizer
};
static constraint *js2constraint(JSValue val) { return JS_GetOpaque(val, js_constraint_id); }
static JSValue constraint2js(constraint *c)
{
JSValue j = JS_NewObjectClass(js, js_constraint_id);
JS_SetOpaque(j, c);
return j;
}
static JSValue sound_proto; static JSValue sound_proto;
sound *js2sound(JSValue v) { return js2dsp_node(v)->data; } sound *js2sound(JSValue v) { return js2dsp_node(v)->data; }

View file

@ -3,6 +3,7 @@
#include "quickjs/quickjs.h" #include "quickjs/quickjs.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
#include <stdarg.h>
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } } #define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
@ -93,6 +94,8 @@ static JSValue TYPE##2js(TYPE *n) { \
YughSpam("Created " #TYPE " at %p", n); \ YughSpam("Created " #TYPE " at %p", n); \
JS_SetOpaque(j,n);\ JS_SetOpaque(j,n);\
return j; }\ return j; }\
\
static JSValue js_##TYPE##_memid (JSContext *js, JSValue this) { return str2js("%p", js2##TYPE(this)); } \
#define QJSGLOBALCLASS(NAME) \ #define QJSGLOBALCLASS(NAME) \
JSValue NAME = JS_NewObject(js); \ JSValue NAME = JS_NewObject(js); \
@ -108,8 +111,10 @@ JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
QJSCLASSPREP(TYPE); \ QJSCLASSPREP(TYPE); \
JSValue TYPE##_proto = JS_NewObject(js); \ JSValue TYPE##_proto = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYPE##_funcs)); \ JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYPE##_funcs)); \
JS_SetPropertyStr(js, TYPE##_proto, "memid", JS_NewCFunction(js, &js_##TYPE##_memid, "memid", 0)); \
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \ JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
#define countof(x) (sizeof(x)/sizeof((x)[0])) #define countof(x) (sizeof(x)/sizeof((x)[0]))
void ffi_load(); void ffi_load();
@ -125,7 +130,7 @@ int js_print_exception(JSValue v);
struct rgba js2color(JSValue v); struct rgba js2color(JSValue v);
double js2number(JSValue v); double js2number(JSValue v);
JSValue number2js(double g); JSValue number2js(double g);
JSValue str2js(const char *c); JSValue str2js(const char *c, ...);
void nota_int(char *blob); void nota_int(char *blob);

View file

@ -98,8 +98,10 @@ void sprite_draw_all() {
qsort(sprites, arrlen(sprites), sizeof(*sprites), sprite_sort); qsort(sprites, arrlen(sprites), sizeof(*sprites), sprite_sort);
for (int i = 0; i < arrlen(sprites); i++) for (int i = 0; i < arrlen(sprites); i++) {
if (!sprites[i]->enabled) continue;
sprite_draw(sprites[i]); sprite_draw(sprites[i]);
}
} }
void sprite_initialize() { void sprite_initialize() {

View file

@ -65,7 +65,10 @@ void c_init() {
JS_FreeValue(js, c_start); JS_FreeValue(js, c_start);
} }
void c_frame() { script_call_sym(c_process_fn,0,NULL); } void c_frame() {
script_call_sym(c_process_fn,0,NULL);
fflush(stdout);
}
void cleanup() void cleanup()
{ {