bucket renamed to edge2d and saves now; proper input blocking with reversed()

This commit is contained in:
John Alanbrook 2023-09-27 22:40:04 +00:00
parent 98ba229c9c
commit 70e4105e1b
10 changed files with 67 additions and 64 deletions

View file

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

View file

@ -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.";

View file

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

View file

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

View file

@ -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;
@ -478,17 +479,14 @@ prototypes.from_file = function(file)
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) {

View file

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

View file

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

View file

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

View file

@ -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,

View file

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