sprite color mixing; make timers and tweens workable

This commit is contained in:
John Alanbrook 2023-10-31 17:38:23 +00:00
parent ecd31eeafa
commit 05a596746e
8 changed files with 71 additions and 74 deletions

View file

@ -53,7 +53,7 @@ var component = {
component.sprite = Object.copy(component, { component.sprite = Object.copy(component, {
pos:[0,0], pos:[0,0],
color:[1,1,1], color:[1,1,1,1],
layer:0, layer:0,
enabled:true, enabled:true,
path: "", path: "",
@ -64,10 +64,12 @@ component.sprite = Object.copy(component, {
component.sprite.impl = { component.sprite.impl = {
set path(x) { set path(x) {
cmd(12,this.id,prototypes.resani(this.gameobject.__proto__.toString(), x),this.rect); //cmd(12,this.id,prototypes.resani(this.gameobject.__proto__.toString(), x),this.rect);
cmd(12,this.id,x,this.rect);
}, },
get path() { get path() {
return prototypes.resavi(this.gameobject.__proto__.toString(), cmd(116,this.id)); return cmd(116,this.id);
//return prototypes.resavi(this.gameobject.__proto__.toString(), cmd(116,this.id));
}, },
toString() { return "sprite"; }, toString() { return "sprite"; },
hide() { this.enabled = false; }, hide() { this.enabled = false; },
@ -76,7 +78,7 @@ component.sprite.impl = {
get enabled() { return cmd(114,this.id); }, get enabled() { return cmd(114,this.id); },
set enabled(x) { cmd(20,this.id,x); }, set enabled(x) { cmd(20,this.id,x); },
set color(x) { cmd(96,this.id,x); }, set color(x) { cmd(96,this.id,x); },
get color() {return undefined; }, get color() {return cmd(148,this.id);},
get pos() { return cmd(111, this.id); }, get pos() { return cmd(111, this.id); },
set pos(x) { cmd(37,this.id,x); }, set pos(x) { cmd(37,this.id,x); },
set layer(x) { cmd(60, this.id, x); }, set layer(x) { cmd(60, this.id, x); },

View file

@ -240,15 +240,19 @@ var timer = {
} }
var t = Object.create(this); var t = Object.create(this);
assign_impl(t, this.impl); // assign_impl(t, this.impl);
t.callback = fn; t.callback = fn;
var guardfn = function() { var guardfn = function() {
if (typeof t.callback === 'function') if (typeof t.callback === 'function') {
t.callback(); t.callback();
if (!t.loop) t.kill();
}
else else
Log.warn("Timer trying to execute without a function."); Log.warn("Timer trying to execute without a function.");
}; };
t.id = make_timer(guardfn, secs, app); t.id = make_timer(guardfn, secs, app);
t.loop = loop;
return t; return t;
}, },
@ -258,7 +262,7 @@ var timer = {
var t = this.make(fn, secs, obj, 0, app); var t = this.make(fn, secs, obj, 0, app);
t.start(); t.start();
}, },
impl: { // impl: {
get remain() { return cmd(32, this.id); }, get remain() { return cmd(32, this.id); },
get on() { return cmd(33, this.id); }, get on() { return cmd(33, this.id); },
get loop() { return cmd(34, this.id); }, get loop() { return cmd(34, this.id); },
@ -271,17 +275,17 @@ var timer = {
set time(x) { cmd(28, this.id, x); }, set time(x) { cmd(28, this.id, x); },
get time() { return cmd(29, this.id); }, get time() { return cmd(29, this.id); },
get pct() { return this.remain / this.time; }, get pct() { return this.remain / this.time; },
}, // },
remain: 0, // remain: 0,
on: false, // on: false,
loop: false, // loop: false,
start(){}, // start(){},
stop(){}, // stop(){},
kill(){}, // kill(){},
pause(){}, // pause(){},
time: 0, // time: 0,
pct: 0, // pct: 0,
}; };
timer.doc = { timer.doc = {
@ -480,7 +484,7 @@ var Register = {
} }
n.unregister = function(fn) { n.unregister = function(fn) {
entries = entries.filter(function(e) { return e.fn !== f; }); entries = entries.filter(function(e) { return e.fn !== fn; });
} }
n.unregister_obj = function(obj) { n.unregister_obj = function(obj) {
@ -528,7 +532,7 @@ var Signal = {
register_collide(3,fn,obj,go.body); register_collide(3,fn,obj,go.body);
}, },
clera_obj(obj) { clear_obj(obj) {
this.signals.filter(function(x) { return x[1] !== obj; }); this.signals.filter(function(x) { return x[1] !== obj; });
}, },
@ -767,6 +771,8 @@ Game.doc.stop = "Stop game simulation. This does the same thing as 'pause', and
Game.doc.play = "Resume or start game simulation."; Game.doc.play = "Resume or start game simulation.";
Game.doc.editor_mode = "Set to true for the game to only update on input; otherwise the game updates every frame."; Game.doc.editor_mode = "Set to true for the game to only update on input; otherwise the game updates every frame.";
Game.doc.dt = "Current frame dt."; Game.doc.dt = "Current frame dt.";
Game.doc.view_camera = "Set the camera for the current view.";
Game.doc.camera = "Current camera.";
Window.doc = {}; Window.doc = {};
Window.doc.width = "Width of the game window."; Window.doc.width = "Width of the game window.";

View file

@ -416,6 +416,8 @@ var Tween = {
defn.fn = function(dt) { defn.fn = function(dt) {
defn.accum += dt; defn.accum += dt;
defn.pct = (defn.accum % defn.time) / defn.time; 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 t = defn.whole ? defn.ease(defn.pct) : defn.pct;
@ -429,54 +431,26 @@ var Tween = {
obj[target] = tvals[i].lerp(tvals[i+1], nval); obj[target] = tvals[i].lerp(tvals[i+1], nval);
}; };
defn.restart = function() { defn.accum = 0; }; var playing = false;
defn.stop = function() { defn.pause(); defn.restart(); };
defn.pause = function() { Register.update.unregister(defn.fn); };
Register.update.register(defn.fn, defn); defn.play = function() {
if (playing) return;
return defn; Register.update.register(defn.fn, defn);
}, playing = true;
};
embed(obj, target, tvals, options) { defn.restart = function() {
var defn = Object.create(this.default); defn.accum = 0;
Object.assign(defn, options); obj[target] = tvals[0];
};
defn.update_vals = function(vals) { defn.stop = function() { if (!playing) return; defn.pause(); defn.restart(); };
defn.vals = vals; defn.pause = function() {
if (!playing) return;
if (defn.loop === 'circle') Register.update.unregister(defn.fn);
defn.vals.push(defn.vals[0]); playing = false;
else if (defn.loop === 'yoyo') {
for (var i = defn.vals.length-2; i >= 0; i--)
defn.vals.push(defn.vals[i]);
}
defn.slices = defn.vals.length - 1;
defn.slicelen = 1 / defn.slices;
}; };
defn.update_vals(tvals);
defn.time_s = Date.now();
Object.defineProperty(obj, target, {
get() {
defn.accum = (Date.now() - defn.time_s)/1000;
defn.pct = (defn.accum % defn.time) / defn.time;
var t = defn.whole ? defn.ease(defn.pct) : defn.pct;
var nval = t / defn.slicelen;
var i = Math.trunc(nval);
nval -= i;
if (!defn.whole)
nval = defn.ease(nval);
return defn.vals[i].lerp(defn.vals[i+1],nval);
},
});
return defn; return defn;
}, },
}; };
Tween.make = Tween.start;

View file

@ -144,6 +144,7 @@ int cpshape_enabled(cpShape *c) {
} }
struct rgba shape_color(cpShape *shape) { struct rgba shape_color(cpShape *shape) {
if (!cpshape_enabled(shape)) return disabled_color;
switch (cpBodyGetType(cpShapeGetBody(shape))) { switch (cpBodyGetType(cpShapeGetBody(shape))) {
case CP_BODY_TYPE_DYNAMIC: case CP_BODY_TYPE_DYNAMIC:
// cpBodySleep(cpShapeGetBody(shape)); // cpBodySleep(cpShapeGetBody(shape));

View file

@ -195,6 +195,16 @@ struct rgba js2color(JSValue v) {
return color; return color;
} }
JSValue color2js(struct rgba color)
{
JSValue arr = JS_NewArray(js);
js_setprop_num(arr,0,JS_NewFloat64(js,(double)color.r/255));
js_setprop_num(arr,1,JS_NewFloat64(js,(double)color.g/255));
js_setprop_num(arr,2,JS_NewFloat64(js,(double)color.b/255));
js_setprop_num(arr,3,JS_NewFloat64(js,(double)color.a/255));
return arr;
}
struct boundingbox js2bb(JSValue v) struct boundingbox js2bb(JSValue v)
{ {
struct boundingbox bb; struct boundingbox bb;
@ -1110,6 +1120,9 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 147: case 147:
exit(js2int(argv[1])); exit(js2int(argv[1]));
break; break;
case 148:
ret = color2js(id2sprite(js2int(argv[1]))->color);
break;
} }
if (str) if (str)

View file

@ -11,7 +11,7 @@ struct timer {
void *data; void *data;
int owndata; int owndata;
int next; int next;
int app; int app; /* True if this timer is an "app" timer, and should always update; otherwise, only update with game time */
}; };
int timer_make(double interval, void (*callback)(void *param), void *param, int own, int app); int timer_make(double interval, void (*callback)(void *param), void *param, int own, int app);

View file

@ -154,13 +154,12 @@ int frame_fps() {
static void process_frame() static void process_frame()
{ {
double elapsed = stm_sec(stm_laptime(&frame_t)); double elapsed = stm_sec(stm_laptime(&frame_t));
physlag += elapsed;
input_poll(0); input_poll(0);
/* Timers all update every frame - once per monitor refresh */ /* Timers all update every frame - once per monitor refresh */
timer_update(elapsed, timescale); timer_update(elapsed, timescale);
if (sim_play == SIM_PLAY || sim_play == SIM_STEP && stm_sec(stm_diff(frame_t, updatelast)) > updateMS) { if (sim_play == SIM_PLAY || sim_play == SIM_STEP) {
if (stm_sec(stm_diff(frame_t, updatelast)) > updateMS) {
double dt = stm_sec(stm_diff(frame_t, updatelast)); double dt = stm_sec(stm_diff(frame_t, updatelast));
updatelast = frame_t; updatelast = frame_t;
prof_start(&prof_update); prof_start(&prof_update);
@ -171,7 +170,8 @@ static void process_frame()
sim_pause(); sim_pause();
} }
while ((sim_play == SIM_PLAY || sim_play == SIM_STEP) && physlag > physMS) { physlag += elapsed;
while (physlag > physMS) {
physlag -= physMS; physlag -= physMS;
prof_start(&prof_physics); prof_start(&prof_physics);
phys_step = 1; phys_step = 1;
@ -180,6 +180,7 @@ static void process_frame()
phys_step = 0; phys_step = 0;
prof(&prof_physics); prof(&prof_physics);
} }
}
prof_start(&prof_draw); prof_start(&prof_draw);
window_render(&mainwin); window_render(&mainwin);
@ -378,5 +379,5 @@ int main(int argc, char **argv) {
double apptime() double apptime()
{ {
return stm_sec(stm_diff(start_t, stm_now())); return stm_sec(stm_diff(stm_now(), start_t));
} }

View file

@ -31,7 +31,7 @@ void main()
if (color.a <= 0.1f) if (color.a <= 0.1f)
discard; discard;
color = mix(color, fcolor, 0.01); color *=fcolor;
} }
@end @end