Add back in ai and tween, and fix

This commit is contained in:
John Alanbrook 2024-04-01 08:13:57 -05:00
parent 1195e6c73d
commit 3b9a932c85
14 changed files with 114 additions and 77 deletions

View file

@ -2,26 +2,28 @@ var ai = {
race(list) {
return function(dt) {
var good = false;
list.forEach(function(x) { if (x.call(this,dt)) good = true; }, this);
for (var i = 0; i < list.length; i++)
if (list[i].call(this,dt)) good=true;
return good;
};
},
do(times, list) {
},
sequence(list) {
var i = 0;
return function(dt) {
var fn = function(dt) {
while (i !== list.length) {
if (list[i].call(this,dt))
i++;
else
return false;
i++;
else
return false;
}
if (fn.done) fn.done();
return true;
};
fn.restart = function() { i = 0; };
return fn;
},
parallel(list) {
@ -31,22 +33,12 @@ var ai = {
return good;
};
},
moveto() {
return function(dt) {
var dir = this.randomloc.sub(this.pos);
if (Vector.length(dir) < 10) return true;
this.velocity = Vector.norm(this.randomloc.sub(this.pos)).scale(20);
return False;
}
},
move() {
return function(dt) {
this.velocity = this.left().scale(20);
return false;
}
dofor(secs, fn) {
return ai.race([
ai.wait(secs),
fn
]);
},
wait(secs) {
@ -54,8 +46,10 @@ var ai = {
var accum = 0;
return function(dt) {
accum += dt;
if (accum >= secs)
if (accum >= secs) {
accum = 0;
return true;
}
return false;
};

View file

@ -4,7 +4,7 @@ this.view2world = function(pos) { return render.view2world(pos); };
this.world2view = function(pos) { return render.world2view(pos); };
this.realzoom = function() { return render.get_zoom(); };
this.right = function() { return this.pos.x + (window.rendersize.x/2); }
this.left = function() { return this.pos.x - (window.rendersize.x/2); }
this.screenright = function() { return this.pos.x + (window.rendersize.x/2); }
this.screenleft = function() { return this.pos.x - (window.rendersize.x/2); }
this.zoom = 1;

View file

@ -322,6 +322,7 @@ var SpriteAnim = {
};
var data = json.decode(io.slurp(path));
if (!data?.meta?.app.includes("aseprite")) return;
var anims = {};
var frames = Array.isArray(data.frames) ? data.frames : Object.values(data.frames);
var f = 0;

View file

@ -362,6 +362,8 @@ global.mixin("scripts/std");
global.mixin("scripts/diff");
global.mixin("scripts/color");
global.mixin("scripts/gui");
global.mixin("scripts/tween");
global.mixin("scripts/ai");
var timer = {
update(dt) {

View file

@ -2,7 +2,7 @@ var keycodes = {
259: "back",
258: "tab",
257: "enter",
1: "escape",
256: "escape",
32: "space",
266: "pgup",
267: "pgdown",
@ -111,7 +111,7 @@ prosperon.droppedfile = function(path)
var mousepos = [0,0];
prosperon.textinput = function(){};
prosperon.textinput = function(l){};
prosperon.mousemove = function(pos, dx){
mousepos = pos;
player[0].mouse_input(modstr() + "move", pos, dx);

View file

@ -135,10 +135,12 @@ render.text = function(str, pos, size, color, wrap, anchor, cursor) {
return bb;
};
render.image = function(tex, pos, rotation, color) {
render.image = function(tex, pos, rotation, color, dimensions) {
color ??= Color.white;
rotation ??= 0;
gui.img(tex,pos, [1.0,1.0], 0.0, false, [0.0,0.0], color);
dimensions ??= [tex.width, tex.height];
var scale = [dimensions.x/tex.width, dimensions.y/tex.height];
gui.img(tex,pos, scale, 0.0, false, [0.0,0.0], color);
return bbox.fromcwh([0,0], [tex.width,tex.height]);
}

View file

@ -17,6 +17,33 @@ audio.bus.master = dspsound.master();
audio.dsp = {};
audio.dsp = dspsound;
var cries = [];
audio.cry = function(file)
{
var player = audio.play(file);
cries.push(player);
var hook = function() { console.warn("ENDED SOUND!!!!"); cries.remove(player); player = undefined; hook = undefined;}
player.hook = hook;
}
var song;
audio.music = function(file, fade) {
fade ??= 0;
if (!fade) {
song = audio.play(file);
console.info("STRAIGHT PLAY");
return;
}
var temp = audio.play(file);
temp.volume = 0;
var temp2 = song;
tween(temp, 'volume', 1, fade);
tween(temp2, 'volume', 0, fade);
song = temp;
}
audio.dsp.mix = function(to) {
var n = audio.dsp.mix();
if (to) n.plugin(to);

View file

@ -280,7 +280,7 @@ Cmdline.register_order("play", function(argv) {
game.engine_start(function() {
global.mixin("scripts/sound.js");
global.app = actor.spawn("game.js");
if (project.icon) window.set_icon(project.icon);
if (project.icon) window.set_icon(game.texture(project.icon));
});
}, "Play the game present in this folder.");

View file

@ -103,6 +103,21 @@ Ease.elastic = {
Ease.elastic.c4 = 2*Math.PI/3;
Ease.elastic.c5 = 2*Math.PI / 4.5;
var tween = function(obj, val, to, time)
{
var start = profile.secs(profile.now());
var startval = obj[val];
var update = function(dt) {
var elapsed = profile.secs(profile.now()) - start;
obj[val] = startval.lerp(to, elapsed/time);
if (elapsed >= time) {
obj[val] = to;
stop();
}
};
var stop = Register.update.register(update);
}
var Tween = {
default: {
loop: "restart",
@ -142,12 +157,12 @@ var Tween = {
if (defn.accum >= defn.time && defn.loop === 'hold') {
if (typeof target === 'string')
obj[target] = tvals[tvals.length-1];
else
target(tvals[tvals.length-1]);
else
target(tvals[tvals.length-1]);
defn.pause();
defn.cb.call(obj);
return;
defn.cb.call(obj);
return;
}
defn.pct = (defn.accum % defn.time) / defn.time;
@ -197,4 +212,4 @@ var Tween = {
Tween.make = Tween.start;
return {Tween, Ease};
return {Tween, Ease, tween};

View file

@ -24,6 +24,8 @@
#include "sokol/sokol_gfx.h"
#include "sokol_gfx_ext.h"
#include "crt.sglsl.h"
#include "msf_gif.h"
HMM_Vec2 campos = {0,0};
@ -38,6 +40,12 @@ static struct {
sg_image depth;
} sg_gif;
static struct {
sg_pipeline pipe;
sg_bindings bind;
sg_shader shader;
} sg_crt;
static struct {
int w;
int h;
@ -217,7 +225,7 @@ void render_init() {
pass_action = (sg_pass_action){
.colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = c},
};
sg_gif.shader = sg_make_shader(box_shader_desc(sg_query_backend()));
sg_gif.pipe = sg_make_pipeline(&(sg_pipeline_desc){
@ -225,14 +233,13 @@ void render_init() {
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2,
[1].format = SG_VERTEXFORMAT_FLOAT2
[1].format = SG_VERTEXFORMAT_FLOAT2
}
},
.colors[0].pixel_format = SG_PIXELFORMAT_RGBA8,
.label = "gif pipe",
});
#if defined SOKOL_GLCORE33 || defined SOKOL_GLES3
float crt_quad[] = {
-1, 1, 0, 1,
-1, -1, 0, 0,
@ -241,16 +248,7 @@ void render_init() {
1, -1, 1, 0,
1, 1, 1, 1
};
#else
float crt_quad[] = {
-1, 1, 0, 0,
-1, -1, 0, 1,
1, -1, 1, 1,
-1, 1, 0, 0,
1, -1, 1, 1,
1, 1, 1, 0
};
#endif
float gif_quad[] = {
-1, 1, 0, 1,
-1, -1, 0, 0,
@ -265,6 +263,24 @@ void render_init() {
.data = gif_quad,
});
sg_gif.bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
sg_crt.shader = sg_make_shader(crt_shader_desc(sg_query_backend()));
sg_crt.pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = sg_crt.shader,
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2,
[1].format = SG_VERTEXFORMAT_FLOAT2
}
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
});
sg_crt.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(crt_quad),
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_IMMUTABLE
});
}
HMM_Vec2 world2screen(HMM_Vec2 pos)
@ -360,23 +376,6 @@ void openglRender(struct window *window, gameobject *cam, float zoom) {
*/
}
sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d)
{
YughInfo("Making shader with %s and %s", v, f);
char *vs = slurp_text(v, NULL);
char *fs = slurp_text(f, NULL);
d->vs.source = vs;
d->fs.source = fs;
d->label = v;
sg_shader ret = sg_make_shader(d);
free(vs);
free(fs);
return ret;
}
struct boundingbox cwh2bb(HMM_Vec2 c, HMM_Vec2 wh) {
struct boundingbox bb = {
.t = c.Y + wh.Y/2,

View file

@ -67,8 +67,6 @@ void capture_screen(int x, int y, int w, int h, const char *path);
HMM_Vec2 world2screen(HMM_Vec2 pos);
HMM_Vec2 screen2world(HMM_Vec2 pos);
sg_shader sg_compile_shader(const char *v, const char *f, sg_shader_desc *d);
void gif_rec_start(int w, int h, int cpf, int bitdepth);
void gif_rec_end(const char *path);

View file

@ -278,7 +278,7 @@ void sound_fillbuf(struct sound *s, soundbyte *buf, int n) {
if(end) {
if (s->loop)
s->frame = 0;
YughInfo("CALLING HOOK");
script_call_sym(s->hook,0,NULL);
}
}

View file

@ -115,7 +115,7 @@ void c_event(const sapp_event *e)
break;
case SAPP_EVENTTYPE_CHAR:
script_evalf("prosperon.textinput(`%lc`);", e->char_code);
script_evalf("prosperon.textinput(`\\%lc`);", e->char_code);
break;
case SAPP_EVENTTYPE_RESIZED:

View file

@ -17,13 +17,12 @@ out vec4 frag_color;
uniform texture2D diffuse;
uniform sampler smp;
uniform mpara {
uniform vec2 screensize;
};
void main()
{
frag_color = texture(sampler2D(diffuse,smp), TexCoords);
return;
/* vec2 screensize = textureSize(diffuse,0);
vec4 color = texture(sampler2D(diffuse,smp), TexCoords);
float avg = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
frag_color = vec4(avg,avg,avg,1.0);
@ -39,7 +38,7 @@ void main()
float b = color.b;
frag_color = vec4(r, g*0.99, b, 1.0) * clamp(line_intensity, 0.85, 1.0);
*/
}
@end