correct polygon2d saving and editing

This commit is contained in:
John Alanbrook 2023-09-27 17:36:32 +00:00
parent 7984c90140
commit 98ba229c9c
8 changed files with 58 additions and 31 deletions

View file

@ -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 = [];

View file

@ -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() {

View file

@ -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];
},
};

View file

@ -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);

View file

@ -542,6 +542,8 @@ var ur_json = function()
{
function objdiff(from, to) {
if (!to) return from; // Everything on from is unique
var ret = {};
for (var key in from) {
@ -549,9 +551,26 @@ var ur_json = function()
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;
}

View file

@ -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();

View file

@ -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) {

View file

@ -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);