timers fixed

This commit is contained in:
John Alanbrook 2023-11-07 18:45:52 +00:00
parent 166857a6df
commit 8c3e8aa539
12 changed files with 66 additions and 32 deletions

View file

@ -252,7 +252,7 @@ crosswin:
clean:
@echo Cleaning project
@rm -rf bin dist
@rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer TAGS scripts/*.jso
@rm -f shaders/*.sglsl.h shaders/*.metal core.cdb jso cdb packer scripts/*.jso
@make -C quickjs clean
TAGINC != find . -name "*.[chj]"

View file

@ -215,7 +215,6 @@ var editor = {
Primum.clear();
load("config.js");
Game.play();
Game.editor_mode(false);
Player.players[0].uncontrol(this);
Player.players[0].control(limited_editor);
Register.unregister_obj(this);
@ -849,7 +848,6 @@ editor.inputs['M-p'].doc = "Do one time step, pausing if necessary.";
editor.inputs['C-M-p'] = function() {
if (!Game.playing()) {
editor.start_play_ed();
Game.editor_mode(false);
}
Log.warn(`Starting edited level ...`);
};
@ -1544,7 +1542,7 @@ var replpanel = Object.copy(inputpanel, {
return [
Mum.text({str:log, anchor:[0,0], offset:[0,-300].sub(this.scrolloffset), selectable: true}),
Mum.text({str:this.value,color:Color.purple, offset:[0,-290], caret: this.caret})
Mum.text({str:this.value,color:Color.green, offset:[0,-290], caret: this.caret})
];
},
prevmark:-1,

View file

@ -239,6 +239,7 @@ load("scripts/gui.js");
var timer = {
make(fn, secs,obj,loop,app) {
obj ??= globalThis;
app ??= false;
if (secs === 0) {
fn.call(obj);
@ -246,17 +247,7 @@ var timer = {
}
var t = Object.create(this);
t.callback = fn;
var guardfn = function() {
if (typeof t.callback === 'function') {
t.callback();
if (!t.loop) t.kill();
}
else
Log.warn("Timer trying to execute without a function.");
};
t.id = make_timer(guardfn, secs, app);
t.id = make_timer(fn, secs, app, obj);
t.loop = loop;
t.pause();

View file

@ -10,6 +10,13 @@ function grab_from_points(pos, points, slop) {
return idx;
};
var actor = {};
actor.spawn = function(ur, config){};
actor.die = function(actor){};
actor.kill = function(actor){};
actor.delay = function(fn, seconds) {};
actor.clock = function(fn){};
var gameobject = {
impl: {
full_path() {
@ -35,6 +42,12 @@ var gameobject = {
this.objects = {};
},
timers:[],
delay(fn, seconds) {
var t = timer.oneshot(fn, seconds, this, false);
return function() { t.kill(); };
},
set max_velocity(x) { cmd(151, this.body, x); },
get max_velocity() { return cmd(152, this.body); },
set max_angularvelocity(x) { cmd(154, this.body, Math.deg2rad(x)); },
@ -286,6 +299,9 @@ var gameobject = {
if (typeof obj.debug === 'function')
Register.debug.register(obj.debug, obj);
if (typeof obj.gui === 'function')
Register.gui.register(obj.gui,obj);
obj.components.forEach(function(x) {
if (typeof x.collide === 'function')
register_collide(1, x.collide, x, obj.body, x.shape);
@ -638,6 +654,7 @@ gameobject.doc = {
transform: `Return an object representing the transform state of this object.`,
kill: `Remove this object from the world.`,
level: "The entity this entity belongs to.",
delay: 'Run the given function after the given number of seconds has elapsed.',
};
/* Default objects */

View file

@ -252,6 +252,7 @@ Mum.image = Mum.extend({
Mum.column = Mum.extend({
draw(cursor, cnt) {
cnt ??= Mum;
if (this.hide) return;
cursor = cursor.add(this.offset);
this.max_width = cnt.width;

View file

@ -37,6 +37,10 @@ var physics = {
return cmd(86, box.pos, box.wh, points, points.length);
},
shape_query(shape) {
return cmd(80,shape);
},
};
physics.doc = {};

View file

@ -1673,11 +1673,8 @@ JSValue duk_anim(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
JSValue duk_make_timer(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
double secs = js2number(argv[1]);
struct callee *c = malloc(sizeof(*c));
c->fn = JS_DupValue(js, argv[0]);
c->obj = globalThis;
struct callee *c = make_callee(argv[0], argv[3]);
int id = timer_make(secs, call_callee, c, 1, js2bool(argv[2]));
return JS_NewInt64(js, id);
}
@ -1749,7 +1746,7 @@ void ffi_load() {
DUK_FUNC(make_edge2d, 3)
DUK_FUNC(cmd_edge2d, 6)
DUK_FUNC(make_model,2);
DUK_FUNC(make_timer, 3)
DUK_FUNC(make_timer, 4)
DUK_FUNC(cmd_points, 5);

View file

@ -71,13 +71,13 @@ void script_startup() {
void script_stop()
{
timers_free();
send_signal("quit",0,NULL);
for (int i = 0; i < shlen(jsstrs); i++)
JS_FreeValue(js,jsstrs[i].value);
JS_FreeContext(js);
JS_RunGC(rt);
JS_FreeRuntime(rt);
}
@ -271,6 +271,21 @@ void script_callee(struct callee c, int argc, JSValue *argv) {
js_callee_exec(&c, argc, argv);
}
struct callee *make_callee(JSValue fn, JSValue obj)
{
struct callee *c = malloc(sizeof(*c));
c->fn = JS_DupValue(js, fn);
c->obj = JS_DupValue(js, obj);
return c;
}
void free_callee(struct callee *c)
{
JS_FreeValue(js,c->fn);
JS_FreeValue(js,c->obj);
free(c);
}
void send_signal(const char *signal, int argc, JSValue *argv)
{
JSValue globalThis = JS_GetGlobalObject(js);

View file

@ -27,6 +27,8 @@ time_t jso_file(const char *file);
JSValue script_runfile(const char *file);
void script_update(double dt);
void script_draw();
struct callee *make_callee(JSValue fn, JSValue obj);
void free_callee(struct callee *c);
void duk_run_err();
void js_dump_stack();

View file

@ -382,20 +382,20 @@ void anim_load(struct anim2d *anim, const char *path) {
}
void anim_play(struct anim2d *anim) {
if (anim->playing)
return;
// if (anim->playing)
// return;
if (anim->frame == anim_frames(anim->anim))
anim->frame = 0;
// if (anim->frame == anim_frames(anim->anim))
// anim->frame = 0;
anim->playing = 1;
// anim->playing = 1;
if (anim->timer == NULL)
anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0));
else
timerr_settime(anim->timer, 1.f / anim->anim->ms);
// if (anim->timer == NULL)
// anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0));
// else
// timerr_settime(anim->timer, 1.f / anim->anim->ms);
timer_start(anim->timer);
// timer_start(anim->timer);
}
void anim_stop(struct anim2d *anim) {

View file

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stb_ds.h>
#include "script.h"
struct timer *timers;
static int first = -1;
@ -76,7 +77,7 @@ void timer_start(struct timer *t) {
void timer_remove(int id) {
struct timer *t = id2timer(id);
if (t->owndata) free(t->data);
free_callee(t->data);
t->next = first;
t->on = 0;
first = id;
@ -90,3 +91,9 @@ void timerr_settime(struct timer *t, double interval) {
struct timer *id2timer(int id) {
return &timers[id];
}
void timers_free()
{
for (int i = 0; i < arrlen(timers); i++)
free_callee(timers[i].data);
}

View file

@ -23,4 +23,6 @@ void timer_stop(struct timer *t);
void timer_update(double dt, double scale);
void timerr_settime(struct timer *t, double interval);
void timers_free();
#endif