This commit is contained in:
2023-05-02 01:58:10 +00:00
parent bea5b326cf
commit d3cb9278a9
9 changed files with 194 additions and 79 deletions

View File

@@ -105,6 +105,10 @@ var sprite = clone(component, {
cmd(37, this.id, this.pos);
},
set color(x) {
cmd(96, this.id, x);
},
load_img(img) {
cmd(12, this.id, img, this.rect);
},
@@ -679,3 +683,35 @@ var circle2d = clone(collider2d, {
this.coll_sync();
},
});
/* ASSETS */
var Texture = {
mipmaps(path, x) {
cmd(94, path, x);
},
sprite(path, x) {
cmd(95, path, x);
},
};
var Resources = {
load(path) {
if (path in this)
return this[path];
var src = {};
this[path] = src;
src.path = path;
if (!IO.exists(`${path}.asset`))
return this[path];
var data = JSON.parse(IO.slurp(`${path}.asset`));
Object.assign(src,data);
return this[path];
},
};

View File

@@ -420,7 +420,7 @@ var Action = {
var Player = {
players: [],
input(fn, ...args) {
this.pawns.forEach(x => { if (fn in x) x[fn](...args); });
this.pawns.forEach(x => x[fn]?.(...args));
},
control(pawn) {
@@ -440,6 +440,7 @@ for (var i = 0; i < 4; i++) {
}
function state2str(state) {
if (typeof state === 'string') return state;
switch (state) {
case 0:
return "down";
@@ -723,6 +724,7 @@ var Game = {
},
};
var Level = {
levels: [],
objects: [],
@@ -922,6 +924,8 @@ var Level = {
newobj.defn('level', this);
this.objects.push(newobj);
Game.register_obj(newobj);
newobj.setup?.();
newobj.start?.();
return newobj;
},
@@ -1185,9 +1189,11 @@ var Level = {
get left() {
return [-1,0].rotate(Math.deg2rad(this.angle));
},
};
var World = Level.create();
World.name = "World";
var gameobjects = {};
/* Returns the index of the smallest element in array, defined by a function that returns a number */
@@ -1211,7 +1217,13 @@ function grab_from_points(pos, points, slop) {
var gameobject = {
get scale() { return this._scale; },
set scale(x) { this._scale = Math.max(0,x); if (this.body > -1) cmd(36, this.body, this._scale); this.sync(); },
set scale(x) {
this._scale = Math.max(0,x);
if (this.body > -1)
cmd(36, this.body, this._scale);
this.sync();
},
_scale: 1.0,
save: true,
@@ -1309,6 +1321,26 @@ var gameobject = {
body: -1,
controlled: false,
get properties() {
var keys = [];
for (var key of Object.keys(this)) {
if (key.startsWith("_"))
keys.push(key.substring(1));
else
keys.push(key);
}
return keys;
},
toJSON() {
var obj = {};
for (var key of this.properties)
obj[key] = this[key];
return obj;
},
set_center(pos) {
var change = pos.sub(this.pos);
@@ -1515,6 +1547,7 @@ var gameobject = {
this2world(pos) { return cmd(71, this.body,pos); },
make(props, level) {
level ??= World;
var obj = Object.create(this);
this.instances.push(obj);
obj.toString = function() {
@@ -1584,39 +1617,11 @@ var gameobject = {
}
var locks = ['draw_layer', 'friction','elasticity', 'visible', 'body', 'flipx', 'flipy', 'scale', 'controlled', 'selectable', 'save', 'velocity', 'angularvelocity', 'alive', 'boundingbox', 'name'];
var locks = ['draw_layer', 'friction','elasticity', 'visible', 'body', 'flipx', 'flipy', 'controlled', 'selectable', 'save', 'velocity', 'angularvelocity', 'alive', 'boundingbox', 'name', 'scale', 'angle', 'properties', 'moi', 'relpos', 'relangle', 'up', 'down', 'right', 'left', 'bodytype', 'gizmo', 'pos'];
locks.forEach(function(x) {
Object.defineProperty(gameobject, x, {enumerable:false});
});
function private_non_enumerable(obj) {
for (var key in obj) {
if (key.startsWith('_'))
Object.defineProperty(obj, key, {enumerable:false});
}
}
function add_sync_prop(obj, prop, syncfn) {
var hidden = "_"+prop;
Log.info(hidden);
Object.defineProperty(obj, hidden, {
value: null,
writable: true,
});
Object.defineProperty(obj, prop, {
get: function() { return obj[hidden]; },
set: function(x) {
obj[hidden] = x;
syncfn(obj[hidden]);
},
enumerable: true,
});
return obj;
};
/* Load configs */
function load_configs(file) {
var configs = JSON.parse(IO.slurp(file));
@@ -1761,5 +1766,3 @@ for (var key in prototypes) {
}
function save_gameobjects_as_prototypes() { slurpwrite(JSON.stringify(gameobjects,null,2), "proto.json"); };
Resources = {};