tojson for ur and entity separation

This commit is contained in:
John Alanbrook 2023-09-20 18:33:11 +00:00
parent 31331af57d
commit f6d491b142
7 changed files with 104 additions and 128 deletions

View file

@ -24,6 +24,12 @@ function clone(proto, binds) {
return c; return c;
}; };
Object.totalassign = function(to, from)
{
for (var key in from)
to[key] = from[key];
}
/* Prototypes out an object and assigns values */ /* Prototypes out an object and assigns values */
function copy(proto, binds) { function copy(proto, binds) {
var c = Object.create(proto); var c = Object.create(proto);

View file

@ -23,6 +23,8 @@ var component = {
}, },
}; };
component.toJSON = ur_json;
var sprite = clone(component, { var sprite = clone(component, {
name: "sprite", name: "sprite",
path: "", path: "",
@ -58,8 +60,6 @@ sync() {
}, },
kill() { cmd(9,this.id); }, kill() { cmd(9,this.id); },
dimensions() { return cmd(64,this.path); }, dimensions() { return cmd(64,this.path); },
width() { return cmd(64,this.path).x; }, width() { return cmd(64,this.path).x; },
height() { return cmd(64,this.path).y; }, height() { return cmd(64,this.path).y; },
@ -68,12 +68,20 @@ kill() { cmd(9,this.id); },
var sprite = Object.create(this); var sprite = Object.create(this);
sprite.id = make_sprite(go); sprite.id = make_sprite(go);
sprite.layer = 1; sprite.layer = 1;
Log.say(`made sprite with pos ${sprite.pos}`);
return sprite; return sprite;
}, },
POS_MID: [-0.5, -0.5], POS_MID: [-0.5, -0.5],
}); });
sprite.ur = {
pos:[0,0],
color:[1,1,1],
layer:0,
enabled:true
};
sprite.inputs = {}; sprite.inputs = {};
sprite.inputs.kp9 = function() { this.pos = [0,0]; }; sprite.inputs.kp9 = function() { this.pos = [0,0]; };
sprite.inputs.kp8 = function() { this.pos = [-0.5, 0]; }; sprite.inputs.kp8 = function() { this.pos = [-0.5, 0]; };
@ -238,14 +246,11 @@ var collider2d = clone(component, {
register_hit(fn, obj) { register_hit(fn, obj) {
register_collide(1, fn, obj, this.gameobject.body, this.shape); register_collide(1, fn, obj, this.gameobject.body, this.shape);
}, },
/* set sensor(x) { cmd(18,this.shape,x); },
make_fns: {
set sensor(x) { cmd(18,this.shape,x); },
get sensor() { return cmd(21,this.shape); }, get sensor() { return cmd(21,this.shape); },
set enabled(x) { cmd(22,this.shape,x); }, set enabled(x) { cmd(22,this.shape,x); },
get enabled() { return cmd(23,this.shape); } get enabled() { return cmd(23,this.shape); }
}, */
}); });
collider2d.inputs = {}; collider2d.inputs = {};
@ -623,21 +628,23 @@ bucket.inputs.rb.rep = true;
var circle2d = clone(collider2d, { var circle2d = clone(collider2d, {
name: "circle 2d", name: "circle 2d",
radius: 10, set radius(x) { cmd_circle2d(0,this.id,x); },
offset: [0,0], get radius() { return cmd_circle2d(2,this.id); },
get scale() { return this.radius; }, set offset(x) { cmd_circle2d(1,this.id,x); },
set scale(x) { this.radius = x; }, get offset() { return cmd_circle2d(3,this.id); },
get pos() { return this.offset; }, boundingbox() {
set pos(x) { this.offset = x; }, var diameter = this.radius*2*this.gameobject.scale;
return cwh2bb(this.offset.scale(this.gameobject.scale), [this.radius,this.radius]);
},
make(go) { make(go) {
var circle = Object.create(this.made); var circle = Object.create(this);
var circ = make_circle2d(go, circle.radius, circle.offset); Object.assign(circle, make_circle2d(go, circle.radius, circle.offset));
Object.assign(circle, circ); circle.radius = 10;
Object.defineProperty(circle, 'id', {enumerable:false}); circle.offset = [0,0];
Object.defineProperty(circle, 'shape', {enumerable:false});
return circle; return circle;
}, },
@ -649,22 +656,10 @@ var circle2d = clone(collider2d, {
}, },
}); });
circle2d.made = Object.create(circle2d); circle2d.ur = {
complete_assign(circle2d.made, collider2d.make_fns); radius:10,
complete_assign(circle2d.made, { offset:[0,0],
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]);
},
});
/* ASSETS */ /* ASSETS */

View file

@ -750,10 +750,8 @@ editor.inputs['C-d'].doc = "Duplicate all selected objects.";
editor.inputs.f3 = function() { editor.inputs.f3 = function() {
editor.selectlist.forEach(function(x) { editor.selectlist.forEach(function(x) {
Log.say("Selected JSON ...");
Log.say(JSON.stringify(x,null,2)); Log.say(JSON.stringify(x,null,2));
Log.say("UR JSON"); // x.components.forEach(function(x) { Log.say(JSON.stringify(x,null,2)); Log.say(JSON.stringify(x.ur,null,2));});
Log.say(JSON.stringify(x.__proto__,null,2))
}); });
}; };

View file

@ -172,7 +172,6 @@ var Render = {
}, },
}; };
load("scripts/physics.js"); load("scripts/physics.js");
load("scripts/input.js"); load("scripts/input.js");
load("scripts/sound.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"); load("scripts/components.js");
function replacer_empty_nil(key, val) { function replacer_empty_nil(key, val) {
@ -544,8 +573,6 @@ var Game = {
Register.update.register(Game.exec, Game); Register.update.register(Game.exec, Game);
//load("scripts/level.js");
load("scripts/entity.js"); load("scripts/entity.js");
var preprimum = {}; var preprimum = {};

View file

@ -11,7 +11,6 @@ function grab_from_points(pos, points, slop) {
}; };
var gameobject = { var gameobject = {
scale: 1.0,
save: true, save: true,
selectable: true, selectable: true,
@ -22,7 +21,6 @@ var gameobject = {
return obj; return obj;
}, },
layer: 0, /* Collision layer; should probably have been called "mask" */
layer_nuke() { layer_nuke() {
Nuke.label("Collision layer"); Nuke.label("Collision layer");
Nuke.newline(Collision.num); Nuke.newline(Collision.num);
@ -51,9 +49,6 @@ var gameobject = {
} }
}, },
mass: 1,
phys: 2,
phys_nuke() { phys_nuke() {
Nuke.newline(1); Nuke.newline(1);
Nuke.label("phys"); Nuke.label("phys");
@ -62,10 +57,6 @@ var gameobject = {
this.phys = Nuke.radio("kinematic", this.phys, 1); this.phys = Nuke.radio("kinematic", this.phys, 1);
this.phys = Nuke.radio("static", this.phys, 2); this.phys = Nuke.radio("static", this.phys, 2);
}, },
friction: 0,
elasticity: 0,
flipx: false,
flipy: false,
set_center(pos) { set_center(pos) {
var change = pos.sub(this.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) { if (!this.level) {
this.pos = x; this.pos = x;
return; return;
@ -87,29 +76,20 @@ var gameobject = {
this.pos = Vector.rotate(x, Math.deg2rad(this.level.angle)).add(this.level.pos); this.pos = Vector.rotate(x, Math.deg2rad(this.level.angle)).add(this.level.pos);
}, },
get relpos() { get_relpos() {
if (!this.level) return this.pos; if (!this.level) return this.pos;
var offset = this.pos.sub(this.level.pos); var offset = this.pos.sub(this.level.pos);
return Vector.rotate(offset, -Math.deg2rad(this.level.angle)); return Vector.rotate(offset, -Math.deg2rad(this.level.angle));
}, },
angle: 0, get_relangle() {
get relangle() {
if (!this.level) return this.angle; if (!this.level) return this.angle;
return this.angle - this.level.angle; return this.angle - this.level.angle;
}, },
velocity: [0,0],
angularvelocity: 0,
gizmo: "", /* Path to an image to draw for this gameobject */ gizmo: "", /* Path to an image to draw for this gameobject */
/* Bounding box of the ur, if it were to be spawned */
boundingbox() {
},
width() { width() {
var bb = this.boundingbox(); var bb = this.boundingbox();
return bb.r - bb.l; return bb.r - bb.l;
@ -167,24 +147,6 @@ var gameobject = {
}, },
instances: [], 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); }, 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); }, get flipx() { return cmd(104,this.body); },
@ -288,8 +250,8 @@ var gameobject = {
cmd(2, this.body); cmd(2, this.body);
delete Game.objects[this.body]; delete Game.objects[this.body];
if (this.level) // if (this.level)
this.level.unregister(this); // this.level.unregister(this);
Player.uncontrol(this); Player.uncontrol(this);
this.instances.remove(this); this.instances.remove(this);
@ -309,34 +271,19 @@ var gameobject = {
}, },
up() { return [0,1].rotate(Math.deg2rad(this.angle));}, up() { return [0,1].rotate(Math.deg2rad(this.angle));},
down() { 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));}, // right() { return [1,0].rotate(Math.deg2rad(this.angle));},
left() { return [-1,0].rotate(Math.deg2rad(this.angle));}, // left() { return [-1,0].rotate(Math.deg2rad(this.angle));},
toJSON() { make(level) {
function objdiff(from, to) { level ??= Primum;
if (!to) return from; /* Everything on from is unique */ var obj = Object.create(this);
var ret = {}; obj.defn('body', make_gameobject());
for (var key of Object.keys(from)) { obj.defn('components', {});
if (typeof from[key] === 'object') {
var diff = objdiff(from[key], to[key]); Game.register_obj(obj);
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);
if (ret.empty) return undefined; cmd(113, obj.body, obj); // set the internal obj reference to this obj
return ret;
}
return objdiff(this, this.__proto__);
},
});
for (var prop in obj) { for (var prop in obj) {
if (typeof obj[prop] === 'object' && 'make' in obj[prop]) { if (typeof obj[prop] === 'object' && 'make' in obj[prop]) {
@ -346,8 +293,6 @@ var gameobject = {
} }
}; };
dainty_assign(obj, this);
obj.check_registers(obj); obj.check_registers(obj);
gameobject.make_parentable(obj); gameobject.make_parentable(obj);
@ -358,6 +303,8 @@ var gameobject = {
obj.$[e] = obj.spawn(prototypes.get_ur(obj.$[e].ur)); obj.$[e] = obj.spawn(prototypes.get_ur(obj.$[e].ur));
} }
Object.totalassign(obj, obj.ur);
if (typeof obj.start === 'function') obj.start(); if (typeof obj.start === 'function') obj.start();
level.add_child(obj); 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) { gameobject.make_parentable = function(obj) {
var objects = []; var objects = [];
Object.defHidden(obj, 'level'); Object.defHidden(obj, 'level');

View file

@ -129,7 +129,7 @@ void phys2d_set_gravity(cpVect v);
void shape_enabled(struct phys2d_shape *shape, int enabled); void shape_enabled(struct phys2d_shape *shape, int enabled);
int shape_is_enabled(struct phys2d_shape *shape); int shape_is_enabled(struct phys2d_shape *shape);
void shape_set_sensor(struct phys2d_shape *shape, int sensor); 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); struct rgba shape_color_s(cpShape *shape);

View file

@ -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) { JSValue duk_make_gameobject(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
int g = MakeGameobject(); return JS_NewInt64(js, 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);
} }
JSValue duk_yughlog(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { JSValue duk_yughlog(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
@ -1653,7 +1640,7 @@ void ffi_load() {
DUK_FUNC(nuke, 6) DUK_FUNC(nuke, 6)
#endif #endif
DUK_FUNC(make_gameobject, 7) DUK_FUNC(make_gameobject, 0)
DUK_FUNC(set_body, 3) DUK_FUNC(set_body, 3)
DUK_FUNC(q_body, 2) DUK_FUNC(q_body, 2)