timers are better

This commit is contained in:
John Alanbrook 2024-09-30 10:14:56 -05:00
parent a476562a9e
commit b3b3aa9803
6 changed files with 65 additions and 62 deletions

View file

@ -132,33 +132,7 @@ actor.kill = function () {
actor.kill.doc = `Remove this actor and all its padawans from existence.`;
actor.delay = function (fn, seconds) {
var timers = this.timers;
var stop = function () {
timers.remove(stop);
stop.timer = undefined;
stop = undefined;
};
function execute() {
if (fn) fn();
if (stop && stop.then) stop.then();
stop();
}
stop.remain = seconds;
stop.seconds = seconds;
stop.pct = function () {
return 1 - stop.remain / stop.seconds;
};
stop.timer = os.make_timer(execute);
stop.timer.remain = seconds;
timers.push(stop);
return stop;
};
actor.delay = function (fn, seconds) { prosperon.add_timer(this, fn, seconds); }
actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`;
actor.interval = function (fn, seconds) {

View file

@ -51,7 +51,7 @@ var sprite = {
self.path = playing.path;
function advance() {
function advance(time) {
if (!self) return;
if (!self.gameobject) return;
self.frame = playing.frames[f].rect;
@ -72,12 +72,11 @@ var sprite = {
self?.stop();
return;
}
// self?.anim_done?.();
// if (!self.loop) { self.stop(); return; }
}
if (self) stop = self.gameobject.delay(advance, playing.frames[f].time / self.anim_speed);
}
return playing.frames[f].time/self.anim_speed;
}
stop = self.gameobject.delay(advance, playing.frames[f].time/self.anim_speed);
advance();
},
tex_sync() {

View file

@ -1,8 +1,21 @@
globalThis.entityreport = {};
var timer_update = function (dt) {
this.fn();
var timer_manager = {};
timer_manager.timers = new Map();
timer_manager.make_timer = function(obj, fn, seconds)
{
var timer = os.make_timer(_ => {
fn();
this.timers.delete(obj);
});
this.timers.set(obj, timer);
return _ => {
if (this.timers.has(obj)) {
}
};
}
function obj_unique_name(name, obj) {
name = name.replaceAll(".", "_");
@ -70,32 +83,7 @@ var entity = {
for (var o of Object.values(this.objects)) o.sync();
},
delay(fn, seconds) {
var timers = this.timers;
var stop = function () {
timers.remove(stop);
execute = undefined;
stop.timer = undefined;
stop = undefined;
};
function execute() {
fn();
stop?.();
}
stop.remain = seconds;
stop.seconds = seconds;
stop.pct = function () {
return 1 - stop.remain / stop.seconds;
};
stop.timer = os.make_timer(execute);
stop.timer.remain = seconds;
timers.push(stop);
return stop;
},
delay(fn, seconds) { prosperon.add_timer(this, fn, seconds); },
cry(file) {
return audio.cry(file);

View file

@ -418,6 +418,36 @@ var Event = {
},
};
prosperon.add_timer = function(obj, fn, seconds)
{
var timers = obj.timers;
var stop = function () {
timers.remove(stop);
timer.fn = undefined;
timer = undefined;
};
function execute() {
if (fn)
timer.remain = fn(stop.seconds);
if (!timer.remain)
stop();
else
stop.seconds = timer.remain;
}
var timer = os.make_timer(execute);
timer.remain = seconds;
stop.remain = seconds;
stop.seconds = seconds;
timers.push(stop);
return stop;
}
global.mixin("scripts/spline");
global.mixin("scripts/components");
global.mixin("scripts/actor");

View file

@ -1,5 +1,8 @@
#include "jsffi.h"
//#define STB_LEAKCHECK_IMPLEMENTATION
//#include "stb/stb_leakcheck.h"
#include "script.h"
#include "font.h"
#include "gameobject.h"
@ -2665,10 +2668,12 @@ static const JSCFunctionListEntry js_texture_funcs[] = {
MIST_FUNC_DEF(texture, inram, 0),
};
JSC_GETSET_CALLBACK(timer, fn)
JSC_GETSET(timer, remain, number)
static const JSCFunctionListEntry js_timer_funcs[] = {
CGETSET_ADD(timer, remain),
CGETSET_ADD(timer, fn),
};
JSC_GETSET(font, linegap, number)

View file

@ -75,7 +75,14 @@ JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue self) { \
#define JSC_GETSET(ID, ENTRY, TYPE) GETSETPAIR( ID , ENTRY , TYPE , ; )
#define JSC_GETSET_APPLY(ID, ENTRY, TYPE) GETSETPAIR(ID, ENTRY, TYPE, ID##_apply(js2##ID (self));)
#define JSC_GETSET_HOOK(ID, ENTRY) GETSETPAIR(ID, ENTRY, JSValue, ;)
#define JSC_GETSET_CALLBACK(ID, ENTRY) \
JSValue js_##ID##_set_##ENTRY (JS_SETSIG) { \
JSValue fn = js2##ID (self)->ENTRY; \
if (!JS_IsUndefined(fn)) JS_FreeValue(js, fn); \
js2##ID (self)->ENTRY = JS_DupValue(js, val); \
return JS_UNDEFINED; \
}\
JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue self) { return JS_DupValue(js, js2##ID (self)->ENTRY); } \
#define JSC_GETSET_GLOBAL(ENTRY, TYPE) \
JSValue js_global_set_##ENTRY (JS_SETSIG) { \