improved UR loading

This commit is contained in:
John Alanbrook 2023-09-26 13:37:19 +00:00
parent 9a1f1408a6
commit db8e59a8eb
4 changed files with 67 additions and 63 deletions

View file

@ -140,7 +140,6 @@ var gif2anim = function(gif)
anim.frames = [];
anim.path = gif;
var frames = cmd(139,gif);
Log.warn(`gif has ${frames} frames`);
var yslice = 1/frames;
for (var f = 0; f < frames; f++) {
var frame = {};

View file

@ -628,18 +628,13 @@ return;
killring: [],
killcom: [],
paste() {
this.selectlist = this.dup_objects(this.killring);
this.selectlist.forEach(function(x) {
x.pos = x.pos.sub(this.killcom).add(this.cursor);
},this);
},
lvl_history: [],
load(file) {
var obj = editor.edit_level.spawn(prototypes.get_ur(file));
var ur = prototypes.get_ur(file);
if (!ur) return;
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;
this.selectlist = [obj];
},
@ -878,7 +873,6 @@ editor.inputs.escape = function() { editor.openpanel(quitpanel); }
editor.inputs.escape.doc = "Quit editor.";
editor.inputs['C-s'] = function() {
if (editor.edit_level.level) {
if (!editor.edit_level.unique)
editor.save_current();
@ -1190,6 +1184,7 @@ editor.inputs.delete = function() {
};
editor.inputs.delete.doc = "Delete selected objects.";
editor.inputs['S-d'] = editor.inputs.delete;
editor.inputs['C-k'] = editor.inputs.delete;
editor.inputs['C-u'] = function() {
this.selectlist.forEach(function(x) {
@ -1274,22 +1269,31 @@ editor.inputs['C-rb'].rep = true;
editor.inputs['C-c'] = function() {
this.killring = [];
this.killcom = [];
this.killcom = find_com(this.selectlist);
this.selectlist.forEach(function(x) {
this.killring.push(x);
this.killring.push(x.make_ur());
},this);
this.killcom = find_com(this.killring);
};
editor.inputs['C-c'].doc = "Copy selected objects to killring.";
editor.inputs['C-x'] = function() {
editor.inputs['C-c']();
this.killring.forEach(function(x) { x.kill(); });
editor.inputs['C-c'].call(editor);
this.selectlist.forEach(function(x) { x.kill(); });
editor.unselect();
};
editor.inputs['C-x'].doc = "Cut objects to killring.";
editor.inputs['C-v'] = function() { editor.paste(); };
editor.inputs['C-v'] = function() {
this.unselect();
this.killring.forEach(function(x) {
editor.selectlist.push(editor.edit_level.spawn(x));
});
this.selectlist.forEach(function(x) {
x.pos = x.pos.sub(this.killcom).add(this.cursor);
},this);
};
editor.inputs['C-v'].doc = "Pull objects from killring to world.";
editor.inputs.char = function(c) {
@ -1298,7 +1302,7 @@ editor.inputs.char = function(c) {
var brushmode = {};
brushmode.inputs = {};
brushmode.inputs.lm = function() { editor.paste(); };
brushmode.inputs.lm = function() { editor.inputs['C-v'].call(editor); };
brushmode.inputs.lm.doc = "Paste selected brush.";
brushmode.inputs.b = function() {

View file

@ -305,19 +305,22 @@ var gameobject = {
if (ret.empty) return undefined;
return ret;
}
var ur = objdiff(this,this.ur);
return ur ? ur : {};
var ur = Object.create(this.ur);
Object.assign(ur,objdiff(this,this.ur));
return ur;
},
make_ur() {
var thisur = this.json_obj();
thisur.pos = this.pos;
thisur.angle = this.angle;
return thisur;
},
dup(diff) {
var dup = this.level.spawn(this.ur);
var thisur = this.json_obj();
thisur.pos = this.pos;
thisur.angle = this.angle;
Object.totalmerge(dup, thisur);
return dup;
var n = this.level.spawn(this.ur);
Object.totalmerge(n, this.make_ur());
return n;
},
kill() {
@ -396,6 +399,7 @@ var gameobject = {
};
Object.totalmerge(obj,ur);
obj.$.forEach(function(x) { x.pos = obj.pos.add(x.pos); });
obj.check_registers(obj);
if (typeof obj.start === 'function') obj.start();
@ -471,23 +475,27 @@ prototypes.from_file = function(file)
Log.error(`File ${file} does not exist.`);
return;
}
var urpath = prototypes.file2ur(file);
var path = urpath.split('.');
var upperur = gameobject.ur;
if (path.length > 1) {
var upperpath = path.slice(0,-1);
upperur = prototypes.get_ur(upperpath.join('/'));
}
var newur = Object.create(gameobject.ur);
newur.$ = {};
var newur = Object.create(upperur);
var script = IO.slurp(file);
var json = {};
if (IO.exists(file.name() + ".json")) {
var json = JSON.parse(IO.slurp(file.name() + ".json"));
Object.assign(newur.$, json.$);
delete json.$;
}
if (IO.exists(file.name() + ".json"))
json = JSON.parse(IO.slurp(file.name() + ".json"));
compile_env(script, newur, file);
Object.merge(newur,json);
file = file.replaceAll('/', '.');
var path = file.name().split('.');
var nested_access = function(base, names) {
for (var i = 0; i < names.length; i++)
base = base[names[i]] = base[names[i]] || {};
@ -495,13 +503,11 @@ prototypes.from_file = function(file)
return base;
};
var instances = [];
var tag = file.name();
prototypes.list.push(tag);
prototypes.list.push(urpath);
newur.toString = function() { return tag; };
ur[path] = nested_access(ur,path);
Object.assign(ur[path], newur);
newur.toString = function() { return urpath; };
ur[urpath] = nested_access(ur,path);
Object.assign(ur[urpath], newur);
nested_access(ur,path).__proto__ = newur.__proto__;
return ur[path];
@ -517,17 +523,6 @@ prototypes.from_obj = function(name, obj)
return prototypes.ur[name];
}
prototypes.load_config = function(name)
{
if (!prototypes.ur[name])
prototypes.ur[name] = gameobject.clone(name);
Log.warn(`Made new ur of name ${name}`);
return prototypes.ur[name];
}
prototypes.list_ur = function()
{
var list = [];
@ -547,14 +542,23 @@ prototypes.list_ur = function()
return list_obj(ur);
}
prototypes.file2ur(file)
{
file = file.replaceAll('/','.');
return file.name();
}
prototypes.get_ur = function(name)
{
var urpath = prototypes.file2ur(name);
if (!prototypes.ur[name]) {
if (IO.exists(name + ".js"))
prototypes.from_file(name + ".js");
prototypes.load_config(name);
return prototypes.ur[name];
if (IO.exists(name.name() + ".js")) {
prototypes.from_file(name.name() + ".js");
return prototypes.ur[name];
} else {
Log.warn(`Could not find prototype using name ${name}.`);
return undefined;
}
} else
return prototypes.ur[name];
}
@ -567,8 +571,8 @@ prototypes.generate_ur = function(path)
ob.forEach(function(name) {
if (name === "game.js") return;
if (name === "play.js") return;
prototypes.from_file(name);
Log.warn("generating for " + name);
prototypes.get_ur(name);
});
}

View file

@ -118,9 +118,6 @@ struct sFont *MakeFont(const char *fontfile, int height) {
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
unsigned char *ttf_buffer = slurp_file(fontfile, NULL);
unsigned char *bitmap = malloc(packsize * packsize);