flip, scale, angle works for subobject movement

This commit is contained in:
John Alanbrook 2023-09-21 13:38:23 +00:00
parent 81875c3436
commit 47dc6aa1fe
5 changed files with 67 additions and 27 deletions

View file

@ -82,6 +82,7 @@ component.sprite.ur = {
color:[1,1,1], color:[1,1,1],
layer:0, layer:0,
enabled:true, enabled:true,
toString() { return "sprite"; },
}; };
sprite.inputs = {}; sprite.inputs = {};
@ -644,6 +645,7 @@ component.circle2d = Object.copy(collider2d, {
ur: { ur: {
radius:10, radius:10,
offset:[0,0], offset:[0,0],
toString() { return "circle2d"; },
}, },
}); });

View file

@ -1195,6 +1195,7 @@ editor.inputs.delete = function() {
this.unselect(); this.unselect();
}; };
editor.inputs.delete.doc = "Delete selected objects."; editor.inputs.delete.doc = "Delete selected objects.";
editor.inputs['S-d'] = editor.inputs.delete;
editor.inputs['C-u'] = function() { editor.inputs['C-u'] = function() {
this.selectlist.forEach(function(x) { this.selectlist.forEach(function(x) {

View file

@ -378,15 +378,15 @@ var ur_json = function()
function objdiff(from, to) { function objdiff(from, to) {
if (!to) return from; // Everything on from is unique if (!to) return from; // Everything on from is unique
var ret = {}; var ret = {};
ret.ur = to.toString();
for (var key in from) { for (var key in from) {
if (from[key]?.ur) {
ret[key] = objdiff(from[key], from[key].ur);
continue;
}
if (!(key in to)) continue; if (!(key in to)) continue;
if (typeof from[key] === 'object') { if (typeof from[key] === 'object') {
if ('ur' in from[key]) {
ret[key] = objdiff(from[key],from[key].ur);
continue;
}
var diff = objdiff(from[key], to[key]); var diff = objdiff(from[key], to[key]);
if (diff && !diff.empty) ret[key] = diff; if (diff && !diff.empty) ret[key] = diff;
continue; continue;

View file

@ -140,15 +140,39 @@ var gameobject = {
}, },
instances: [], instances: [],
set scale(x) { cmd(36, this.body, x); },
get scale() { return cmd(103, this.body); }, get scale() { return cmd(103, this.body); },
get flipx() { return cmd(104,this.body); }, set scale(x) {
set flipx(x) { cmd(55, this.body, x); }, var pct = x/this.scale;
get flipy() { return cmd(105,this.body); }, cmd(36, this.body, x);
set flipy(x) { cmd(56, this.body, x); },
get angle() { return Math.rad2deg(q_body(2,this.body))%360; }, this.objects.forEach(function(obj) {
set angle(x) { set_body(0,this.body, Math.deg2rad(x)); }, obj.scale *= pct;
obj.set_relpos(obj.get_relpos().scale(pct));
}, this);
},
get flipx() { return cmd(104,this.body); },
set flipx(x) {
cmd(55, this.body, x);
return;
this.objects.forEach(function(obj) {
obj.flipx = !obj.flipx;
var rp = obj.get_relpos();
obj.pos = [-rp.x, rp.y].add(this.pos);
obj.angle = -obj.angle;
},this);
},
get flipy() { return cmd(105,this.body); },
set flipy(x) {
cmd(56, this.body, x);
return;
this.objects.forEach(function(obj) {
var rp = obj.get_relpos();
obj.pos = [rp.x, -rp.y].add(this.pos);
obj.angle = -obj.angle;
},this);
},
set pos(x) { set pos(x) {
var diff = x.sub(this.pos); var diff = x.sub(this.pos);
@ -156,6 +180,22 @@ var gameobject = {
set_body(2,this.body,x); }, set_body(2,this.body,x); },
get pos() { return q_body(1,this.body); }, get pos() { return q_body(1,this.body); },
get angle() { return Math.rad2deg(q_body(2,this.body))%360; },
set angle(x) {
var diff = x - this.angle;
this.objects.forEach(function(x) {
x.angle = x.angle + diff;
var pos = x.pos.sub(this.pos);
var r = Vector.length(pos);
var p = Math.rad2deg(Math.atan2(pos.y, pos.x));
p += diff;
p = Math.deg2rad(p);
x.pos = this.pos.add([r*Math.cos(p), r*Math.sin(p)]);
}, this);
set_body(0,this.body, Math.deg2rad(x));
},
get elasticity() { return cmd(107,this.body); }, get elasticity() { return cmd(107,this.body); },
set elasticity(x) { cmd(106,this.body,x); }, set elasticity(x) { cmd(106,this.body,x); },
@ -270,7 +310,7 @@ var gameobject = {
make(ur, level) { make(ur, level) {
level ??= Primum; level ??= Primum;
var obj = Object.create(this); var obj = Object.create(gameobject);
obj.defn('body', make_gameobject()); obj.defn('body', make_gameobject());
obj.defn('components', {}); obj.defn('components', {});
@ -280,6 +320,7 @@ var gameobject = {
cmd(113, obj.body, obj); // set the internal obj reference to this obj cmd(113, obj.body, obj); // set the internal obj reference to this obj
Object.totalassign(obj, ur); Object.totalassign(obj, ur);
obj.ur = ur;
for (var prop in obj) { for (var prop in obj) {
if (typeof obj[prop] === 'object' && 'comp' in obj[prop]) { if (typeof obj[prop] === 'object' && 'comp' in obj[prop]) {
@ -292,10 +333,7 @@ var gameobject = {
} }
}; };
obj.check_registers(obj);
/* Spawn subobjects defined */ /* Spawn subobjects defined */
if (obj.$) { if (obj.$) {
for (var e in obj.$) { for (var e in obj.$) {
var newobj = obj.spawn(prototypes.get_ur(obj.$[e].ur)); var newobj = obj.spawn(prototypes.get_ur(obj.$[e].ur));
@ -304,8 +342,7 @@ var gameobject = {
} }
} }
obj.check_registers(obj);
if (typeof obj.start === 'function') obj.start(); if (typeof obj.start === 'function') obj.start();
@ -415,19 +452,18 @@ prototypes.from_file = function(file)
} }
var newur = Object.create(gameobject.ur); var newur = Object.create(gameobject.ur);
newur.$ = {};
var script = IO.slurp(file); var script = IO.slurp(file);
// Object.defHidden(newur, '$'); var json = {};
// newur.$ = {};
/* var json = {};
if (IO.exists(file.name() + ".json")) { if (IO.exists(file.name() + ".json")) {
json = JSON.parse(IO.slurp(file.name() + ".json")); var json = JSON.parse(IO.slurp(file.name() + ".json"));
Object.assign(newur.$, json.$); Object.assign(newur.$, json.$);
delete json.$; delete json.$;
} }
*/
compile_env(`var self = this; ${script}`, newur, file); compile_env(script, newur, file);
// Object.dainty_assign(newur, json); Object.dainty_assign(newur, json);
file = file.replaceAll('/', '.'); file = file.replaceAll('/', '.');
var path = file.name().split('.'); var path = file.name().split('.');
@ -446,6 +482,8 @@ prototypes.from_file = function(file)
Object.assign(nested_access(ur,path), newur); Object.assign(nested_access(ur,path), newur);
nested_access(ur,path).__proto__ = newur.__proto__; nested_access(ur,path).__proto__ = newur.__proto__;
Log.warn(`Made ur from script ${file}: ${JSON.stringify(newur)}`);
return nested_access(ur,path); return nested_access(ur,path);
} }
prototypes.from_file.doc = "Create a new ur-type from a given script file."; prototypes.from_file.doc = "Create a new ur-type from a given script file.";

View file

@ -38,7 +38,6 @@ static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
void script_startup() { void script_startup() {
rt = JS_NewRuntime(); rt = JS_NewRuntime();
JS_SetMaxStackSize(rt, 0);
js = JS_NewContext(rt); js = JS_NewContext(rt);
ffi_load(); ffi_load();