Ur saving, ur subtyping
This commit is contained in:
parent
db8e59a8eb
commit
fe21b84785
|
@ -315,6 +315,13 @@ Object.defineProperty(String.prototype, 'shift', {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(String.prototype, 'strip_ext', {
|
||||||
|
value: function() {
|
||||||
|
var idx = this.lastIndexOf('.');
|
||||||
|
if (idx === -1) return this.slice();
|
||||||
|
return this.slice(0,idx);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'ext', {
|
Object.defineProperty(String.prototype, 'ext', {
|
||||||
value: function() {
|
value: function() {
|
||||||
|
@ -326,7 +333,7 @@ Object.defineProperty(String.prototype, 'ext', {
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'set_ext', {
|
Object.defineProperty(String.prototype, 'set_ext', {
|
||||||
value: function(val) {
|
value: function(val) {
|
||||||
return this.dir() + "/" + this.name() + val;
|
return this.dir() + this.name() + val;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -348,7 +355,8 @@ Object.defineProperty(String.prototype, 'base', {
|
||||||
Object.defineProperty(String.prototype, 'dir', {
|
Object.defineProperty(String.prototype, 'dir', {
|
||||||
value: function() {
|
value: function() {
|
||||||
var e = this.lastIndexOf('/');
|
var e = this.lastIndexOf('/');
|
||||||
return this.slice(0, e);
|
if (e === -1) return "";
|
||||||
|
return this.slice(0, e+1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -424,24 +424,8 @@ return;
|
||||||
this.openpanel(saveaspanel);
|
this.openpanel(saveaspanel);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var lvl = this.edit_level.save();
|
|
||||||
|
|
||||||
Log.info("Saving level of size " + lvl.length + " bytes.");
|
|
||||||
|
|
||||||
slurpwrite(lvl, this.edit_level.file);
|
|
||||||
this.edit_level.filejson = lvl;
|
|
||||||
this.edit_level.check_dirty();
|
|
||||||
|
|
||||||
if (this.edit_level.script) {
|
|
||||||
var scriptfile = this.edit_level.file.replace('.lvl', '.js');
|
|
||||||
slurpwrite(this.edit_level.script, scriptfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
Level.sync_file(this.edit_level.file);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
clear_level() {
|
clear_level() {
|
||||||
if (this.edit_level) {
|
if (this.edit_level) {
|
||||||
if (this.edit_level.level) {
|
if (this.edit_level.level) {
|
||||||
|
@ -634,7 +618,6 @@ return;
|
||||||
var ur = prototypes.get_ur(file);
|
var ur = prototypes.get_ur(file);
|
||||||
if (!ur) return;
|
if (!ur) return;
|
||||||
var obj = editor.edit_level.spawn(ur);
|
var obj = editor.edit_level.spawn(ur);
|
||||||
Log.warn(`editor loading ur ${file} with type ${JSON.stringify(prototypes.get_ur(file),null,2)}`);
|
|
||||||
obj.pos = Mouse.worldpos;
|
obj.pos = Mouse.worldpos;
|
||||||
this.selectlist = [obj];
|
this.selectlist = [obj];
|
||||||
},
|
},
|
||||||
|
@ -666,17 +649,24 @@ return;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
saveas_check(file) {
|
/* Checking to save an entity as a subtype. */
|
||||||
if (!file) return;
|
saveas_check(sub) {
|
||||||
|
if (!sub) return;
|
||||||
|
var curur = prototypes.get_ur(sub);
|
||||||
|
|
||||||
if (!file.endsWith(".lvl"))
|
if (curur) {
|
||||||
file = file + ".lvl";
|
|
||||||
|
|
||||||
if (IO.exists(file)) {
|
|
||||||
notifypanel.action = editor.saveas;
|
notifypanel.action = editor.saveas;
|
||||||
this.openpanel(gen_notify("Level already exists with that name. Overwrite?", this.saveas.bind(this, file)));
|
this.openpanel(gen_notify("Entity already exists with that name. Overwrite?", this.saveas.bind(this, sub)));
|
||||||
} else
|
} else {
|
||||||
this.saveas(file);
|
var path = sub.replaceAll('.', '/') + ".json";
|
||||||
|
IO.slurpwrite(JSON.stringify(editor.selectlist[0],null,1), path);
|
||||||
|
var t = editor.selectlist[0].transform();
|
||||||
|
editor.selectlist[0].kill();
|
||||||
|
editor.unselect();
|
||||||
|
editor.load(sub);
|
||||||
|
editor.selectlist[0].pos = t.pos;
|
||||||
|
editor.selectlist[0].angle = t.angle;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
saveas(file) {
|
saveas(file) {
|
||||||
|
@ -718,16 +708,16 @@ editor.inputs['C-h'].doc = "Unhide all objects.";
|
||||||
editor.inputs['C-e'] = function() { editor.openpanel(assetexplorer); };
|
editor.inputs['C-e'] = function() { editor.openpanel(assetexplorer); };
|
||||||
editor.inputs['C-e'].doc = "Open asset explorer.";
|
editor.inputs['C-e'].doc = "Open asset explorer.";
|
||||||
|
|
||||||
editor.inputs['C-l'] = function() { editor.openpanel(entitylistpanel, true); };
|
editor.inputs['C-l'] = function() { editor.openpanel(entitylistpanel); };
|
||||||
editor.inputs['C-l'].doc = "Open list of spawned entities.";
|
editor.inputs['C-l'].doc = "Open list of spawned entities.";
|
||||||
|
|
||||||
editor.inputs['C-i'] = function() {
|
/*editor.inputs['C-i'] = function() {
|
||||||
if (editor.selectlist.length !== 1) return;
|
if (editor.selectlist.length !== 1) return;
|
||||||
objectexplorer.obj = editor.selectlist[0];
|
objectexplorer.obj = editor.selectlist[0];
|
||||||
editor.openpanel(objectexplorer);
|
editor.openpanel(objectexplorer);
|
||||||
};
|
};
|
||||||
editor.inputs['C-i'].doc = "Open the object explorer for a selected object.";
|
editor.inputs['C-i'].doc = "Open the object explorer for a selected object.";
|
||||||
|
*/
|
||||||
editor.inputs['C-d'] = function() {
|
editor.inputs['C-d'] = function() {
|
||||||
if (editor.selectlist.length === 0) return;
|
if (editor.selectlist.length === 0) return;
|
||||||
var duped = editor.dup_objects(editor.selectlist);
|
var duped = editor.dup_objects(editor.selectlist);
|
||||||
|
@ -873,6 +863,13 @@ editor.inputs.escape = function() { editor.openpanel(quitpanel); }
|
||||||
editor.inputs.escape.doc = "Quit editor.";
|
editor.inputs.escape.doc = "Quit editor.";
|
||||||
|
|
||||||
editor.inputs['C-s'] = function() {
|
editor.inputs['C-s'] = function() {
|
||||||
|
if (editor.selectlist.length !== 1 || !editor.selectlist[0].dirty) return;
|
||||||
|
Log.warn(`Saving ur for ${editor.selectlist[0].toString()}`);
|
||||||
|
|
||||||
|
Object.merge(editor.selectlist[0].ur, editor.selectlist[0].json_obj());
|
||||||
|
IO.slurpwrite(JSON.stringify(editor.selectlist[0].ur,null,1), editor.selectlist[0].toString() + ".json");
|
||||||
|
return;
|
||||||
|
|
||||||
if (editor.edit_level.level) {
|
if (editor.edit_level.level) {
|
||||||
if (!editor.edit_level.unique)
|
if (!editor.edit_level.unique)
|
||||||
editor.save_current();
|
editor.save_current();
|
||||||
|
@ -889,6 +886,8 @@ editor.inputs['C-s'] = function() {
|
||||||
editor.inputs['C-s'].doc = "Save selected.";
|
editor.inputs['C-s'].doc = "Save selected.";
|
||||||
|
|
||||||
editor.inputs['C-S'] = function() {
|
editor.inputs['C-S'] = function() {
|
||||||
|
if (editor.selectlist.length !== 1) return;
|
||||||
|
saveaspanel.stem = this.selectlist[0].toString();
|
||||||
editor.openpanel(saveaspanel);
|
editor.openpanel(saveaspanel);
|
||||||
};
|
};
|
||||||
editor.inputs['C-S'].doc = "Save selected as.";
|
editor.inputs['C-S'].doc = "Save selected as.";
|
||||||
|
@ -1500,7 +1499,7 @@ var replpanel = Object.copy(inputpanel, {
|
||||||
ecode += this.value;
|
ecode += this.value;
|
||||||
Log.say(this.value);
|
Log.say(this.value);
|
||||||
this.value = "";
|
this.value = "";
|
||||||
var ret = eval(ecode);
|
var ret = function() {return eval(ecode);}.call(editor.selectlist[0]);
|
||||||
if (ret) Log.say(ret);
|
if (ret) Log.say(ret);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -1707,7 +1706,7 @@ var openlevelpanel = Object.copy(inputpanel, {
|
||||||
var saveaspanel = Object.copy(inputpanel, {
|
var saveaspanel = Object.copy(inputpanel, {
|
||||||
title: "save level as",
|
title: "save level as",
|
||||||
action() {
|
action() {
|
||||||
editor.saveas_check(this.value);
|
editor.saveas_check(this.stem + "." + this.value);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -279,35 +279,7 @@ var gameobject = {
|
||||||
},
|
},
|
||||||
|
|
||||||
json_obj() {
|
json_obj() {
|
||||||
function objdiff(from, to) {
|
return JSON.parse(JSON.stringify(this));
|
||||||
if (!to) return from; // Everything on from is unique
|
|
||||||
var ret = {};
|
|
||||||
|
|
||||||
for (var key in from) {
|
|
||||||
if (!from[key] || !to[key]) continue;
|
|
||||||
if (typeof from[key] === 'function') continue;
|
|
||||||
if (typeof to === 'object' && !(key in to)) continue;
|
|
||||||
|
|
||||||
if (typeof from[key] === 'object') {
|
|
||||||
if ('ur' in from[key]) {
|
|
||||||
var urdiff = objdiff(from[key],from[key].ur);
|
|
||||||
if (urdiff && !urdiff.empty) ret[key] = urdiff;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
var ur = Object.create(this.ur);
|
|
||||||
Object.assign(ur,objdiff(this,this.ur));
|
|
||||||
return ur;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
make_ur() {
|
make_ur() {
|
||||||
|
@ -317,6 +289,13 @@ var gameobject = {
|
||||||
return thisur;
|
return thisur;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
transform() {
|
||||||
|
var t = {};
|
||||||
|
t.pos = this.pos;
|
||||||
|
t.angle = this.angle;
|
||||||
|
return t;
|
||||||
|
},
|
||||||
|
|
||||||
dup(diff) {
|
dup(diff) {
|
||||||
var n = this.level.spawn(this.ur);
|
var n = this.level.spawn(this.ur);
|
||||||
Object.totalmerge(n, this.make_ur());
|
Object.totalmerge(n, this.make_ur());
|
||||||
|
@ -468,49 +447,65 @@ for (var key in prototypes) {
|
||||||
|
|
||||||
prototypes.save_gameobjects = function() { slurpwrite(JSON.stringify(gameobjects,null,2), "proto.json"); };
|
prototypes.save_gameobjects = function() { slurpwrite(JSON.stringify(gameobjects,null,2), "proto.json"); };
|
||||||
|
|
||||||
/* Makes a new ur-type from a file. The file can define components. */
|
/* Makes a new ur-type from disk. If the ur doesn't exist, it searches on the disk to create it. */
|
||||||
prototypes.from_file = function(file)
|
prototypes.from_file = function(file)
|
||||||
{
|
{
|
||||||
if (!IO.exists(file)) {
|
var urpath = file;
|
||||||
Log.error(`File ${file} does not exist.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var urpath = prototypes.file2ur(file);
|
|
||||||
var path = urpath.split('.');
|
var path = urpath.split('.');
|
||||||
|
if (path.length > 1 && (path.at(-1) === path.at(-2))) {
|
||||||
|
return prototypes.get_ur(path.at(-1));
|
||||||
|
}
|
||||||
|
|
||||||
var upperur = gameobject.ur;
|
var upperur = gameobject.ur;
|
||||||
|
|
||||||
if (path.length > 1) {
|
if (path.length > 1) {
|
||||||
var upperpath = path.slice(0,-1);
|
var upperpath = path.slice(0,-1);
|
||||||
upperur = prototypes.get_ur(upperpath.join('/'));
|
upperur = prototypes.get_ur(upperpath.join('/'));
|
||||||
|
if (!upperur) {
|
||||||
|
Log.error(`Attempted to create an UR ${urpath}, but ${upperpath} is not a defined UR.`);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var newur = Object.create(upperur);
|
var newur = Object.create(upperur);
|
||||||
var script = IO.slurp(file);
|
file = file.replaceAll('.','/');
|
||||||
|
|
||||||
var json = {};
|
var jsfile = file + ".js";
|
||||||
if (IO.exists(file.name() + ".json"))
|
var jsonfile = file + ".json";
|
||||||
json = JSON.parse(IO.slurp(file.name() + ".json"));
|
|
||||||
|
|
||||||
compile_env(script, newur, file);
|
var script = undefined;
|
||||||
|
var json = undefined;
|
||||||
|
|
||||||
|
if (IO.exists(jsfile))
|
||||||
|
script = IO.slurp(jsfile);
|
||||||
|
else {
|
||||||
|
jsfile = urpath + "/" + path.at(-1) + ".js";
|
||||||
|
if (IO.exists(jsfile)) script = IO.slurp(jsfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IO.exists(jsonfile))
|
||||||
|
json = JSON.parse(IO.slurp(jsonfile));
|
||||||
|
else {
|
||||||
|
jsonfile = urpath + "/" + path.at(-1) + ".json";
|
||||||
|
if (IO.exists(jsonfile)) json = JSON.parse(IO.slurp(jsonfile));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!json && !script) {
|
||||||
|
Log.warn(`Could not make ur from ${file}`);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script)
|
||||||
|
compile_env(script, newur, file);
|
||||||
|
|
||||||
|
json ??= {};
|
||||||
Object.merge(newur,json);
|
Object.merge(newur,json);
|
||||||
|
|
||||||
file = file.replaceAll('/', '.');
|
|
||||||
|
|
||||||
var nested_access = function(base, names) {
|
|
||||||
for (var i = 0; i < names.length; i++)
|
|
||||||
base = base[names[i]] = base[names[i]] || {};
|
|
||||||
|
|
||||||
return base;
|
|
||||||
};
|
|
||||||
|
|
||||||
prototypes.list.push(urpath);
|
prototypes.list.push(urpath);
|
||||||
|
|
||||||
newur.toString = function() { return urpath; };
|
newur.toString = function() { return urpath; };
|
||||||
ur[urpath] = nested_access(ur,path);
|
ur[urpath] = newur;
|
||||||
Object.assign(ur[urpath], newur);
|
|
||||||
nested_access(ur,path).__proto__ = newur.__proto__;
|
|
||||||
|
|
||||||
return ur[path];
|
return ur[urpath];
|
||||||
}
|
}
|
||||||
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 = [];
|
||||||
|
@ -532,7 +527,6 @@ prototypes.list_ur = function()
|
||||||
var list = [];
|
var list = [];
|
||||||
for (var e in obj) {
|
for (var e in obj) {
|
||||||
list.push(prefix + e);
|
list.push(prefix + e);
|
||||||
Log.warn("Descending into " + e);
|
|
||||||
list.concat(list_obj(obj[e], e + "."));
|
list.concat(list_obj(obj[e], e + "."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,38 +536,43 @@ prototypes.list_ur = function()
|
||||||
return list_obj(ur);
|
return list_obj(ur);
|
||||||
}
|
}
|
||||||
|
|
||||||
prototypes.file2ur(file)
|
prototypes.file2ur = function(file)
|
||||||
{
|
{
|
||||||
|
file = file.strip_ext();
|
||||||
file = file.replaceAll('/','.');
|
file = file.replaceAll('/','.');
|
||||||
return file.name();
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns an ur, or makes it, for any given type of path
|
||||||
|
could be a file on a disk like ball/big.js
|
||||||
|
could be an ur path like ball.big
|
||||||
|
*/
|
||||||
prototypes.get_ur = function(name)
|
prototypes.get_ur = function(name)
|
||||||
{
|
{
|
||||||
var urpath = prototypes.file2ur(name);
|
var urpath = name;
|
||||||
if (!prototypes.ur[name]) {
|
if (urpath.includes('/'))
|
||||||
if (IO.exists(name.name() + ".js")) {
|
urpath = prototypes.file2ur(name);
|
||||||
prototypes.from_file(name.name() + ".js");
|
|
||||||
return prototypes.ur[name];
|
if (!prototypes.ur[urpath]) {
|
||||||
} else {
|
var ur = prototypes.from_file(urpath);
|
||||||
|
Log.warn(`tried to make ${urpath}`);
|
||||||
|
if (ur)
|
||||||
|
return ur;
|
||||||
|
else {
|
||||||
Log.warn(`Could not find prototype using name ${name}.`);
|
Log.warn(`Could not find prototype using name ${name}.`);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
return prototypes.ur[name];
|
return prototypes.ur[urpath];
|
||||||
}
|
}
|
||||||
|
|
||||||
prototypes.generate_ur = function(path)
|
prototypes.generate_ur = function(path)
|
||||||
{
|
{
|
||||||
var ob = IO.glob("**.js");
|
var ob = IO.glob("**.js");
|
||||||
ob = ob.filter(function(str) { return !str.startsWith("scripts"); });
|
ob = ob.concat(IO.glob("**.json"));
|
||||||
|
ob = ob.filter(function(path) { return path !== "game.js" && path !== "play.js" });
|
||||||
ob.forEach(function(name) {
|
ob = ob.map(function(path) { return path.set_ext(""); });
|
||||||
if (name === "game.js") return;
|
ob.forEach(function(name) { prototypes.get_ur(name); });
|
||||||
if (name === "play.js") return;
|
|
||||||
Log.warn("generating for " + name);
|
|
||||||
prototypes.get_ur(name);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ur = prototypes.ur;
|
var ur = prototypes.ur;
|
||||||
|
|
|
@ -63,8 +63,6 @@ void mYughLog(int category, int priority, int line, const char *file, const char
|
||||||
// js_stacktrace();
|
// js_stacktrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (priority >= 2)
|
|
||||||
print_stacktrace();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue