From 70e4105e1b8b000c7f4bd2c022ca362ebbe1984a Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 27 Sep 2023 22:40:04 +0000 Subject: [PATCH] bucket renamed to edge2d and saves now; proper input blocking with reversed() --- scripts/base.js | 9 ++++- scripts/components.js | 74 +++++++++++++++++++------------------- scripts/editor.js | 4 ++- scripts/engine.js | 2 -- scripts/entity.js | 20 +++++------ scripts/input.js | 7 ++-- scripts/std.js | 9 +++-- source/engine/2dphysics.c | 2 +- source/engine/font.c | 2 +- source/engine/gameobject.c | 2 +- 10 files changed, 67 insertions(+), 64 deletions(-) diff --git a/scripts/base.js b/scripts/base.js index a376e82..972f668 100644 --- a/scripts/base.js +++ b/scripts/base.js @@ -92,7 +92,7 @@ Object.mergekey = function(o1,o2,k) Object.defineProperty(o1, k, Object.getOwnPropertyDescriptor(o2,k)); else if (typeof o2[k] === 'object') { if (Array.isArray(o2[k])) - o1[k] = o2[k].slice(); + o1[k] = deep_copy(o2[k]); else { if (!o1[k]) o1[k] = {}; if (typeof o1[k] === 'object') @@ -386,6 +386,13 @@ Object.defineProperty(Array.prototype, 'copy', { } }); +Object.defineProperty(Array.prototype, 'reversed', { + value: function() { + var c = this.slice(); + return c.reverse(); + } +}); + Object.defineProperty(Array.prototype, 'rotate', { value: function(a) { return Vector.rotate(this, a); diff --git a/scripts/components.js b/scripts/components.js index d5fd6b3..5f01d3f 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -362,7 +362,7 @@ component.polygon2d = Object.copy(collider2d, { make(go) { var poly = Object.create(this); poly.gameobject = go; - poly.points = [0,0]; + poly.points = []; poly.flipx = false; poly.flipy = false; Object.assign(poly, make_poly2d(go.body, poly.points)); @@ -453,9 +453,7 @@ polygon2d.inputs['C-b'].doc = "Freeze mirroring in place."; Object.freeze(polygon2d); -component.bucket = Object.copy(collider2d, { - name: "bucket", - cpoints:[], +component.edge2d = Object.copy(collider2d, { degrees:2, dimensions:2, /* open: 0 @@ -532,52 +530,42 @@ component.bucket = Object.copy(collider2d, { assert knots%order != 0 */ - if (this.type === bucket.typeid.looped) + if (this.type === typeid.this.looped) return spline_cmd(0, this.degrees, this.dimensions, 0, spoints.wrapped(this.degrees), n); return spline_cmd(0, this.degrees, this.dimensions, this.type, spoints, n); - }, + }, + set thickness(x) { + cmd_edge2d(1,this.id,x); + }, + + get thickness() { return cmd(112,this.id); }, samples: 10, - points:[], - thickness:0, /* Number of pixels out the edge is */ + boundingbox() { + return points2bb(this.points.map(x => x.scale(this.gameobject.scale))); + }, + make(go) { var edge = Object.create(this); edge.gameobject = go; - Object.assign(edge, make_edge2d(go.body, this.points, this.thickness)); - Object.assign(edge, { - set thickness(x) { - cmd_edge2d(1,this.id,x); - }, - get thickness() { return cmd(112,this.id); }, - - boundingbox() { - return points2bb(this.points.map(x => x.scale(this.gameobject.scale))); - }, - - sync() { - var sensor = this.sensor; - this.points = this.sample(this.samples); - cmd_edge2d(0,this.id,this.points); - this.sensor = sensor; - }, - }); - - Object.assign(edge, this.make_fns); - - Object.defineProperty(edge, 'id', {enumerable:false}); - Object.defineProperty(edge, 'shape', {enumerable:false}); - - edge.defn('points', []); - + edge.cpoints = []; + edge.points = []; + Object.assign(edge, make_edge2d(go.body, edge.points, 0)); + edge.thickness = 0; return edge; }, + sync() { + var sensor = this.sensor; + this.points = this.sample(this.samples); + cmd_edge2d(0,this.id,this.points); + this.sensor = sensor; + }, + /* EDITOR */ gizmo() { - if (!this.hasOwn('cpoints')) this.cpoints = this.__proto__.cpoints.copy(); - this.spoints.forEach(function(x) { Debug.point(world2screen(this.gameobject.this2world(x)), 3, Color.green); }, this); @@ -591,10 +579,20 @@ component.bucket = Object.copy(collider2d, { this.cpoints = this.cpoints.map(function(x) { return x.sub(change); }); }, - pick(pos) { return Gizmos.pick_gameobject_points(pos, this.gameobject, this.cpoints); }, + pick(pos) { + var p = Gizmos.pick_gameobject_points(pos, this.gameobject, this.cpoints); + if (p) return { + set pos(n) { p.x = n.x; p.y = n.y; }, + get pos() { return p; }, + sync: this.sync.bind(this), + }; + + + return undefined; + }, }); -var bucket = component.bucket; +var bucket = component.edge2d; bucket.inputs = {}; bucket.inputs.h = function() { this.hollow = !this.hollow; }; bucket.inputs.h.doc = "Toggle hollow."; diff --git a/scripts/editor.js b/scripts/editor.js index 66f4954..834a248 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -992,7 +992,9 @@ editor.inputs.mm = function() { } if (editor.sel_comp && 'pick' in editor.sel_comp) { - editor.grabselect = [editor.sel_comp.pick(Mouse.worldpos)]; + var o = editor.sel_comp.pick(Mouse.worldpos); + if (o) + editor.grabselect = [o]; return; } diff --git a/scripts/engine.js b/scripts/engine.js index ca19b5b..e3247b6 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -543,7 +543,6 @@ var ur_json = function() function objdiff(from, to) { if (!to) return from; // Everything on from is unique - var ret = {}; for (var key in from) { @@ -565,7 +564,6 @@ var ur_json = function() continue; } - if (typeof from[key] === 'object') { if (key === 'points') Log.warn("POINTS"); var diff = objdiff(from[key], to[key]); diff --git a/scripts/entity.js b/scripts/entity.js index 46fd783..6ad942f 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -448,7 +448,8 @@ prototypes.from_file = function(file) var urpath = file; var path = urpath.split('.'); if (path.length > 1 && (path.at(-1) === path.at(-2))) { - return prototypes.get_ur(path.at(-1)); + urpath = path.slice(0,-1).join('.'); + return prototypes.get_ur(urpath); } var upperur = gameobject.ur; @@ -477,18 +478,15 @@ prototypes.from_file = function(file) jsfile = urpath + "/" + path.at(-1) + ".js"; if (IO.exists(jsfile)) script = IO.slurp(jsfile); } - - if (IO.exists(jsonfile)) { - try { - json = JSON.parse(IO.slurp(jsonfile)); - } + + try { + try {json = JSON.parse(IO.slurp(jsonfile)); } catch(e) { - Log.warn(`JSON in file ${jsonfile} is malformed.`); + jsonfile = file + "/" + path.at(-1) + ".json"; + if (IO.exists(jsonfile)) json = JSON.parse(IO.slurp(jsonfile)); } - } - else { - jsonfile = urpath + "/" + path.at(-1) + ".json"; - if (IO.exists(jsonfile)) json = JSON.parse(IO.slurp(jsonfile)); + } catch(e) { + Log.warn(`JSON in file ${jsonfile} is malformed.${e}`); } if (!json && !script) { diff --git a/scripts/input.js b/scripts/input.js index 13a8477..7bbfa87 100644 --- a/scripts/input.js +++ b/scripts/input.js @@ -110,7 +110,7 @@ var Player = { }, mouse_input(type, ...args) { - for (var pawn of this.pawns.reverse()) { + for (var pawn of this.pawns.reversed()) { if (typeof pawn.inputs?.mouse?.[type] === 'function') { pawn.inputs.mouse[type].call(pawn,...args); pawn.inputs.post?.call(pawn); @@ -120,7 +120,7 @@ var Player = { }, char_input(c) { - for (var pawn of this.pawns.reverse()) { + for (var pawn of this.pawns.reversed()) { if (typeof pawn.inputs?.char === 'function') { pawn.inputs.char.call(pawn, c); pawn.inputs.post?.call(pawn); @@ -130,7 +130,7 @@ var Player = { }, raw_input(cmd, state, ...args) { - for (var pawn of this.pawns.reverse()) { + for (var pawn of this.pawns.reversed()) { if (typeof pawn.inputs?.any === 'function') { pawn.inputs.any(cmd); return; @@ -156,6 +156,7 @@ var Player = { if (typeof fn === 'function') { fn.call(pawn, ... args); pawn.inputs.post?.call(pawn); + return; } } }, diff --git a/scripts/std.js b/scripts/std.js index 37895eb..9d019db 100644 --- a/scripts/std.js +++ b/scripts/std.js @@ -75,11 +75,10 @@ var Log = { var IO = { exists(file) { return cmd(65, file);}, slurp(file) { - if (!this.exists(file)) { - Log.warn(`File ${file} does not exist; can't slurp.`); - return ""; - } - return cmd(38,file); + if (IO.exists(file)) + return cmd(38,file); + else + throw new Error(`File ${file} does not exist; can't slurp`); }, slurpwrite(str, file) { return cmd(39, str, file); }, extensions(ext) { diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 94cf757..869a2d6 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -435,7 +435,7 @@ struct phys2d_edge *Make2DEdge(int go) { new->shape.debugdraw = phys2d_dbgdrawedge; new->shape.moi = phys2d_edge_moi; new->shape.shape = NULL; - new->shape.apply = phys2d_applyedge; + new->shape.apply = NULL; new->draws = 0; new->closed = 0; phys2d_applyedge(new); diff --git a/source/engine/font.c b/source/engine/font.c index 672a78d..6d4297c 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -138,7 +138,7 @@ struct sFont *MakeFont(const char *fontfile, int height) { stbtt_GetFontVMetrics(&fontinfo, &newfont->ascent, &newfont->descent, &newfont->linegap); newfont->emscale = stbtt_ScaleForMappingEmToPixels(&fontinfo, 16); - newfont->linegap = (newfont->ascent - newfont->descent)* 2 * newfont->emscale; + newfont->linegap = (newfont->ascent - newfont->descent) * newfont->emscale; newfont->texID = sg_make_image(&(sg_image_desc){ .type = SG_IMAGETYPE_2D, diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index c8cfb93..30d8494 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -97,7 +97,7 @@ void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go) { cpShapeSetFilter(shape, filter); struct phys2d_shape *ape = cpShapeGetUserData(shape); - if (ape) + if (ape && ape->apply) ape->apply(ape->data); }