add object merging

This commit is contained in:
John Alanbrook 2023-09-22 00:51:38 +00:00
parent 14d743e704
commit a8eb444991
6 changed files with 101 additions and 43 deletions

View file

@ -72,8 +72,63 @@ Object.dainty_assign = function(target, source)
} }
} }
/* This name is more consistent with Ruby, etc */ Object.isAccessor = function(obj, prop)
Object.merge = Object.dainty_assign; {
var prop = Object.getOwnPropertyDescriptor(obj,prop);
if (prop) return false;
return true;
}
/* Same as merge from Ruby */
Object.merge = function(target, ...objs)
{
var objmerge = function(tar, obj)
{
for (var key of Object.keys(obj)) {
if (typeof obj[key] === 'object') {
if (tar[key]) {
objmerge(tar[key], obj[key]);
continue;
}
else {
tar[key] = obj[key];
continue;
}
}
tar[key] = obj[key];
}
}
for (var obj of objs)
objmerge(target,obj);
}
Object.totalmerge = function(target, ...objs)
{
for (var obj of objs) {
for (var key in obj) {
if (typeof obj[key] === 'object') {
if (typeof target[key] === 'object') {
if (Object.isAccessor(target,key))
target[key] = obj[key];
else
Object.merge(target[key], obj[key]);
}
else
target[key] = obj[key];
} else
target[key] = obj[key];
}
}
}
/* Returns a new object with undefined, null, and empty values removed. */
Object.compact = function(obj)
{
}
Object.totalassign = function(to, from) Object.totalassign = function(to, from)
{ {

View file

@ -522,7 +522,16 @@ var editor = {
this.selectlist.forEach(function(x) { this.selectlist.forEach(function(x) {
var color = x.color ? x.color : Color.white; var color = x.color ? x.color : Color.white;
GUI.text(x.ur.toString(), world2screen(x.pos).add([0, 16]), 1, Color.purple); var ojson = JSON.parse(JSON.stringify(x));
delete ojson.pos;
delete ojson.angle;
var sname = x.ur.toString();
if (!ojson.empty)
x.dirty = true;
else
x.dirty = false;
if (x.dirty) sname += "*";
GUI.text(sname, world2screen(x.pos).add([0, 16]), 1, Color.purple);
for (var key in x.$) { for (var key in x.$) {
var o = x.$[key]; var o = x.$[key];
GUI.text(o.ur.toString(), world2screen(o.pos).add([0,16]),1,Color.purple); GUI.text(o.ur.toString(), world2screen(o.pos).add([0,16]),1,Color.purple);
@ -704,8 +713,8 @@ editor.inputs['C-a'] = function() {
}; };
editor.inputs['C-a'].doc = "Select all objects."; editor.inputs['C-a'].doc = "Select all objects.";
editor.inputs['`'] = function() { editor.openpanel(replpanel); } editor.inputs['C-`'] = function() { editor.openpanel(replpanel); }
editor.inputs['`'].doc = "Open or close the repl."; editor.inputs['C-`'].doc = "Open or close the repl.";
/* Return if selected component. */ /* Return if selected component. */
editor.inputs['h'] = function() { editor.inputs['h'] = function() {
@ -1494,8 +1503,8 @@ var replpanel = Object.copy(inputpanel, {
ecode += this.value; ecode += this.value;
this.value = ""; this.value = "";
var ret = eval(ecode);
Log.say(eval(ecode)); if (ret) Log.say(ret);
}, },
}); });
@ -1727,7 +1736,7 @@ var savetypeas = Object.copy(inputpanel, {
var quitpanel = Object.copy(inputpanel, { var quitpanel = Object.copy(inputpanel, {
title: "really quit?", title: "really quit?",
action() { action() {
quit(); Game.quit();
}, },
guibody () { guibody () {

View file

@ -319,42 +319,32 @@ 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); obj.$ = {};
obj.ur = ur; obj.ur = ur;
/* for (var prop in obj) {
if (typeof obj[prop] === 'object' && 'comp' in obj[prop]) {
var newcomp = component[obj[prop].comp].make(obj.body);
Object.assign(newcomp, obj[prop]);
newcomp.sync?.();
obj[prop] = newcomp;
obj[prop].defn('gameobject', obj);
obj.components[prop] = obj[prop];
}
};
*/
level.add_child(obj); level.add_child(obj);
for (var prop in obj) { for (var prop in ur) {
var p = obj[prop]; var p = ur[prop];
if (typeof p !== 'object') continue; if (typeof p !== 'object') continue;
if ('ur' in p) { if ('ur' in p) {
Log.warn(`spawning a ${prop} on this obj`); obj[prop] = obj.spawn(prototypes.get_ur(p.ur));
var newobj = obj.spawn(prototypes.get_ur(p.ur)); obj.$[prop] = obj[prop];
Object.assign(newobj, p); // Object.assign(obj[prop], p);
obj.$[prop] = newobj;
} else if ('make' in p) { } else if ('make' in p) {
obj[prop] = obj[prop].make(obj.body); obj[prop] = p.make(obj.body);
obj.components[prop] = obj[prop];
} else if ('comp' in p) {
obj[prop] = component[p.comp].make(obj.body);
obj.components[prop] = obj[prop]; obj.components[prop] = obj[prop];
} }
}; };
Object.totalmerge(obj,ur);
obj.check_registers(obj); obj.check_registers(obj);
if (typeof obj.start === 'function') obj.start(); if (typeof obj.start === 'function') obj.start();
return obj; return obj;
}, },
@ -470,7 +460,7 @@ prototypes.from_file = function(file)
} }
compile_env(script, newur, file); compile_env(script, newur, file);
Object.dainty_assign(newur, json); Object.merge(newur,json);
file = file.replaceAll('/', '.'); file = file.replaceAll('/', '.');
var path = file.name().split('.'); var path = file.name().split('.');
@ -486,10 +476,11 @@ prototypes.from_file = function(file)
prototypes.list.push(tag); prototypes.list.push(tag);
newur.toString = function() { return tag; }; newur.toString = function() { return tag; };
Object.assign(nested_access(ur,path), newur); ur[path] = nested_access(ur,path);
Object.assign(ur[path], newur);
nested_access(ur,path).__proto__ = newur.__proto__; nested_access(ur,path).__proto__ = newur.__proto__;
return nested_access(ur,path); return 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.";
prototypes.list = []; prototypes.list = [];

View file

@ -1314,11 +1314,9 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a
break; break;
case 7: case 7:
go->mass = js2number(argv[2]);
if (go->bodytype == CP_BODY_TYPE_DYNAMIC) if (go->bodytype == CP_BODY_TYPE_DYNAMIC)
cpBodySetMass(go->body, js2number(argv[2])); cpBodySetMass(go->body, go->mass);
else
YughWarn("Cannot set mass of a non dynamic body.");
break; break;
case 8: case 8:

View file

@ -8,6 +8,7 @@
#include "time.h" #include "time.h"
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <wchar.h>
#include "resources.h" #include "resources.h"
#include "stb_ds.h" #include "stb_ds.h"
@ -190,13 +191,17 @@ void input_btn(int btn, int state, uint32_t mod)
} }
} }
void input_key(int key, uint32_t mod) static const uint32_t UNCHAR_FLAGS = SAPP_MODIFIER_CTRL | SAPP_MODIFIER_ALT | SAPP_MODIFIER_SUPER;
void input_key(uint32_t key, uint32_t mod)
{ {
if (mod & UNCHAR_FLAGS) return;
if (key <= 31 || key >= 127) return;
JSValue argv[2]; JSValue argv[2];
char out[2] = {0}; char s[2] = {key, '\0'};
out[0] = (char)key;
argv[0] = JS_NewString(js, "char"); argv[0] = JS_NewString(js, "char");
argv[1] = JS_NewString(js, out); argv[1] = JS_NewString(js, s);
script_callee(pawn_callee, 2, argv); script_callee(pawn_callee, 2, argv);
JS_FreeValue(js, argv[0]); JS_FreeValue(js, argv[0]);

View file

@ -29,7 +29,7 @@ void input_mouse(int btn, int state, uint32_t mod);
void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod); void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod);
void input_mouse_scroll(float x, float y, uint32_t mod); void input_mouse_scroll(float x, float y, uint32_t mod);
void input_btn(int btn, int state, uint32_t mod); void input_btn(int btn, int state, uint32_t mod);
void input_key(int key, uint32_t mod); void input_key(uint32_t key, uint32_t mod);
const char *keyname_extd(int key); const char *keyname_extd(int key);
int action_down(int key); int action_down(int key);