From 47dc6aa1fe441468a63c1a5fa0caa09d5aac0353 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 21 Sep 2023 13:38:23 +0000 Subject: [PATCH] flip, scale, angle works for subobject movement --- scripts/components.js | 2 ++ scripts/editor.js | 1 + scripts/engine.js | 10 +++--- scripts/entity.js | 80 +++++++++++++++++++++++++++++++----------- source/engine/script.c | 1 - 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/scripts/components.js b/scripts/components.js index 905427c..9ac450f 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -82,6 +82,7 @@ component.sprite.ur = { color:[1,1,1], layer:0, enabled:true, + toString() { return "sprite"; }, }; sprite.inputs = {}; @@ -644,6 +645,7 @@ component.circle2d = Object.copy(collider2d, { ur: { radius:10, offset:[0,0], + toString() { return "circle2d"; }, }, }); diff --git a/scripts/editor.js b/scripts/editor.js index c857f01..090c70d 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -1195,6 +1195,7 @@ editor.inputs.delete = function() { this.unselect(); }; editor.inputs.delete.doc = "Delete selected objects."; +editor.inputs['S-d'] = editor.inputs.delete; editor.inputs['C-u'] = function() { this.selectlist.forEach(function(x) { diff --git a/scripts/engine.js b/scripts/engine.js index a82a3c2..7be2347 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -378,15 +378,15 @@ var ur_json = function() function objdiff(from, to) { if (!to) return from; // Everything on from is unique var ret = {}; + ret.ur = to.toString(); for (var key in from) { - if (from[key]?.ur) { - ret[key] = objdiff(from[key], from[key].ur); - continue; - } - if (!(key in to)) continue; 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]); if (diff && !diff.empty) ret[key] = diff; continue; diff --git a/scripts/entity.js b/scripts/entity.js index a714afa..df55e60 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -140,22 +140,62 @@ var gameobject = { }, instances: [], - set scale(x) { cmd(36, this.body, x); }, get scale() { return cmd(103, this.body); }, + set scale(x) { + var pct = x/this.scale; + cmd(36, this.body, x); + + this.objects.forEach(function(obj) { + 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); }, + 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); }, - - get angle() { return Math.rad2deg(q_body(2,this.body))%360; }, - set angle(x) { set_body(0,this.body, Math.deg2rad(x)); }, - + 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) { var diff = x.sub(this.pos); this.objects.forEach(function(x) { x.pos = x.pos.add(diff); }); set_body(2,this.body,x); }, 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); }, set elasticity(x) { cmd(106,this.body,x); }, @@ -270,7 +310,7 @@ var gameobject = { make(ur, level) { level ??= Primum; - var obj = Object.create(this); + var obj = Object.create(gameobject); obj.defn('body', make_gameobject()); obj.defn('components', {}); @@ -280,6 +320,7 @@ var gameobject = { cmd(113, obj.body, obj); // set the internal obj reference to this obj Object.totalassign(obj, ur); + obj.ur = ur; for (var prop in obj) { if (typeof obj[prop] === 'object' && 'comp' in obj[prop]) { @@ -292,10 +333,7 @@ var gameobject = { } }; - obj.check_registers(obj); - /* Spawn subobjects defined */ - if (obj.$) { for (var e in obj.$) { var newobj = obj.spawn(prototypes.get_ur(obj.$[e].ur)); @@ -304,9 +342,8 @@ var gameobject = { } } - - - + obj.check_registers(obj); + if (typeof obj.start === 'function') obj.start(); level.add_child(obj); @@ -415,19 +452,18 @@ prototypes.from_file = function(file) } var newur = Object.create(gameobject.ur); + newur.$ = {}; var script = IO.slurp(file); -// Object.defHidden(newur, '$'); -// newur.$ = {}; -/* var json = {}; + var 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.$); delete json.$; } -*/ - compile_env(`var self = this; ${script}`, newur, file); -// Object.dainty_assign(newur, json); + + compile_env(script, newur, file); + Object.dainty_assign(newur, json); file = file.replaceAll('/', '.'); var path = file.name().split('.'); @@ -446,6 +482,8 @@ prototypes.from_file = function(file) Object.assign(nested_access(ur,path), newur); nested_access(ur,path).__proto__ = newur.__proto__; + Log.warn(`Made ur from script ${file}: ${JSON.stringify(newur)}`); + return nested_access(ur,path); } prototypes.from_file.doc = "Create a new ur-type from a given script file."; diff --git a/source/engine/script.c b/source/engine/script.c index 7f699bb..a6de1de 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -38,7 +38,6 @@ static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) { void script_startup() { rt = JS_NewRuntime(); - JS_SetMaxStackSize(rt, 0); js = JS_NewContext(rt); ffi_load();