add tween; fix collision js ref bug

This commit is contained in:
John Alanbrook 2023-12-28 13:57:22 +00:00
parent 2195f9f5db
commit c4b2b23941
6 changed files with 201 additions and 14 deletions

View file

@ -81,8 +81,8 @@
/* dump object free */
#ifdef DBG
//#define DUMP_FREE
//#define DUMP_CLOSURE
#define DUMP_FREE
#define DUMP_CLOSURE
/* dump the bytecode of the compiled functions: combination of bits
1: dump pass 3 final byte code
2: dump pass 2 code
@ -91,7 +91,7 @@
16: dump bytecode in hex
32: dump line number table
*/
//#define DUMP_BYTECODE (1)
#define DUMP_BYTECODE (1)
/* dump the occurence of the automatic GC */
#define DUMP_GC
/* dump objects freed by the garbage collector */
@ -103,9 +103,9 @@
#define DUMP_OBJECTS /* dump objects in JS_FreeContext */
#define DUMP_ATOMS /* dump atoms in JS_FreeContext */
#define DUMP_SHAPES /* dump shapes in JS_FreeContext */
//#define DUMP_MODULE_RESOLVE
//#define DUMP_PROMISE
//#define DUMP_READ_OBJECT
#define DUMP_MODULE_RESOLVE
#define DUMP_PROMISE
#define DUMP_READ_OBJECT
#endif
/* test the GC by forcing it before each object allocation */
//#define FORCE_GC_AT_MALLOC

View file

@ -94,7 +94,7 @@ var timer = {
},
};
load("scripts/animation.js");
load("scripts/tween.js");
var Render = {
normal() { cmd(67);},
@ -526,6 +526,7 @@ function world_start() {
Primum.toString = function() { return "Primum"; };
Primum.ur = "Primum";
Primum.kill = function() { this.clear(); };
Primum.phys = 2;
gameobject.level = Primum;
}
@ -535,7 +536,6 @@ Game.view_camera = function(cam)
{
Game.camera = cam;
cmd(61, Game.camera.body);
// cam.zoom = cam.zoom;
}
Window.name = "Primum Machinam (V0.1)";

188
scripts/tween.js Normal file
View file

@ -0,0 +1,188 @@
/* Take numbers from 0 to 1 and remap them to easing functions */
var Ease = {
linear(t) { return t; },
in(t) { return t*t; },
out(t) {
var d = 1-t;
return 1 - d*d
},
inout(t) {
var d = -2*t + 2;
return t < 0.5 ? 2 * t * t : 1 - (d * d) / 2;
},
};
function make_easing_fns(num) {
var obj = {};
obj.in = function(t) {
return Math.pow(t,num);
};
obj.out = function(t) {
return 1 - Math.pow(1 - t, num);
};
var mult = Math.pow(2, num-1);
obj.inout = function(t) {
return t < 0.5 ? mult * Math.pow(t, num) : 1 - Math.pow(-2 * t + 2, num) / 2;
};
return obj;
};
Ease.quad = make_easing_fns(2);
Ease.cubic = make_easing_fns(3);
Ease.quart = make_easing_fns(4);
Ease.quint = make_easing_fns(5);
Ease.expo = {
in(t) {
return t === 0 ? 0 : Math.pow(2, 10 * t - 10);
},
out(t) {
return t === 1 ? 1 : 1 - Math.pow(2, -10 * t);
},
inout(t) {
return t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2;
}
};
Ease.bounce = {
in(t) {
return 1 - this.out(t - 1);
},
out(t) {
var n1 = 7.5625;
var d1 = 2.75;
if (t < 1 / d1) { return n1 * t * t; }
else if (t < 2 / d1) { return n1 * (t -= 1.5 / d1) * t + 0.75; }
else if (t < 2.5 / d1) { return n1 * (t -= 2.25 / d1) * t + 0.9375; }
else
return n1 * (t -= 2.625 / d1) * t + 0.984375;
},
inout(t) {
return t < 0.5 ? (1 - this.out(1 - 2 * t)) / 2 : (1 + this.out(2 * t - 1)) / 2;
}
};
Ease.sine = {
in(t) { return 1 - Math.cos((t * Math.PI)/2); },
out(t) { return Math.sin((t*Math.PI)/2); },
inout(t) { return -(Math.cos(Math.PI*t) - 1) / 2; }
};
Ease.elastic = {
in(t) {
return t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10*t-10) * Math.sin((t * 10 - 10.75) * this.c4);
},
out(t) {
return t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10*t) * Math.sin((t * 10 - 0.75) * this.c4) + 1;
},
inout(t) {
t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ?
-(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2
: (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 + 1;
},
};
Ease.elastic.c4 = 2*Math.PI/3;
Ease.elastic.c5 = 2*Math.PI / 4.5;
var Tween = {
default: {
loop: "restart",
/*
loop types
none: when done, return to first value
hold: hold last value of tween
restart: restart at beginning, looping
yoyo: go up and then back down
circle: go up and back down, looped
*/
time: 1, /* seconds to do */
ease: Ease.linear,
whole: true, /* True if time is for the entire tween, false if each stage */
cb: function(){},
},
start(obj, target, tvals, options)
{
var defn = Object.create(this.default);
Object.assign(defn, options);
if (defn.loop === 'circle')
tvals.push(tvals[0]);
else if (defn.loop === 'yoyo') {
for (var i = tvals.length-2; i >= 0; i--)
tvals.push(tvals[i]);
}
defn.accum = 0;
var slices = tvals.length - 1;
var slicelen = 1 / slices;
defn.fn = function(dt) {
defn.accum += dt;
if (defn.accum >= defn.time && defn.loop === 'hold') {
obj[target] = tvals[tvals.length-1];
defn.pause();
defn.cb.call(obj);
return;
}
defn.pct = (defn.accum % defn.time) / defn.time;
if (defn.loop === 'none' && defn.accum >= defn.time)
defn.stop();
var t = defn.whole ? defn.ease(defn.pct) : defn.pct;
var nval = t / slicelen;
var i = Math.trunc(nval);
nval -= i;
if (!defn.whole)
nval = defn.ease(nval);
obj[target] = tvals[i].lerp(tvals[i+1], nval);
};
var playing = false;
defn.play = function() {
if (playing) return;
Register.update.register(defn.fn, defn);
playing = true;
};
defn.restart = function() {
defn.accum = 0;
obj[target] = tvals[0];
};
defn.stop = function() { if (!playing) return; defn.pause(); defn.restart(); };
defn.pause = function() {
Register.update.unregister(defn.fn);
if (!playing) return;
playing = false;
};
return defn;
},
};
Tween.make = Tween.start;

View file

@ -525,7 +525,7 @@ static struct postphys_cb *begins = NULL;
void flush_collide_cbs() {
for (int i = 0; i < arrlen(begins); i++) {
script_callee(begins[i].c, 1, &begins[i].send);
// JS_FreeValue(js, begins[i].send);
JS_FreeValue(js, begins[i].send);
}
arrsetlen(begins,0);
@ -538,7 +538,7 @@ void duk_call_phys_cb(HMM_Vec2 norm, struct callee c, gameobject *hit, cpArbiter
JSValue obj = JS_NewObject(js);
JS_SetPropertyStr(js, obj, "normal", vec2js(norm));
JS_SetPropertyStr(js, obj, "obj", hit->ref);
JS_SetPropertyStr(js, obj, "obj", JS_DupValue(js,hit->ref));
JS_SetPropertyStr(js, obj, "sensor", JS_NewBool(js, cpShapeGetSensor(shape2)));
HMM_Vec2 srfv;
srfv.cp = cpArbiterGetSurfaceVelocity(arb);

View file

@ -73,10 +73,8 @@ void script_stop()
{
script_evalf("Event.notify('quit');");
send_signal("quit",0,NULL);
for (int i = 0; i < shlen(jsstrs); i++)
JS_FreeValue(js,jsstrs[i].value);
JS_FreeContext(js);
JS_FreeRuntime(rt);
}
@ -185,6 +183,7 @@ time_t jso_file(const char *file)
JSValue ret = JS_EvalFunction(js, obj);
js_print_exception(ret);
JS_FreeValue(js,ret);
JS_FreeValue(js,obj);
free(byte);
return file_mod_secs(file);
}

View file

@ -276,12 +276,12 @@ int main(int argc, char **argv) {
snprintf(fname, 100, "yugine-%ld.log", now);
log_setfile(fname);
}
/*
signal(SIGSEGV, seghandle);
signal(SIGABRT, seghandle);
signal(SIGFPE, seghandle);
signal(SIGBUS, seghandle);
*/
#endif
#ifdef STEAM