Add ur prototype idea

This commit is contained in:
2023-09-08 06:26:48 +00:00
parent a4111b01a5
commit cc879746e3
4 changed files with 110 additions and 14 deletions

View File

@@ -51,7 +51,7 @@ void mYughLog(int category, int priority, int line, const char *file, const char
snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer);
log_print(buffer);
if (category != LOG_SCRIPT && priority >= 2)
if (category == LOG_SCRIPT && priority >= 2)
js_stacktrace();
}
#endif

View File

@@ -198,6 +198,34 @@ Object.defineProperty(String.prototype, 'shift', {
});
Object.defineProperty(String.prototype, 'ext', {
value: function() {
return this.slice(this.lastIndexOf('.'));
}
});
Object.defineProperty(String.prototype, 'name', {
value: function() {
var s = this.lastIndexOf('/');
var e = this.lastIndexOf('.');
return this.slice(s+1,e);
}
});
Object.defineProperty(String.prototype, 'base', {
value: function() {
return this.slice(this.lastIndexOf('/')+1);
}
});
Object.defineProperty(String.prototype, 'dir', {
value: function() {
var e = this.lastIndexOf('/');
return this.slice(0, e);
}
});
/* ARRAY DEFS */
Object.defineProperty(Array.prototype, 'copy', {

View File

@@ -16,8 +16,6 @@ function initialize()
if (IO.exists("config.js"))
load("config.js");
// prototypes.load_all();
if (Cmdline.play)
run("scripts/play.js");
else
@@ -243,6 +241,7 @@ var Keys = {
load("scripts/physics.js");
load("scripts/input.js");
load("scripts/sound.js");
function screen2world(screenpos) { return Game.camera.view2world(screenpos); }
function world2screen(worldpos) { return Game.camera.world2view(worldpos); }
@@ -1122,19 +1121,9 @@ Game.view_camera(World.spawn(camera2d));
win_make(Game.title, Game.resolution[0], Game.resolution[1]);
/* Default objects */
gameobject.clone("polygon2d", {
polygon2d: polygon2d.clone(),
});
gameobject.clone("edge2d", {
edge2d: bucket.clone(),
});
gameobject.clone("sprite", {
sprite: sprite.clone(),
});
var prototypes = {};
prototypes.ur = {};
prototypes.load_all = function()
{
if (IO.exists("proto.json"))
@@ -1170,7 +1159,57 @@ prototypes.from_file = function(file)
var newobj = gameobject.clone(file, {});
var script = IO.slurp(file);
compile_env(`var self = this;${script}`, newobj, file);
prototypes.ur[file.name()] = newobj;
return newobj;
}
prototypes.from_file.doc = "Create a new ur-type from a given script file.";
prototypes.from_obj = function(name, obj)
{
var newobj = gameobject.clone(name, obj);
prototypes.ur[name] = newobj;
return newobj;
}
prototypes.load_config = function(name)
{
if (!prototypes.config) {
prototypes.config = {};
if (IO.exists("proto.json"))
prototypes.config = JSON.parse(IO.slurp("proto.json"));
}
Log.warn(`Loading a config for ${name}`);
if (!prototypes.ur[name])
prototypes.ur[name] = gameobject.clone(name);
return prototypes.ur[name];
}
prototypes.get_ur = function(name)
{
if (!prototypes.ur[name]) {
if (IO.exists(name + ".js"))
prototypes.from_file(name + ".js");
prototypes.load_config(name);
return prototypes.ur[name];
} else
return prototypes.ur[name];
}
var Gamestate = {};
prototypes.from_obj("polygon2d", {
polygon2d: polygon2d.clone(),
});
prototypes.from_obj("edge2d", {
edge2d: bucket.clone(),
});
prototypes.from_obj("sprite", {
sprite: sprite.clone(),
});