From f6d491b1423df888b01ac5996ee5eccaf7777b4c Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 20 Sep 2023 18:33:11 +0000 Subject: [PATCH] tojson for ur and entity separation --- scripts/base.js | 6 +++ scripts/components.js | 65 +++++++++++------------ scripts/editor.js | 4 +- scripts/engine.js | 33 ++++++++++-- scripts/entity.js | 105 ++++++++++++-------------------------- source/engine/2dphysics.h | 2 +- source/engine/ffi.c | 17 +----- 7 files changed, 104 insertions(+), 128 deletions(-) diff --git a/scripts/base.js b/scripts/base.js index 1c94f23..dcb57a4 100644 --- a/scripts/base.js +++ b/scripts/base.js @@ -24,6 +24,12 @@ function clone(proto, binds) { return c; }; +Object.totalassign = function(to, from) +{ + for (var key in from) + to[key] = from[key]; +} + /* Prototypes out an object and assigns values */ function copy(proto, binds) { var c = Object.create(proto); diff --git a/scripts/components.js b/scripts/components.js index 116005e..80c7bef 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -23,6 +23,8 @@ var component = { }, }; +component.toJSON = ur_json; + var sprite = clone(component, { name: "sprite", path: "", @@ -58,8 +60,6 @@ sync() { }, kill() { cmd(9,this.id); }, - - dimensions() { return cmd(64,this.path); }, width() { return cmd(64,this.path).x; }, height() { return cmd(64,this.path).y; }, @@ -68,12 +68,20 @@ kill() { cmd(9,this.id); }, var sprite = Object.create(this); sprite.id = make_sprite(go); sprite.layer = 1; + Log.say(`made sprite with pos ${sprite.pos}`); return sprite; }, POS_MID: [-0.5, -0.5], }); +sprite.ur = { + pos:[0,0], + color:[1,1,1], + layer:0, + enabled:true +}; + sprite.inputs = {}; sprite.inputs.kp9 = function() { this.pos = [0,0]; }; sprite.inputs.kp8 = function() { this.pos = [-0.5, 0]; }; @@ -238,14 +246,11 @@ var collider2d = clone(component, { register_hit(fn, obj) { register_collide(1, fn, obj, this.gameobject.body, this.shape); }, - - make_fns: { - set sensor(x) { cmd(18,this.shape,x); }, +/* set sensor(x) { cmd(18,this.shape,x); }, get sensor() { return cmd(21,this.shape); }, set enabled(x) { cmd(22,this.shape,x); }, get enabled() { return cmd(23,this.shape); } - }, - +*/ }); collider2d.inputs = {}; @@ -623,21 +628,23 @@ bucket.inputs.rb.rep = true; var circle2d = clone(collider2d, { name: "circle 2d", - radius: 10, - offset: [0,0], + set radius(x) { cmd_circle2d(0,this.id,x); }, + get radius() { return cmd_circle2d(2,this.id); }, - get scale() { return this.radius; }, - set scale(x) { this.radius = x; }, - - get pos() { return this.offset; }, - set pos(x) { this.offset = x; }, + set offset(x) { cmd_circle2d(1,this.id,x); }, + get offset() { return cmd_circle2d(3,this.id); }, + + boundingbox() { + var diameter = this.radius*2*this.gameobject.scale; + return cwh2bb(this.offset.scale(this.gameobject.scale), [this.radius,this.radius]); + }, make(go) { - var circle = Object.create(this.made); - var circ = make_circle2d(go, circle.radius, circle.offset); - Object.assign(circle, circ); - Object.defineProperty(circle, 'id', {enumerable:false}); - Object.defineProperty(circle, 'shape', {enumerable:false}); + var circle = Object.create(this); + Object.assign(circle, make_circle2d(go, circle.radius, circle.offset)); + circle.radius = 10; + circle.offset = [0,0]; + return circle; }, @@ -649,22 +656,10 @@ var circle2d = clone(collider2d, { }, }); -circle2d.made = Object.create(circle2d); -complete_assign(circle2d.made, collider2d.make_fns); -complete_assign(circle2d.made, { - set radius(x) { cmd_circle2d(0,this.id,x); }, - get radius() { return cmd_circle2d(2,this.id); }, - - set offset(x) { cmd_circle2d(1,this.id,x); }, - get offset() { return cmd_circle2d(3,this.id); }, - - boundingbox() { - var diameter = this.radius*2*this.gameobject.scale; - return cwh2bb(this.offset.scale(this.gameobject.scale), [this.radius,this.radius]); - }, -}); - - +circle2d.ur = { + radius:10, + offset:[0,0], +}; /* ASSETS */ diff --git a/scripts/editor.js b/scripts/editor.js index c3297ce..d590b4f 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -750,10 +750,8 @@ editor.inputs['C-d'].doc = "Duplicate all selected objects."; editor.inputs.f3 = function() { editor.selectlist.forEach(function(x) { - Log.say("Selected JSON ..."); Log.say(JSON.stringify(x,null,2)); - Log.say("UR JSON"); - Log.say(JSON.stringify(x.__proto__,null,2)) +// x.components.forEach(function(x) { Log.say(JSON.stringify(x,null,2)); Log.say(JSON.stringify(x.ur,null,2));}); }); }; diff --git a/scripts/engine.js b/scripts/engine.js index 99bcdb5..25af732 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -172,7 +172,6 @@ var Render = { }, }; - load("scripts/physics.js"); load("scripts/input.js"); load("scripts/sound.js"); @@ -374,6 +373,36 @@ function Color(from) { }; */ +var ur_json = function() +{ + function objdiff(from, to) { + if (!to) return from; // Everything on from is unique + var ret = {}; + 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') { + var diff = objdiff(from[key], to[key]); + if (diff && !diff.empty) ret[key] = diff; + continue; + } + + if (from[key] !== to[key]) + ret[key] = from[key]; + } + if (ret.empty) return undefined; + return ret; + } + + return objdiff(this, this.ur); +} + + load("scripts/components.js"); function replacer_empty_nil(key, val) { @@ -544,8 +573,6 @@ var Game = { Register.update.register(Game.exec, Game); -//load("scripts/level.js"); - load("scripts/entity.js"); var preprimum = {}; diff --git a/scripts/entity.js b/scripts/entity.js index 42f2ca5..fb3349b 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -11,7 +11,6 @@ function grab_from_points(pos, points, slop) { }; var gameobject = { - scale: 1.0, save: true, selectable: true, @@ -22,7 +21,6 @@ var gameobject = { return obj; }, - layer: 0, /* Collision layer; should probably have been called "mask" */ layer_nuke() { Nuke.label("Collision layer"); Nuke.newline(Collision.num); @@ -51,9 +49,6 @@ var gameobject = { } }, - mass: 1, - - phys: 2, phys_nuke() { Nuke.newline(1); Nuke.label("phys"); @@ -62,10 +57,6 @@ var gameobject = { this.phys = Nuke.radio("kinematic", this.phys, 1); this.phys = Nuke.radio("static", this.phys, 2); }, - friction: 0, - elasticity: 0, - flipx: false, - flipy: false, set_center(pos) { var change = pos.sub(this.pos); @@ -76,9 +67,7 @@ var gameobject = { } }, - pos: [0,0], - - set relpos(x) { + set_relpos(x) { if (!this.level) { this.pos = x; return; @@ -87,29 +76,20 @@ var gameobject = { this.pos = Vector.rotate(x, Math.deg2rad(this.level.angle)).add(this.level.pos); }, - get relpos() { + get_relpos() { if (!this.level) return this.pos; var offset = this.pos.sub(this.level.pos); return Vector.rotate(offset, -Math.deg2rad(this.level.angle)); }, - angle: 0, - get relangle() { + get_relangle() { if (!this.level) return this.angle; return this.angle - this.level.angle; }, - velocity: [0,0], - angularvelocity: 0, - gizmo: "", /* Path to an image to draw for this gameobject */ - /* Bounding box of the ur, if it were to be spawned */ - boundingbox() { - - }, - width() { var bb = this.boundingbox(); return bb.r - bb.l; @@ -167,24 +147,6 @@ var gameobject = { }, instances: [], - make(level) { - level ??= Primum; - var obj = Object.create(this); - obj.defn('body', make_gameobject(this.scale, - this.phys, - this.mass, - this.friction, - this.elasticity) ); - - obj.defn('components', {}); - - Game.register_obj(obj); - - cmd(113, obj.body, obj); - - /* Now that it's concrete in the engine, these functions update to return engine data */ - complete_assign(obj, { - make() { Log.error("Cannot make from an entity.") }, set scale(x) { cmd(36, this.body, x); }, get scale() { return cmd(103, this.body); }, get flipx() { return cmd(104,this.body); }, @@ -288,8 +250,8 @@ var gameobject = { cmd(2, this.body); delete Game.objects[this.body]; - if (this.level) - this.level.unregister(this); +// if (this.level) +// this.level.unregister(this); Player.uncontrol(this); this.instances.remove(this); @@ -309,34 +271,19 @@ var gameobject = { }, up() { return [0,1].rotate(Math.deg2rad(this.angle));}, - down() { return [0,-1].rotate(Math.deg2rad(this.angle));}, - right() { return [1,0].rotate(Math.deg2rad(this.angle));}, - left() { return [-1,0].rotate(Math.deg2rad(this.angle));}, +// down() { return [0,-1].rotate(Math.deg2rad(this.angle));}, +// right() { return [1,0].rotate(Math.deg2rad(this.angle));}, +// left() { return [-1,0].rotate(Math.deg2rad(this.angle));}, - toJSON() { - function objdiff(from, to) { - if (!to) return from; /* Everything on from is unique */ - var ret = {}; - for (var key of Object.keys(from)) { - if (typeof from[key] === 'object') { - var diff = objdiff(from[key], to[key]); - if (diff && !diff.empty) ret[key] = diff; - continue; - } - if (from[key] !== to[key]) - ret[key] = from[key]; - } - Log.say("Returning a obj with values ..."); - for (var key in ret) - Log.say(key); + make(level) { + level ??= Primum; + var obj = Object.create(this); + obj.defn('body', make_gameobject()); + obj.defn('components', {}); + + Game.register_obj(obj); - if (ret.empty) return undefined; - return ret; - } - - return objdiff(this, this.__proto__); - }, - }); + cmd(113, obj.body, obj); // set the internal obj reference to this obj for (var prop in obj) { if (typeof obj[prop] === 'object' && 'make' in obj[prop]) { @@ -346,8 +293,6 @@ var gameobject = { } }; - dainty_assign(obj, this); - obj.check_registers(obj); gameobject.make_parentable(obj); @@ -358,6 +303,8 @@ var gameobject = { obj.$[e] = obj.spawn(prototypes.get_ur(obj.$[e].ur)); } + Object.totalassign(obj, obj.ur); + if (typeof obj.start === 'function') obj.start(); level.add_child(obj); @@ -380,6 +327,22 @@ var gameobject = { }, } +gameobject.toJSON = ur_json; + +gameobject.ur = { + pos: [0,0], + scale:1, + flipx:false, + flipy:false, + angle:0, + elasticity:0.5, + friction:1, + mass:1, + velocity:[0,0], + angularvelocity:0, + layer: 0, +}; + gameobject.make_parentable = function(obj) { var objects = []; Object.defHidden(obj, 'level'); diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index 7eb33e5..a814546 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -129,7 +129,7 @@ void phys2d_set_gravity(cpVect v); void shape_enabled(struct phys2d_shape *shape, int enabled); int shape_is_enabled(struct phys2d_shape *shape); void shape_set_sensor(struct phys2d_shape *shape, int sensor); -int shape_get_sensor(struct phys2d_shape *shape); +int shape_get_sensor(struct phys2d_shape *shape); struct rgba shape_color_s(cpShape *shape); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 126b199..acc2458 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -1257,20 +1257,7 @@ JSValue duk_sys_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *ar } JSValue duk_make_gameobject(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { - int g = MakeGameobject(); - struct gameobject *go = get_gameobject_from_id(g); - - go->scale = js2number(argv[0]); - go->bodytype = js2int(argv[1]); - go->mass = js2number(argv[2]); - go->f = js2number(argv[3]); - go->e = js2number(argv[4]); - go->flipx = 1.f; - go->flipy = 1.f; - - gameobject_apply(go); - - return JS_NewInt64(js, g); + return JS_NewInt64(js, MakeGameobject()); } JSValue duk_yughlog(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { @@ -1653,7 +1640,7 @@ void ffi_load() { DUK_FUNC(nuke, 6) #endif - DUK_FUNC(make_gameobject, 7) + DUK_FUNC(make_gameobject, 0) DUK_FUNC(set_body, 3) DUK_FUNC(q_body, 2)