Removed more Javascript "features"; simplified ur with closures

This commit is contained in:
John Alanbrook 2023-09-19 22:37:54 +00:00
parent a57aaeb5d5
commit 266ad65ba7
6 changed files with 55 additions and 98 deletions

View file

@ -90,6 +90,17 @@ Ur-ur, the thing all Ur-types derive from
All ur-types and variants can be created in the world, where they become a true blue ENTITY. Entities can be under entities infinitely.
Ur-types have a specific combination of components. When a component is removed from an entity, the link to its ur-type breaks.
Entities have access to their ur-type through their .ur parameter. Each ur-type and variant likewise stores a list of entities that have been created from them.
When an entity changes from its ur-type, it can be saved as its own variant, or merged up to its ur type. Brand new entities can be spawned via duping an already existing one (which retains its differences), or by requesting one from an ur type. Ur types are accessed through dot notation - ur.urtype.var1.vara. The 'ur' global object stores all ur types.
the "ur" object has a few fields.
ur.type : the actual object
ur.instances : instances of it
ur.tag : the full name of it
### Loading traits
Traits are defined by code and a data file. When an Ur-type is extended with a trait, the code is run, and then the data file contains modifications and

View file

@ -1,3 +1,22 @@
/* Removing crud I don't like */
/* Functions that lead to bloated error prone javascript */
/* It is EMCA6 but without a lot of builtin objects and various functions. There are no:
* Promises and so on (Generators, async)
* WeakMaps and so on (weakset, weakref)
* Typed arrays
* Proxys
* Modules
* Symbols (use closures)
In addition to the removal of a bunch of stuff as seen here.
Access prototypes through __proto__ instead of the long-winded Object.getProtoTypeOf.
*/
var FNRM = function() { Log.error("removed"); };
Object.getPrototypeOf = FNRM;
Object.setPrototypeOf = FNRM;
Reflect = {};
Symbol = {};
URIError = {};
/* Prototypes out an object and extends with values */
function clone(proto, binds) {
var c = Object.create(proto);
@ -13,17 +32,6 @@ function copy(proto, binds) {
};
/* OBJECT DEFININTIONS */
Object.defineProperty(Object.prototype, 'getOwnPropertyDescriptors', {
value: function() {
var obj = {};
for (var key in this) {
obj[key] = Object.getOwnPropertyDescriptor(this, key);
}
return obj;
}
});
Object.defHidden = function(obj, prop)
{
Object.defineProperty(obj, prop, {enumerable:false, writable:true});

View file

@ -93,7 +93,6 @@ var editor = {
pobj.from = this.selectlist[0].__proto__.from;
protos[this.selectlist[0].__proto__.name] = pobj;
Log.warn(JSON.stringify(protos));
slurpwrite(JSON.stringify(protos, undefined, 2), "proto.json");
@ -482,20 +481,6 @@ var editor = {
GUI.text("MODE: " + this.edit_mode, [0,500]);
Debug.point(world2screen(this.cursor), 2, Color.green);
/*
this.selectlist.forEach(function(x) {
var p = [];
p.push(world2screen(this.cursor));
p.push(world2screen(x.pos));
Debug.line(p, Color.green);
},this);
*/
if (this.programmode) {
this.edit_level.objects.forEach(function(x) {
if (x.hasOwn('varname')) GUI.text(x.varname, world2screen(x.pos).add([0,32]), 1, [84,110,255]);
});
}
if (this.comp_info && this.sel_comp) {
GUI.text(Input.print_pawn_kbm(this.sel_comp), [100,700],1);
@ -539,7 +524,7 @@ var editor = {
GUI.text(x.toString(), world2screen(x.pos).add([0, 16]), 1, color);
GUI.text(x.pos.map(function(x) { return Math.round(x); }), world2screen(x.pos), 1, color);
Debug.arrow(world2screen(x.pos), world2screen(x.pos.add(x.up().scale(40))), Color.yellow, 1);
if (x.hasOwn('varname')) GUI.text(x.varname, world2screen(x.pos).add([0,32]), 1, [84,110,255]);
if ('gizmo' in x && typeof x['gizmo'] === 'function' )
x.gizmo();
@ -1030,33 +1015,6 @@ editor.inputs.f2 = function() {
};
editor.inputs.f2.doc = "Open configurations object.";
editor.inputs['C-j'] = function() {
var varmakes = this.selectlist.filter(function(x) { return !x.hasOwn('varname'); });
varmakes.forEach(function(x) {
var allvnames = [];
this.edit_level.objects.forEach(function(x) {
if (x.varname)
allvnames.push(x.varname);
});
var vname = x.from.replace(/ /g, '_').replace(/_object/g, '').replace(/\..*$/g, '');
var tnum = 1;
var testname = vname + "_" + tnum;
while (allvnames.includes(testname)) {
tnum++;
testname = vname + "_" + tnum;
}
x.varname = testname;
},this);
};
editor.inputs['C-j'].doc = "Give selected objects a variable name.";
editor.inputs['M-j'] = function() {
var varmakes = this.selectlist.filter(function(x) { return x.hasOwn('varname'); });
varmakes.forEach(function(x) { delete x.varname; });
};
editor.inputs['M-j'].doc = "Remove variable names from selected objects.";
editor.inputs.lm = function() { editor.sel_start = Mouse.worldpos; };
editor.inputs.lm.doc = "Selection box.";
@ -1930,4 +1888,6 @@ if (IO.exists("editor.config"))
editor.clear_level();
editor.camera = Game.camera;
Game.stop();
Game.editor_mode(false);
Game.editor_mode(true);
load("editorconfig.js");

View file

@ -548,23 +548,13 @@ Register.update.register(Game.exec, Game);
load("scripts/entity.js");
var World = Object.create(gameobject);
var preprimum = {};
preprimum.add_child = function() {};
var World = gameobject.make(preprimum);
var Primum = World;
gameobject.make_parentable(Primum);
Primum.tag = "PRIMUM";
Primum.selectable = false;
Primum.ur = { tag: "Primum" };
Primum.spawn = function(ur) {
if (typeof ur === 'string')
ur = prototypes.get_ur(ur);
return ur.type.make(this);
};
/* Reparent this object to a new one */
World.reparent = function(parent) { Log.warn("Cannot reparent the Primum."); }
World.unparent = function() { Log.warn("The Primum has no parent, always."); }
World.fullpath = function() { return World.name; };
/* Load configs */
function load_configs(file) {

View file

@ -76,8 +76,6 @@ var gameobject = {
}
},
varname: "",
pos: [0,0],
set relpos(x) {
@ -112,7 +110,6 @@ var gameobject = {
},
width() {
var bb = this.boundingbox();
return bb.r - bb.l;
@ -173,14 +170,6 @@ var gameobject = {
make(level) {
level ??= Primum;
var obj = Object.create(this);
// this.instances.push(obj);
// obj.ur = this;
obj.toString = function() {
if (obj.ur)
return obj.ur.tag;
return "NO UR"};
obj.defn('body', make_gameobject(this.scale,
this.phys,
this.mass,
@ -195,6 +184,7 @@ var gameobject = {
/* 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); },
@ -245,8 +235,9 @@ var gameobject = {
spawn(ur) {
if (typeof ur === 'string')
ur = prototypes.get_ur(ur);
if (!ur) Log.warn("Failed to make UR from " + ur);
return ur.type.make(this);
return ur.make(this);
},
@ -282,7 +273,7 @@ var gameobject = {
},
dup(diff) {
var dup = Primum.spawn(this.ur);
var dup = Primum.spawn(this.__proto__);
Object.assign(dup, this);
return dup;
},
@ -330,7 +321,7 @@ var gameobject = {
var prop = Object.getOwnPropertyDescriptor(this, key);
if (!prop) continue;
if (prop.get) {
if (prop.get() !== Object.getPrototypeOf(this)[key])
if (prop.get() !== this.__proto__[key])
ret[key] = prop.get();
}
else
@ -469,16 +460,16 @@ prototypes.from_file = function(file)
return base;
};
var a = nested_access(ur, path);
Object.defHidden(a, 'instances');
a.instances = [];
a.tag = file.name();
prototypes.list.push(a.tag);
a.type = newur;
a.instances = [];
// newur.ur = a;
return a;
var instances = [];
var tag = file.name();
prototypes.list.push(tag);
newur.toString = function() { return tag; };
Object.assign(nested_access(ur,path), newur);
nested_access(ur,path).__proto__ = newur.__proto__;
return nested_access(ur,path);
}
prototypes.from_file.doc = "Create a new ur-type from a given script file.";
prototypes.list = [];
@ -486,11 +477,8 @@ prototypes.list = [];
prototypes.from_obj = function(name, obj)
{
var newobj = gameobject.clone(name, obj);
prototypes.ur[name] = {
tag: name,
type: newobj
};
newobj.ur = prototypes.ur[name];
prototypes.ur[name] = newobj;
newobj.toString = function() { return name; };
return prototypes.ur[name];
}

View file

@ -73,7 +73,7 @@ static float timescale = 1.f;
static int sim_play = SIM_PLAY;
static int editor_mode = 0;
int editor_mode = 0;
#ifdef __TINYC__
int backtrace(void **buffer, int size) {