merge made functions into original prototype

This commit is contained in:
John Alanbrook 2023-09-20 13:11:14 +00:00
parent 266ad65ba7
commit 31331af57d
5 changed files with 79 additions and 79 deletions

View file

@ -34,40 +34,39 @@ var sprite = clone(component, {
angle: 0,
rect: {s0:0, s1: 1, t0: 0, t1: 1},
get enabled() { return cmd(114,this.id); },
set enabled(x) { cmd(20,this.id,x); },
set color(x) { cmd(96,this.id,x); },
get color() {return undefined; },
get pos() { return cmd(111, this.id); },
set pos(x) { cmd(37,this.id,x); },
set layer(x) { cmd(60, this.id, x); },
get layer() { return undefined; },
boundingbox() {
var dim = this.dimensions();
dim = dim.scale(this.gameobject.scale);
var realpos = this.pos.copy();
realpos.x = realpos.x * dim.x + (dim.x/2);
realpos.y = realpos.y * dim.y + (dim.y/2);
return cwh2bb(realpos,dim);
},
sync() {
if (this.path)
cmd(12,this.id,this.path,this.rect);
},
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; },
make(go) {
var sprite = Object.create(this);
var id = make_sprite(go,this.path,this.pos);
complete_assign(sprite, {
get enabled() { return cmd(114,id); },
set enabled(x) { cmd(20,id,x); },
set color(x) { cmd(96,id,x); },
get color() {return undefined; },
get pos() { return cmd(111, id); },
set pos(x) { cmd(37,id,x); },
set layer(x) { cmd(60, id, x); },
get layer() { return undefined; },
boundingbox() {
var dim = this.dimensions();
dim = dim.scale(this.gameobject.scale);
var realpos = this.pos.copy();
realpos.x = realpos.x * dim.x + (dim.x/2);
realpos.y = realpos.y * dim.y + (dim.y/2);
return cwh2bb(realpos,dim);
},
sync() {
if (this.path)
cmd(12,id,this.path,this.rect);
},
kill() { cmd(9,id); },
});
sprite.id = make_sprite(go);
sprite.layer = 1;
return sprite;
},
@ -634,27 +633,11 @@ var circle2d = clone(collider2d, {
set pos(x) { this.offset = x; },
make(go) {
var circle = clone(this);
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});
complete_assign(circle, {
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]);
},
});
complete_assign(circle, this.make_fns);
return circle;
},
@ -666,6 +649,22 @@ 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]);
},
});
/* ASSETS */

View file

@ -239,8 +239,8 @@ var DebugControls = {};
DebugControls.inputs = {};
DebugControls.inputs.f1 = function () { Debug.draw_phys(!Debug.phys_drawing); };
DebugControls.inputs.f1.doc = "Draw physics debugging aids.";
DebugControls.inputs.f3 = function() { Debug.draw_bb = !Debug.draw_bb; };
DebugControls.inputs.f3.doc = "Toggle drawing bounding boxes.";
//DebugControls.inputs.f3 = function() { Debug.draw_bb = !Debug.draw_bb; };
//DebugControls.inputs.f3.doc = "Toggle drawing bounding boxes.";
DebugControls.inputs.f4 = function() {
Debug.draw_names = !Debug.draw_names;
Debug.draw_gizmos = !Debug.draw_gizmos;

View file

@ -748,13 +748,13 @@ editor.inputs['C-d'] = function() {
editor.inputs['C-d'].doc = "Duplicate all selected objects.";
editor.inputs.f3 = function() {
Log.say("Selected JSON ...");
editor.selectlist.forEach(x => Log.say(JSON.stringify(x,null,2)));
Log.say("UR JSON ...");
for (var key of Object.keys(editor.selectlist[0].ur.type))
Log.say(key);
editor.selectlist.forEach(x => Log.say(JSON.stringify(x.ur.type,null,2)));
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))
});
};
editor.inputs['C-m'] = function() {

View file

@ -313,21 +313,28 @@ var gameobject = {
right() { return [1,0].rotate(Math.deg2rad(this.angle));},
left() { return [-1,0].rotate(Math.deg2rad(this.angle));},
/* Given an ur-type, spawn one attached to us */
toJSON() {
var ret = {};
for (var key in this) {
var prop = Object.getOwnPropertyDescriptor(this, key);
if (!prop) continue;
if (prop.get) {
if (prop.get() !== this.__proto__[key])
ret[key] = prop.get();
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];
}
else
ret[key] = this[key];
Log.say("Returning a obj with values ...");
for (var key in ret)
Log.say(key);
if (ret.empty) return undefined;
return ret;
}
return ret;
return objdiff(this, this.__proto__);
},
});
@ -339,6 +346,8 @@ var gameobject = {
}
};
dainty_assign(obj, this);
obj.check_registers(obj);
gameobject.make_parentable(obj);
@ -402,6 +411,8 @@ gameobject.make_parentable = function(obj) {
obj.objects = objects;
}
gameobject.entity = {};
/* Default objects */
var prototypes = {};
prototypes.ur = {};
@ -468,7 +479,7 @@ prototypes.from_file = function(file)
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.";

View file

@ -1400,17 +1400,7 @@ JSValue duk_q_body(JSContext *js, JSValueConst this, int argc, JSValueConst *arg
}
JSValue duk_make_sprite(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
int go = js2int(argv[0]);
const char *path = JS_ToCString(js, argv[1]);
HMM_Vec2 pos = js2hmmv2(argv[2]);
int sprite = make_sprite(go);
struct sprite *sp = id2sprite(sprite);
sprite_loadtex(sp, path, ST_UNIT);
sp->pos = pos;
JS_FreeCString(js, path);
return JS_NewInt64(js, sprite);
return JS_NewInt64(js, make_sprite(js2int(argv[0])));
}
/* Make anim from texture */
@ -1669,7 +1659,7 @@ void ffi_load() {
DUK_FUNC(sys_cmd, 1)
DUK_FUNC(make_sprite, 3)
DUK_FUNC(make_sprite, 1)
DUK_FUNC(make_anim2d, 3)
DUK_FUNC(spline_cmd, 6)