From 7b8be5b4f8ea644b0660946f4a43210a7cece025 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 4 Mar 2024 15:18:11 -0600 Subject: [PATCH] actors have delay now; fix sprite rendering for painters algo --- scripts/actor.js | 20 ++++------- scripts/engine.js | 32 ++++++++++++++++++ scripts/entity.js | 70 +++------------------------------------ scripts/std.js | 6 ++-- source/engine/render.c | 6 +--- source/engine/resources.c | 2 -- source/engine/sprite.c | 5 --- 7 files changed, 48 insertions(+), 93 deletions(-) diff --git a/scripts/actor.js b/scripts/actor.js index b5e06b4..d6353e8 100644 --- a/scripts/actor.js +++ b/scripts/actor.js @@ -14,6 +14,7 @@ actor.spawn = function(script, config){ padawan.timers = []; padawan.master = this; Object.hide(padawan, "master","timers", "padawans"); + check_registers(padawan); this.padawans.push(padawan); return padawan; }; @@ -28,7 +29,9 @@ actor.rm_pawn = function(pawn) actor.timers = []; actor.kill = function(){ if (this.__dead__) return; - this.timers.forEach(t => t.kill()); + this.timers.forEach(t => t()); + Player.do_uncontrol(this); + Event.rm_obj(this); if (this.master) this.master.rm_pawn(this); this.padawans.forEach(p => p.kill()); this.padawans = []; @@ -40,20 +43,9 @@ actor.kill = function(){ actor.kill.doc = `Remove this actor and all its padawans from existence.`; actor.delay = function(fn, seconds) { - var t = Object.create(timer); - t.remain = seconds; - t.kill = () => { - timer.kill.call(t); - delete this.timers[t.toString()]; - } - t.fire = () => { - if (this.__dead__) return; - fn(); - t.kill(); - }; - Register.appupdate.register(t.update, t); + var t = timer.delay(fn.bind(this), seconds); this.timers.push(t); - return function() { t.kill(); }; + return t; }; actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`; diff --git a/scripts/engine.js b/scripts/engine.js index 74f6392..b111fee 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -29,6 +29,38 @@ function eval_env(script, env, file) return cmd(123,script,env,file); } +global.check_registers = function(obj) +{ + if (typeof obj.update === 'function') + obj.timers.push(Register.update.register(obj.update.bind(obj))); + + if (typeof obj.physupdate === 'function') + obj.timers.push(Register.physupdate.register(obj.physupdate.bind(obj))); + + if (typeof obj.collide === 'function') + register_collide(0, obj.collide.bind(obj), obj.body); + + if (typeof obj.separate === 'function') + register_collide(3,obj.separate.bind(obj), obj.body); + + if (typeof obj.draw === 'function') + obj.timers.push(Register.draw.register(obj.draw.bind(obj), obj)); + + if (typeof obj.debug === 'function') + obj.timers.push(Register.debug.register(obj.debug.bind(obj))); + + if (typeof obj.gui === 'function') + obj.timers.push(Register.gui.register(obj.gui.bind(obj))); + + for (var k in obj) { + if (!k.startswith("on_")) continue; + var signal = k.fromfirst("on_"); + Event.observe(signal, obj, obj[k]); + }; + +} + + eval_env.dov = `Counterpart to /load_env/, but with a string.`; function feval_env(file, env) diff --git a/scripts/entity.js b/scripts/entity.js index 02d42dc..4ee6221 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -11,40 +11,6 @@ function obj_unique_name(name, obj) return n; } -function check_registers(obj) -{ - if (typeof obj.update === 'function') - obj.timers.push(Register.update.register(obj.update.bind(obj))); - - if (typeof obj.physupdate === 'function') - obj.timers.push(Register.physupdate.register(obj.physupdate.bind(obj))); - - if (typeof obj.collide === 'function') - register_collide(0, obj.collide.bind(obj), obj.body); - - if (typeof obj.separate === 'function') - register_collide(3,obj.separate.bind(obj), obj.body); - - if (typeof obj.draw === 'function') - obj.timers.push(Register.draw.register(obj.draw.bind(obj), obj)); - - if (typeof obj.debug === 'function') - obj.timers.push(Register.debug.register(obj.debug.bind(obj))); - - if (typeof obj.gui === 'function') - obj.timers.push(Register.gui.register(obj.gui.bind(obj))); - - for (var k in obj) { - if (!k.startswith("on_")) continue; - var signal = k.fromfirst("on_"); - Event.observe(signal, obj, obj[k]); - }; - - obj.components.forEach(function(x) { - if (typeof x.collide === 'function') - register_collide(1, x.collide.bind(x), obj.body, x.shape); - }); -} var gameobject_impl = { get pos() { @@ -378,6 +344,11 @@ var gameobject = { }; check_registers(ent); + ent.components.forEach(function(x) { + if (typeof x.collide === 'function') + register_collide(1, x.collide.bind(x), ent.body, x.shape); + }); + if (typeof ent.load === 'function') ent.load(); if (Game.playing()) @@ -749,37 +720,6 @@ gameobject.doc = { warp_layer: 'Bitmask for selecting what warps should affect this entity.', }; -var resavi = function(ur, path) -{ - if (!ur) return path; - if (path[0] === '/') return path; - - var res = ur.replaceAll('.', '/'); - var dir = path.dir(); - if (res.startsWith(dir)) - return path.base(); - - return path; -} - -var resani = function(ur, path) -{ - if (!path) return ""; - if (!ur) return path; - if (path[0] === '/') return path.slice(1); - - var res = ur.replaceAll('.', '/'); - var restry = res + "/" + path; - while (!io.exists(restry)) { - res = res.updir() + "/"; - if (res === "/") - return path; - - restry = res + path; - } - return restry; -} - global.ur = {}; if (io.exists(".prosperon/ur.json")) diff --git a/scripts/std.js b/scripts/std.js index 62f3cf3..44970ea 100644 --- a/scripts/std.js +++ b/scripts/std.js @@ -365,8 +365,9 @@ Cmdline.register_order("play", function(argv) { if (project.title) Window.title(project.title); Game.engine_start(function() { - global.mixin("scripts/sound.js"); - global.mixin("game.js"); + global.mixin("scripts/sound.js"); + global.game = actor.spawn("game.js"); + say(`spawned game with ${game.score} points`); if (project.icon) Window.icon(project.icon); }); }, "Play the game present in this folder."); @@ -423,6 +424,7 @@ Cmdline.register_order("about", function(argv) { }, "Get information about this game."); Cmdline.register_order("ur", function(argv) { + Game.loadurs(); for (var i of ur._list.sort()) say(i); }, "Get information about the ur types in your game."); diff --git a/source/engine/render.c b/source/engine/render.c index 1b4e348..fe62897 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -75,7 +75,6 @@ void gif_rec_start(int w, int h, int cpf, int bitdepth) .render_target = true, .width = gif.w, .height = gif.h, -// .pixel_format = SG_PIXELFORMAT_DEPTH, .label = "gif depth", }); @@ -255,7 +254,6 @@ void render_init() { mainwin.height = sapp_height(); sg_setup(&(sg_desc){ .environment = sglue_environment(), -// .mtl_force_managed_storage_mode = 1, .logger = { .func = sg_logging }, .buffer_pool_size = 1024 }); @@ -320,7 +318,6 @@ void render_init() { .size = sizeof(gif_quad), .data = gif_quad, }); -// sg_gif.bind.fs.images[0] = crt_post.img; sg_gif.bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){}); /* @@ -367,7 +364,6 @@ void render_init() { void render_winsize() { -// sg_gif.bind.fs.images[0] = crt_post.img; } static cpBody *camera = NULL; @@ -402,7 +398,7 @@ HMM_Vec3 dirl_pos = {4, 100, 20}; void full_2d_pass(struct window *window) { - //////////// 2D projection + // 2D projection cpVect pos = cam_pos(); projection = HMM_Orthographic_LH_NO( diff --git a/source/engine/resources.c b/source/engine/resources.c index 494043a..b3f1a1a 100644 --- a/source/engine/resources.c +++ b/source/engine/resources.c @@ -50,9 +50,7 @@ static void response_cb(const sfetch_response_t *r) if (r->finished) { LOADED_GAME = -1; if (r->failed) { - printf("NO GAME\n"); LOADED_GAME = -1; - } } } diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 60d608a..b4c8d2e 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -144,14 +144,9 @@ void sprite_initialize() { [1].format = SG_VERTEXFORMAT_FLOAT2, [2].format = SG_VERTEXFORMAT_UBYTE4N, [3].format = SG_VERTEXFORMAT_UBYTE4N}}, -// [4].format = SG_VERTEXFORMAT_FLOAT}}, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .label = "sprite pipeline", .colors[0].blend = blend_trans, - .depth = { - .write_enabled = true, - .compare = SG_COMPAREFUNC_LESS_EQUAL, - } }); bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){