From 98ba229c9c92b004e7cab4bf547235b8748c44e7 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 27 Sep 2023 17:36:32 +0000 Subject: [PATCH] correct polygon2d saving and editing --- scripts/base.js | 9 --------- scripts/components.js | 23 ++++++++++++++--------- scripts/debug.js | 2 +- scripts/editor.js | 11 ++++++----- scripts/engine.js | 25 ++++++++++++++++++++++--- scripts/entity.js | 10 +++++++--- scripts/std.js | 6 +++++- source/engine/2dphysics.c | 3 +++ 8 files changed, 58 insertions(+), 31 deletions(-) diff --git a/scripts/base.js b/scripts/base.js index 34c71db..a376e82 100644 --- a/scripts/base.js +++ b/scripts/base.js @@ -392,14 +392,6 @@ Object.defineProperty(Array.prototype, 'rotate', { } }); - -Object.defineProperty(Array.prototype, '$add', { -value: function(b) { - for (var i = 0; i < this.length; i++) { - this[i] += b[i]; - } -}}); - function setelem(n) { return { get: function() { return this[n]; }, @@ -421,7 +413,6 @@ arrsetelem('g', 1); arrsetelem('b', 2); arrsetelem('a', 3); - Object.defineProperty(Array.prototype, 'add', { value: function(b) { var c = []; diff --git a/scripts/components.js b/scripts/components.js index 3968c87..d5fd6b3 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -351,11 +351,6 @@ collider2d.inputs['M-t'] = function() { this.enabled = !this.enabled; } collider2d.inputs['M-t'].doc = "Toggle if this collider is enabled."; component.polygon2d = Object.copy(collider2d, { - ur: { - flipx: false, - flipy: false - }, - sync() { cmd_poly2d(0, this.id, this.spoints); }, @@ -367,7 +362,7 @@ component.polygon2d = Object.copy(collider2d, { make(go) { var poly = Object.create(this); poly.gameobject = go; - poly.points = []; + poly.points = [0,0]; poly.flipx = false; poly.flipy = false; Object.assign(poly, make_poly2d(go.body, poly.points)); @@ -398,8 +393,6 @@ component.polygon2d = Object.copy(collider2d, { }, gizmo() { - if (!this.hasOwn('points')) this.points = this.__proto__.points.copy(); - this.spoints.forEach(function(x) { Debug.point(world2screen(this.gameobject.this2world(x)), 3, Color.green); }, this); @@ -410,7 +403,19 @@ component.polygon2d = Object.copy(collider2d, { }, pick(pos) { - return Gizmos.pick_gameobject_points(pos, this.gameobject, this.points); + var p = Gizmos.pick_gameobject_points(pos, this.gameobject, this.points); + 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; }, query() { diff --git a/scripts/debug.js b/scripts/debug.js index 39701a5..4a5fe1e 100644 --- a/scripts/debug.js +++ b/scripts/debug.js @@ -1,7 +1,7 @@ var Gizmos = { pick_gameobject_points(worldpos, gameobject, points) { var idx = grab_from_points(worldpos, points.map(gameobject.this2world,gameobject), 25); - if (idx === -1) return null; + if (idx === -1) return undefined; return points[idx]; }, }; diff --git a/scripts/editor.js b/scripts/editor.js index 156c7ac..66f4954 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -992,10 +992,7 @@ editor.inputs.mm = function() { } if (editor.sel_comp && 'pick' in editor.sel_comp) { - editor.grabselect = editor.sel_comp.pick(Mouse.worldpos); - if (!editor.grabselect) return; - -// editor.moveoffset = editor.sel_comp.gameobject.editor2world(editor.grabselect).sub(Mouse.worldpos); + editor.grabselect = [editor.sel_comp.pick(Mouse.worldpos)]; return; } @@ -1045,7 +1042,11 @@ editor.inputs.mouse.move = function(pos, dpos) editor.camera.pos = editor.camera.pos.sub(dpos.scale(editor.camera.zoom)); } - editor.grabselect?.forEach(x => x.pos = x.pos.add(dpos.scale(editor.camera.zoom))); + editor.grabselect?.forEach(function(x) { + x.pos = x.pos.add(dpos.scale(editor.camera.zoom)); + if ('sync' in x) + x.sync(); + }); var relpos = Mouse.worldpos.sub(editor.cursor); var dist = Vector.length(relpos); diff --git a/scripts/engine.js b/scripts/engine.js index c8c15fb..ca19b5b 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -542,16 +542,35 @@ var ur_json = function() { function objdiff(from, to) { if (!to) return from; // Everything on from is unique - var ret = {}; + + var ret = {}; + for (var key in from) { if (!from[key] || !to[key]) continue; if (typeof from[key] === 'function') continue; if (typeof to === 'object' && !(key in to)) continue; - if (typeof from[key] === 'object') { + if (Array.isArray(from[key])) { + if (!Array.isArray(to[key])) + ret[key] = from[key].slice(); + + if (from[key].length !== to[key].length) + ret[key] = from[key].slice(); + var diff = objdiff(from[key], to[key]); - if (diff && !diff.empty) ret[key] = diff; + if (diff && !diff.empty) + ret[key] = from[key]; + + continue; + } + + + if (typeof from[key] === 'object') { + if (key === 'points') Log.warn("POINTS"); + var diff = objdiff(from[key], to[key]); + if (diff && !diff.empty) + ret[key] = diff; continue; } diff --git a/scripts/entity.js b/scripts/entity.js index cb101a3..46fd783 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -205,7 +205,12 @@ var gameobject = { set friction(x) { cmd(108,this.body,x); }, set mass(x) { set_body(7,this.body,x); }, - get mass() { return q_body(5, this.body); }, + get mass() { + if (!(this.phys === Physics.dynamic)) + return this.ur.mass; + + return q_body(5, this.body); + }, set phys(x) { set_body(1, this.body, x); }, get phys() { return q_body(0,this.body); }, @@ -363,13 +368,12 @@ var gameobject = { Log.warn(`spawning a ${p.comp}`); obj[prop] = component[p.comp].make(obj); obj.components[prop] = obj[prop]; -// obj[prop].ur = Object.create(obj[prop].ur); -// Object.totalmerge(obj[prop].ur, p); } }; Object.totalmerge(obj,ur); obj.$.forEach(function(x) { x.pos = obj.pos.add(x.pos); }); + obj.components.forEach(function(x) { if ('sync' in x) x.sync(); }); obj.check_registers(obj); if (typeof obj.start === 'function') obj.start(); diff --git a/scripts/std.js b/scripts/std.js index c26251e..37895eb 100644 --- a/scripts/std.js +++ b/scripts/std.js @@ -51,11 +51,15 @@ var Log = { }, write(msg) { + if (typeof msg === 'object') + msg = JSON.stringify(msg,null,2); + cmd(91,msg); }, say(msg) { - cmd(91, `${msg}\n`); + Log.write(msg); + Log.write('\n'); }, stack(skip = 0) { diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index b028b59..94cf757 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -296,6 +296,7 @@ struct phys2d_box *Make2DBox(int go) { new->offset[0] = 0.f; new->offset[1] = 0.f; new->shape.go = go; + new->shape.apply = phys2d_applybox; phys2d_applybox(new); new->shape.debugdraw = phys2d_dbgdrawbox; new->shape.moi = phys2d_box_moi; @@ -362,6 +363,7 @@ struct phys2d_poly *Make2DPoly(int go) { new->shape.shape = cpSpaceAddShape(space, cpPolyShapeNewRaw(id2go(go)->body, 0, new->points, new->radius)); new->shape.debugdraw = phys2d_dbgdrawpoly; new->shape.moi = phys2d_poly_moi; + new->shape.apply = phys2d_applypoly; init_phys2dshape(&new->shape, go, new); return new; } @@ -433,6 +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->draws = 0; new->closed = 0; phys2d_applyedge(new);