fix makefile; prettify javascript

This commit is contained in:
John Alanbrook 2024-09-26 11:36:09 -05:00
parent f95249f147
commit f35c77c4a9
34 changed files with 4384 additions and 4203 deletions

4
.prettierignore Normal file
View file

@ -0,0 +1,4 @@
**/*.html
**/*.json
**/*.css
**/*.md

21
.prettierrc Normal file
View file

@ -0,0 +1,21 @@
{
"arrowParens": "avoid",
"bracketSameLine": false,
"bracketSpacing": true,
"semi": true,
"experimentalTernaries": false,
"singleQuote": false,
"jsxSingleQuote": false,
"quoteProps": "as-needed",
"trailingComma": "all",
"singleAttributePerLine": false,
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"proseWrap": "preserve",
"insertPragma": false,
"printWidth": 1000,
"requirePragma": false,
"tabWidth": 2,
"useTabs": false,
"embeddedLanguageFormatting": "auto"
}

View file

@ -27,19 +27,19 @@ ifeq ($(CROSS)$(CC), emcc)
endif
ifdef NEDITOR
CPPFLAGS += -DNO_EDITOR
CPPFLAGS += -DNO_EDITOR
endif
ifdef NFLAC
CPPFLAGS += -DNFLAC
CPPFLAGS += -DNFLAC
endif
ifdef NMP3
CPPFLAGS += -DNMP3
CPPFLAGS += -DNMP3
endif
ifdef NSVG
CPPFLAGS += -DNSVG
CPPFLAGS += -DNSVG
endif
ifdef NQOA
@ -297,4 +297,4 @@ tags: $(TAGINC)
MYFILES != (ls scripts/*.js* source/engine/*.[ch] source/engine/sound/*.[ch])
pretty:
clang-format -i $(MYFILES)
clang-format -i $(MYFILES)

View file

@ -16,5 +16,5 @@ achievement.doc = {
hidden: "True if the player shouldn't see this achievement.",
icon: "Path to an unlocked icon.",
licon: "Path to icon for not achieved.",
max: "Value needed to reach to unlock the achievement."
max: "Value needed to reach to unlock the achievement.",
};

View file

@ -5,8 +5,7 @@ var script_times = {};
var actor_spawns = {};
globalThis.class_use = function(script, config, base, callback)
{
globalThis.class_use = function (script, config, base, callback) {
var file = Resources.find_script(script);
if (!file) {
@ -31,24 +30,21 @@ globalThis.class_use = function(script, config, base, callback)
var script = Resources.replstrs(file);
script = `(function() { var self = this; var $ = this.__proto__; ${script}; })`;
var fn = os.eval(file,script);
var fn = os.eval(file, script);
fn.call(padawan);
if (typeof config === 'object')
Object.merge(padawan,config);
if (typeof config === "object") Object.merge(padawan, config);
return padawan;
}
};
globalThis.rmactor = function(e)
{
globalThis.rmactor = function (e) {
if (!actor_spawns[e._file]) return;
actor_spawns[e._file].remove(e);
}
};
actor.__stats = function()
{
actor.__stats = function () {
var total = 0;
var stats = {};
for (var i in actor_spawns) {
@ -57,10 +53,9 @@ actor.__stats = function()
}
stats.total = total;
return stats;
}
};
actor.hotreload = function()
{
actor.hotreload = function () {
profile.cache("hotreload", "check");
for (var i in script_times) {
if (io.mod(i) > script_times[i]) {
@ -72,54 +67,54 @@ actor.hotreload = function()
var $ = this.__proto__;
${script};
})`;
var fn = os.eval(i,script);
var fn = os.eval(i, script);
for (var obj of actor_spawns[i]) {
var a = obj;
for (var t of a.timers)
t();
a.timers = [];
var save = json.decode(json.encode(a));
for (var t of a.timers) t();
a.timers = [];
var save = json.decode(json.encode(a));
fn.call(a);
Object.merge(a,save);
check_registers(a);
Object.merge(a, save);
check_registers(a);
}
}
}
profile.endcache();
}
};
actor.spawn = function(script, config){
if (typeof script !== 'string') return undefined;
actor.spawn = function (script, config) {
if (typeof script !== "string") return undefined;
var padawan = class_use(script, config, actor);
padawan.padawans = [];
padawan.timers = [];
padawan.master = this;
Object.hide(padawan, "master","padawans");
padawan.toString = function() { return script; }
Object.hide(padawan, "master", "padawans");
padawan.toString = function () {
return script;
};
check_registers(padawan);
this.padawans.push(padawan);
if (padawan.awake) padawan.awake();
return padawan;
};
actor.tween = function(from,to,time,fn) {
var stop = tween(from,to,time,fn);
actor.tween = function (from, to, time, fn) {
var stop = tween(from, to, time, fn);
this.timers.push(stop);
return stop;
}
};
actor.spawn.doc = `Create a new actor, using this actor as the master, initializing it with 'script' and with data (as a JSON or Nota file) from 'config'.`;
actor.rm_pawn = function(pawn)
{
actor.rm_pawn = function (pawn) {
this.padawans.remove(pawn);
}
};
actor.timers = [];
actor.kill = function(){
actor.kill = function () {
if (this.__dead__) return;
this.timers.forEach(t => t());
input.do_uncontrol(this);
@ -129,62 +124,65 @@ actor.kill = function(){
this.padawans = [];
this.__dead__ = true;
actor_spawns[this._file].remove(this);
if (typeof this.die === 'function') this.die();
if (typeof this.stop === 'function') this.stop();
if (typeof this.garbage === 'function') this.garbage();
if (typeof this.then === 'function') this.then();
if (typeof this.die === "function") this.die();
if (typeof this.stop === "function") this.stop();
if (typeof this.garbage === "function") this.garbage();
if (typeof this.then === "function") this.then();
};
actor.kill.doc = `Remove this actor and all its padawans from existence.`;
actor.delay = function(fn, seconds) {
actor.delay = function (fn, seconds) {
var timers = this.timers;
var stop = function() {
var stop = function () {
timers.remove(stop);
stop = undefined;
rm();
}
};
function execute() {
if (fn) fn();
if (stop && stop.then) stop.then();
stop();
}
stop.remain = seconds;
stop.seconds = seconds;
stop.pct = function() { return 1-(stop.remain / stop.seconds); };
stop.pct = function () {
return 1 - stop.remain / stop.seconds;
};
function update(dt) {
profile.frame("timer");
stop.remain -= dt;
if (stop.remain <= 0) execute();
profile.endframe();
}
var rm = Register.appupdate.register(update);
timers.push(stop);
return stop;
};
actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`;
actor.interval = function(fn, seconds)
{
actor.interval = function (fn, seconds) {
var self = this;
var stop;
var usefn = function() {
var usefn = function () {
fn();
stop = self.delay(usefn, seconds);
}
};
stop = self.delay(usefn, seconds);
return stop;
}
};
actor.padawans = [];
global.app = Object.create(actor);
app.die = function() { os.quit(); }
app.die = function () {
os.quit();
};
return {actor, app};
return { actor, app };

View file

@ -1,58 +1,55 @@
var ai = {
race(list) {
return function(dt) {
return function (dt) {
var good = false;
for (var i = 0; i < list.length; i++)
if (list[i].call(this,dt)) good=true;
for (var i = 0; i < list.length; i++) if (list[i].call(this, dt)) good = true;
return good;
};
},
sequence(list) {
var i = 0;
var fn = function(dt) {
var fn = function (dt) {
while (i !== list.length) {
if (list[i].call(this,dt))
i++;
else
return false;
if (list[i].call(this, dt)) i++;
else return false;
}
if (fn.done) fn.done();
return true;
};
fn.restart = function() { i = 0; };
fn.restart = function () {
i = 0;
};
return fn;
},
parallel(list) {
return function(dt) {
return function (dt) {
var good = true;
list.forEach(function(x){ if (!x.call(this,dt)) good = false; },this);
list.forEach(function (x) {
if (!x.call(this, dt)) good = false;
}, this);
return good;
};
},
dofor(secs, fn) {
return ai.race([
ai.wait(secs),
fn
]);
return ai.race([ai.wait(secs), fn]);
},
wait(secs = 1) {
var accum = 0;
return function(dt) {
return function (dt) {
accum += dt;
if (accum >= secs) {
accum = 0;
return true;
}
return false;
};
},
};
return {ai};
return { ai };

File diff suppressed because it is too large Load diff

View file

@ -1,43 +1,48 @@
this.phys = physics.kinematic;
this.dir_view2world = function(dir) { return dir.scale(this.zoom); };
this.view2world = function(pos) {
this.dir_view2world = function (dir) {
return dir.scale(this.zoom);
};
this.view2world = function (pos) {
var useren = window.rendersize.scale(this.zoom);
if (window.mode === window.modetypes.stretch) {
pos = pos.scale([window.rendersize.x/window.size.x, window.rendersize.y/window.size.y]);
pos = pos.scale([window.rendersize.x / window.size.x, window.rendersize.y / window.size.y]);
pos = pos.sub(window.rendersize.scale(0.5));
pos = pos.scale(this.zoom);
pos = pos.add(this.pos);
}
if (window.mode === window.modetypes.full) {
pos = pos.sub(window.size.scale(0.5));
pos = pos.scale(this.zoom);
pos = pos.scale(this.zoom);
pos = pos.add(this.pos);
}
if (window.mode === window.modetypes.expand) {
pos = pos.sub(window.size.scale(0.5));
pos = pos.scale([window.rendersize.x/window.size.x, window.rendersize.y/window.size.y]);
pos = pos.sub(window.size.scale(0.5));
pos = pos.scale([window.rendersize.x / window.size.x, window.rendersize.y / window.size.y]);
}
return pos;
};
this.world2view = function(pos) {
this.world2view = function (pos) {
if (window.mode === window.modetypes.stretch) {
pos = pos.sub(this.pos);
pos = pos.scale(1.0/this.zoom);
pos = pos.scale(1.0 / this.zoom);
pos = pos.add(window.rendersize.scale(0.5));
}
if (window.mode === window.modetypes.full) {
pos = pos.sub(this.pos);
pos = pos.scale(1/this.zoom);
pos = pos.scale(1 / this.zoom);
pos = pos.add(window.size.scale(0.5));
}
if (window.mode === window.modetypes.expand) {
}
return pos;
};
this.screenright = function() { return this.view2world(window.size).x; }
this.screenleft = function() { return this.view2world([0,0]).x; }
this.screenright = function () {
return this.view2world(window.size).x;
};
this.screenleft = function () {
return this.view2world([0, 0]).x;
};
var zoom = 1;
@ -46,6 +51,7 @@ Object.mixin(self, {
zoom = x;
if (zoom <= 0.1) zoom = 0.1;
},
get zoom() { return zoom; }
}
);
get zoom() {
return zoom;
},
});

View file

@ -1,113 +1,118 @@
var Color = {
white: [255,255,255],
black: [0,0,0],
blue: [0,0,255],
green: [0,255,0],
yellow: [255,255,0],
red: [255,0,0],
gray: [181,181,181],
cyan: [0,255,255],
purple: [162,93,227],
orange: [255,144,64],
magenta: [255,0,255],
white: [255, 255, 255],
black: [0, 0, 0],
blue: [0, 0, 255],
green: [0, 255, 0],
yellow: [255, 255, 0],
red: [255, 0, 0],
gray: [181, 181, 181],
cyan: [0, 255, 255],
purple: [162, 93, 227],
orange: [255, 144, 64],
magenta: [255, 0, 255],
};
Color.editor = {};
Color.editor.ur = Color.green;
Color.tohtml = function(v)
{
var html = v.map(function(n) { return Number.hex(n*255); });
return "#" + html.join('');
}
Color.tohtml = function (v) {
var html = v.map(function (n) {
return Number.hex(n * 255);
});
return "#" + html.join("");
};
var esc = {};
esc.reset = "\x1b[0";
esc.color = function(v) {
var c = v.map(function(n) { return Math.floor(n*255); });
var truecolor = "\x1b[38;2;" + c.join(';') + ';';
esc.color = function (v) {
var c = v.map(function (n) {
return Math.floor(n * 255);
});
var truecolor = "\x1b[38;2;" + c.join(";") + ";";
return truecolor;
}
};
esc.doc = "Functions and constants for ANSI escape sequences.";
Color.Arkanoid = {
orange: [255,143,0],
teal: [0,255,255],
green: [0,255,0],
red: [255,0,0],
blue: [0,112,255],
purple: [255,0,255],
yellow: [255,255,0],
silver: [157,157,157],
gold: [188,174,0],
orange: [255, 143, 0],
teal: [0, 255, 255],
green: [0, 255, 0],
red: [255, 0, 0],
blue: [0, 112, 255],
purple: [255, 0, 255],
yellow: [255, 255, 0],
silver: [157, 157, 157],
gold: [188, 174, 0],
};
Color.Arkanoid.Powerups = {
red: [174,0,0], /* laser */
blue: [0,0,174], /* enlarge */
green: [0,174,0], /* catch */
orange: [224,143,0], /* slow */
purple: [210,0,210], /* break */
cyan: [0,174,255], /* disruption */
gray: [143,143,143] /* 1up */
red: [174, 0, 0] /* laser */,
blue: [0, 0, 174] /* enlarge */,
green: [0, 174, 0] /* catch */,
orange: [224, 143, 0] /* slow */,
purple: [210, 0, 210] /* break */,
cyan: [0, 174, 255] /* disruption */,
gray: [143, 143, 143] /* 1up */,
};
Color.Gameboy = {
darkest: [229,107,26],
dark: [229,189,26],
light: [189,229,26],
lightest: [107,229,26],
darkest: [229, 107, 26],
dark: [229, 189, 26],
light: [189, 229, 26],
lightest: [107, 229, 26],
};
Color.Apple = {
green: [94,189,62],
yellow: [255,185,0],
orange: [247,130,0],
red: [226,56,56],
purple: [151,57,153],
blue: [0,156,223]
green: [94, 189, 62],
yellow: [255, 185, 0],
orange: [247, 130, 0],
red: [226, 56, 56],
purple: [151, 57, 153],
blue: [0, 156, 223],
};
Color.Debug = {
boundingbox: Color.white,
names: [84,110,255],
names: [84, 110, 255],
};
Color.Editor = {
grid: [99,255,128],
select: [255,255,55],
newgroup: [120,255,10],
grid: [99, 255, 128],
select: [255, 255, 55],
newgroup: [120, 255, 10],
};
/* Detects the format of all colors and munges them into a floating point format */
Color.normalize = function(c) {
var add_a = function(a) {
Color.normalize = function (c) {
var add_a = function (a) {
var n = this.slice();
n.a = a;
return n;
};
for (var p of Object.keys(c)) {
var fmt = "nrm";
if (typeof c[p] !== 'object') continue;
if (typeof c[p] !== "object") continue;
if (!Array.isArray(c[p])) {
Color.normalize(c[p]);
continue;
}
c[p][3] = 255;
for (var color of c[p]) {
if (color > 1) {
fmt = "8b";
break;
break;
}
}
switch(fmt) {
switch (fmt) {
case "8b":
c[p] = c[p].map(function(x) { return x/255; });
c[p] = c[p].map(function (x) {
return x / 255;
});
}
c[p].alpha = add_a;
}
@ -118,66 +123,64 @@ Color.normalize(Color);
Object.deepfreeze(Color);
var ColorMap = {};
ColorMap.makemap = function(map)
{
ColorMap.makemap = function (map) {
var newmap = Object.create(ColorMap);
Object.assign(newmap, map);
return newmap;
}
};
ColorMap.Jet = ColorMap.makemap({
0: [0,0,131],
0.125: [0,60,170],
0.375: [5,255,255],
0.625: [255,255,0],
0.875: [250,0,0],
1: [128,0,0]
0: [0, 0, 131],
0.125: [0, 60, 170],
0.375: [5, 255, 255],
0.625: [255, 255, 0],
0.875: [250, 0, 0],
1: [128, 0, 0],
});
ColorMap.BlueRed = ColorMap.makemap({
0: [0,0,255],
1: [255,0,0]
0: [0, 0, 255],
1: [255, 0, 0],
});
ColorMap.Inferno = ColorMap.makemap({
0:[0,0,4],
0.13: [31,12,72],
0.25: [85,15,109],
0.38: [136,34,106],
0.5: [186,54,85],
0.63: [227,89,51],
0.75: [249,140,10],
0.88: [249,201,50],
1: [252,255,164]
0: [0, 0, 4],
0.13: [31, 12, 72],
0.25: [85, 15, 109],
0.38: [136, 34, 106],
0.5: [186, 54, 85],
0.63: [227, 89, 51],
0.75: [249, 140, 10],
0.88: [249, 201, 50],
1: [252, 255, 164],
});
ColorMap.Bathymetry = ColorMap.makemap({
0: [40,26,44],
0.13: [59.49,90],
0.25: [64,76,139],
0.38: [63,110,151],
0.5: [72,142,158],
0.63: [85,174,163],
0.75: [120,206,163],
0.88: [187,230,172],
1: [253,254,204]
0: [40, 26, 44],
0.13: [59.49, 90],
0.25: [64, 76, 139],
0.38: [63, 110, 151],
0.5: [72, 142, 158],
0.63: [85, 174, 163],
0.75: [120, 206, 163],
0.88: [187, 230, 172],
1: [253, 254, 204],
});
ColorMap.Viridis = ColorMap.makemap({
0: [68,1,84],
0.13: [71,44,122],
0.25: [59,81,139],
0.38: [44,113,142],
0.5: [33,144,141],
0.63: [39,173,129],
0.75: [92,200,99],
0.88: [170,220,50],
1: [253,231,37]
0: [68, 1, 84],
0.13: [71, 44, 122],
0.25: [59, 81, 139],
0.38: [44, 113, 142],
0.5: [33, 144, 141],
0.63: [39, 173, 129],
0.75: [92, 200, 99],
0.88: [170, 220, 50],
1: [253, 231, 37],
});
Color.normalize(ColorMap);
ColorMap.sample = function(t, map = this)
{
ColorMap.sample = function (t, map = this) {
if (t < 0) return map[0];
if (t > 1) return map[1];
@ -192,7 +195,7 @@ ColorMap.sample = function(t, map = this)
lastkey = key;
}
return map[1];
}
};
ColorMap.doc = {
sample: "Sample a given colormap at the given percentage (0 to 1).",
@ -203,5 +206,5 @@ Object.freeze(ColorMap);
return {
Color,
esc,
ColorMap
}
ColorMap,
};

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,7 @@
debug.build = function(fn) { if (!debug.show) return; fn(); }
debug.build = function (fn) {
if (!debug.show) return;
fn();
};
debug.show = true;
debug.urnames = false;
debug.termout = true;
@ -7,64 +10,62 @@ debug.cheat = false;
debug.meta = false;
debug.showprofiler = false;
debug.fn_break = function(fn,obj = globalThis) {
if (typeof fn !== 'function') return;
var newfn = function() {
debug.fn_break = function (fn, obj = globalThis) {
if (typeof fn !== "function") return;
var newfn = function () {
console.log("broke");
fn();
};
obj[fn.name] = newfn;
}
obj[fn.name] = newfn;
};
debug.draw_phys = false;
debug.draw_bb = false;
debug.draw_gizmos = false;
debug.draw_names = false;
debug.sprite_nums = false;
debug.draw = function() {
if (this.draw_phys) game.all_objects(function(x) { debug.draw_gameobject(x); });
debug.draw = function () {
if (this.draw_phys)
game.all_objects(function (x) {
debug.draw_gameobject(x);
});
if (this.draw_bb)
game.all_objects(function(x) { debug.boundingbox(x.boundingbox(), Color.debug.boundingbox.alpha(0.05)); });
game.all_objects(function (x) {
debug.boundingbox(x.boundingbox(), Color.debug.boundingbox.alpha(0.05));
});
if (this.draw_gizmos)
game.all_objects(function(x) {
game.all_objects(function (x) {
if (!x.icon) return;
gui.image(x.icon, game.camera.world2view(x.pos));
});
if (this.draw_names)
game.all_objects(function(x) {
render.text(x, game.camera.view2screen(x.pos).add([0,32]), 1, Color.debug.names);
game.all_objects(function (x) {
render.text(x, game.camera.view2screen(x.pos).add([0, 32]), 1, Color.debug.names);
});
if (debug.gif.rec) {
render.text("REC", [0,40], 1);
render.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0,30], 1);
render.text("REC", [0, 40], 1);
render.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0, 30], 1);
}
return;
if (sim.paused()) render.text("PAUSED", [0,0],1);
render.text(sim.playing() ? "PLAYING"
: sim.stepping() ?
"STEP" :
sim.paused() ?
"PAUSED; EDITING" :
"EDIT", [0, 0], 1);
}
if (sim.paused()) render.text("PAUSED", [0, 0], 1);
var assert = function(op, str = `assertion failed [value '${op}']`)
{
if (!op)
console.panic(str);
}
render.text(sim.playing() ? "PLAYING" : sim.stepping() ? "STEP" : sim.paused() ? "PAUSED; EDITING" : "EDIT", [0, 0], 1);
};
var assert = function (op, str = `assertion failed [value '${op}']`) {
if (!op) console.panic(str);
};
var Gizmos = {
pick_gameobject_points(worldpos, gameobject, points) {
var idx = Math.grab_from_points(worldpos, points.map(gameobject.this2world,gameobject), 25);
var idx = Math.grab_from_points(worldpos, points.map(gameobject.this2world, gameobject), 25);
if (idx === -1) return undefined;
return idx;
},
@ -72,20 +73,24 @@ var Gizmos = {
/* These controls are available during editing, and during play of debug builds */
debug.inputs = {};
debug.inputs.f1 = function () { debug.draw_phys = !debug.draw_phys; };
debug.inputs.f1 = function () {
debug.draw_phys = !debug.draw_phys;
};
debug.inputs.f1.doc = "Draw physics debugging aids.";
debug.inputs.f3 = function() { debug.draw_bb = !debug.draw_bb; };
debug.inputs.f3 = function () {
debug.draw_bb = !debug.draw_bb;
};
debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
debug.inputs.f4 = function() {
debug.inputs.f4 = function () {
debug.draw_names = !debug.draw_names;
debug.draw_gizmos = !debug.draw_gizmos;
};
debug.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
var gif = {
w: 640, /* Max width */
h: 480, /* Max height */
stretch: false, /* True if you want to stretch */
w: 640 /* Max width */,
h: 480 /* Max height */,
stretch: false /* True if you want to stretch */,
cpf: 4,
depth: 16,
file: "out.gif",
@ -97,17 +102,15 @@ var gif = {
var w = this.w;
var h = this.h;
if (!this.stretch) {
var win = window.height / window.width;
var gif = h/w;
if (gif > win)
h = w * win;
else
w = h / win;
var win = window.height / window.width;
var gif = h / w;
if (gif > win) h = w * win;
else w = h / win;
}
// cmd(131, w, h, this.cpf, this.depth);
// cmd(131, w, h, this.cpf, this.depth);
this.rec = true;
this.fps = (1/this.cpf)*100;
this.fps = (1 / this.cpf) * 100;
this.start_time = time.now();
timer.oneshot(this.stop.bind(this), this.secs, this, true);
@ -115,54 +118,59 @@ var gif = {
stop() {
if (!this.rec) return;
// cmd(132, this.file);
// cmd(132, this.file);
this.rec = false;
},
};
debug.inputs.f8 = function() {
debug.inputs.f8 = function () {
var now = new Date();
debug.gif.file = now.toISOString() + ".gif";
debug.gif.start();
};
debug.inputs.f9 = function() {
debug.inputs.f9 = function () {
debug.gif.stop();
}
};
debug.inputs.f10 = function() { time.timescale = 0.1; };
debug.inputs.f10 = function () {
time.timescale = 0.1;
};
debug.inputs.f10.doc = "Toggle timescale to 1/10.";
debug.inputs.f10.released = function () { time.timescale = 1.0; };
debug.inputs.f12 = function() { gui.defaults.debug = !gui.defaults.debug; console.warn("gui toggle debug");};
debug.inputs.f10.released = function () {
time.timescale = 1.0;
};
debug.inputs.f12 = function () {
gui.defaults.debug = !gui.defaults.debug;
console.warn("gui toggle debug");
};
debug.inputs.f12.doc = "Toggle drawing gui debugging aids.";
debug.inputs['M-1'] = render.normal;
debug.inputs['M-2'] = render.wireframe;
debug.inputs['C-M-f'] = function() {};
debug.inputs['C-M-f'].doc = "Enter camera fly mode.";
debug.inputs["M-1"] = render.normal;
debug.inputs["M-2"] = render.wireframe;
debug.inputs["C-M-f"] = function () {};
debug.inputs["C-M-f"].doc = "Enter camera fly mode.";
debug.api = {};
debug.api.doc_entry = function(obj, key)
{
if (typeof key !== 'string') {
debug.api.doc_entry = function (obj, key) {
if (typeof key !== "string") {
console.warn("Cannot print a key that isn't a string.");
return undefined;
}
var title = key;
var o = obj[key];
if (typeof o === 'undefined' && obj.impl && typeof obj.impl[key] !== 'undefined')
o = obj.impl[key];
if (typeof o === "undefined" && obj.impl && typeof obj.impl[key] !== "undefined") o = obj.impl[key];
var t = typeof o;
if (Array.isArray(o)) t = "array";
else if (t === 'function') {
title = o.toString().tofirst(')') + ")";
title = title.fromfirst('(');
else if (t === "function") {
title = o.toString().tofirst(")") + ")";
title = title.fromfirst("(");
title = key + "(" + title;
if (o.doc) doc = o.doc;
t = "";
} else if (t === 'undefined') t = "";
} else if (t === "undefined") t = "";
if (t) t = "**" + t + "**\n";
@ -175,12 +183,11 @@ debug.api.doc_entry = function(obj, key)
${t}
${doc}
`;
}
};
debug.api.print_doc = function(name)
{
debug.api.print_doc = function (name) {
var obj = name;
if (typeof name === 'string') {
if (typeof name === "string") {
obj = eval(name);
if (!obj) {
console.warn(`Cannot print the API of '${name}', as it was not found.`);
@ -189,8 +196,8 @@ debug.api.print_doc = function(name)
obj = globalThis[name];
}
obj = eval(name);
obj = eval(name);
if (!Object.isObject(obj)) {
console.warn("Cannot print the API of something that isn't an object.");
@ -201,24 +208,24 @@ debug.api.print_doc = function(name)
console.warn(`Object '${name}' does not exist.`);
return;
}
var mdoc = "# " + name + "\n";
if (obj.doc?.doc) mdoc += obj.doc.doc + "\n";
else if (typeof obj.doc === 'string') mdoc += obj.doc + "\n";
else if (typeof obj.doc === "string") mdoc += obj.doc + "\n";
var keys = Object.keys(obj);
for (var key of keys) {
if (key === 'doc') continue;
if (key === 'toString') continue;
if (key === "doc") continue;
if (key === "toString") continue;
mdoc += debug.api.doc_entry(obj, key) + "\n";
}
return mdoc;
}
};
return {
debug,
Gizmos,
assert
}
assert,
};

View file

@ -1,98 +1,90 @@
function deep_copy(from) { return json.decode(json.encode(from)); }
function deep_copy(from) {
return json.decode(json.encode(from));
}
function valdiff(from,to)
{
function valdiff(from, to) {
if (typeof from !== typeof to) return from;
if (typeof from === 'function') return undefined;
if (typeof from === 'undefined') return undefined;
if (typeof from === "function") return undefined;
if (typeof from === "undefined") return undefined;
if (typeof from === "number") {
return to;
if (typeof from === 'number') {
return to;
return undefined;
}
if (typeof from === 'object')
return ediff(from,to);
if (typeof from === "object") return ediff(from, to);
if (from !== to) return to;
return undefined;
}
function ediff(from,to)
{
function ediff(from, to) {
var ret = {};
if (!to)
// return ediff(from, {});
return deep_copy(from);
Object.entries(from).forEach(function([key,v]) {
if (typeof v === 'function') return;
if (typeof v === 'undefined') return;
if (!to)
// return ediff(from, {});
return deep_copy(from);
Object.entries(from).forEach(function ([key, v]) {
if (typeof v === "function") return;
if (typeof v === "undefined") return;
if (Array.isArray(v)) {
if (!Array.isArray(to[key]) || v.length !== to[key].length) {
var r = ediff(v,[]);
var r = ediff(v, []);
if (r) ret[key] = Object.values(r);
return;
}
var diff = ediff(from[key], to[key]);
if (diff && !Object.empty(diff))
ret[key] = Object.values(ediff(v,[]));
if (diff && !Object.empty(diff)) ret[key] = Object.values(ediff(v, []));
return;
}
if (typeof v === 'object' && v !== null) {
if (typeof v === "object" && v !== null) {
var diff = ediff(v, to[key]);
if (diff && !Object.empty(diff))
ret[key] = diff;
if (diff && !Object.empty(diff)) ret[key] = diff;
return;
}
if (typeof v === 'number' || v === null) {
if (typeof v === "number" || v === null) {
if (!isFinite(v)) v = null; // Squash infinity to null
if (v !== to[key])
ret[key] = v;
if (v !== to[key]) ret[key] = v;
return;
}
if (!to || v !== to[key])
ret[key] = v;
if (!to || v !== to[key]) ret[key] = v;
});
if (Object.empty(ret)) return undefined;
return ret;
}
ediff.doc = "Given a from and to object, returns an object that, if applied to from, will make it the same as to. Does not include deletion; it is only additive. If one element in an array is different, the entire array is copied. Squashes infinite numbers to null for use in JSON.";
function samediff(from, to)
{
function samediff(from, to) {
var same = [];
if (!to) return same;
if (typeof to !== 'object') {
if (typeof to !== "object") {
console.warn("'To' must be an object. Got " + to);
return same;
}
Object.keys(from).forEach(function(k) {
Object.keys(from).forEach(function (k) {
if (Object.isObject(from[k])) {
samediff(from[k], to[k]);
return;
}
// if (Array.isArray(from[k])) {
// var d = valdiff(from[k], to[k]);
// if (!d)
// }
// if (Array.isArray(from[k])) {
// var d = valdiff(from[k], to[k]);
// if (!d)
// }
var d = valdiff(from[k], to[k]);
if (!d)
delete from[k];
if (!d) delete from[k];
});
return same;
@ -104,4 +96,4 @@ return {
deep_copy,
ediff,
samediff,
}
};

File diff suppressed because it is too large Load diff

View file

@ -4,8 +4,10 @@ os.mem_limit.doc = "Set the memory limit of the runtime in bytes.";
os.gc_threshold.doc = "Set the threshold before a GC pass is triggered in bytes. This is set to malloc_size + malloc_size>>1 after a GC pass.";
os.max_stacksize.doc = "Set the max stack size in bytes.";
Object.defineProperty(String.prototype, 'rm', {
value: function(index, endidx = index+1) { return this.slice(0,index) + this.slice(endidx); }
Object.defineProperty(String.prototype, "rm", {
value: function (index, endidx = index + 1) {
return this.slice(0, index) + this.slice(endidx);
},
});
Object.defineProperty(String.prototype, "tolast", {
@ -33,23 +35,22 @@ Object.defineProperty(String.prototype, "folder", {
globalThis.Resources = {};
Resources.rm_fn = function rm_fn(fn, text)
{
Resources.rm_fn = function rm_fn(fn, text) {
var reg = new RegExp(fn.source + "\\s*\\(");
var match;
while (match = text.match(reg)) {
var last = match.index+match[0].length;
while ((match = text.match(reg))) {
var last = match.index + match[0].length;
var par = 1;
while (par !== 0) {
if (text[last] === '(') par++;
if (text[last] === ')') par--;
if (text[last] === "(") par++;
if (text[last] === ")") par--;
last++;
}
text = text.rm(match.index, last);
}
return text;
}
};
Resources.rm_fn.doc = "Remove calls to a given function from a given text script.";
Resources.replpath = function replpath(str, path) {
@ -76,17 +77,15 @@ Resources.replstrs = function replstrs(path) {
var stem = path.dir();
if (!console.enabled)
script = Resources.rm_fn(/console\.(spam|info|warn|error)/, script);
if (!profile.enabled)
script = Resources.rm_fn(/profile\.(cache|frame|endcache|endframe)/, script);
if (!console.enabled) script = Resources.rm_fn(/console\.(spam|info|warn|error)/, script);
if (!profile.enabled) script = Resources.rm_fn(/profile\.(cache|frame|endcache|endframe)/, script);
if (!debug.enabled) {
script = Resources.rm_fn(/assert/, script);
script = Resources.rm_fn(/debug\.(build|fn_break)/, script);
}
script = script.replace(regexp, function (str) {
var newstr = Resources.replpath(str.trimchr('"'), path);
return `"${newstr}"`;
@ -95,23 +94,21 @@ Resources.replstrs = function replstrs(path) {
return script;
};
Resources.is_sound = function(path) {
Resources.is_sound = function (path) {
var ext = path.ext();
return Resources.sounds.any(x => x === ext);
}
};
Resources.is_animation = function (path) {
if (path.ext() === "gif" && Resources.gif.frames(path) > 1) return true;
if (path.ext() === "ase") return true;
Resources.is_animation = function(path)
{
if (path.ext() === 'gif' && Resources.gif.frames(path) > 1) return true;
if (path.ext() === 'ase') return true;
return false;
}
};
Resources.is_path = function(str)
{
Resources.is_path = function (str) {
return !/[\\\/:*?"<>|]/.test(str);
}
};
globalThis.json = {};
json.encode = function (value, replacer, space = 1) {
@ -154,16 +151,14 @@ function find_ext(file, ext, root = "") {
if (ext.some(x => x === file_ext)) return file;
for (var e of ext) {
var nf = `${file}.${e}`;
if (io.exists(nf))
return nf;
if (io.exists(nf)) return nf;
}
var all_files = io.glob(`**/${file}.*`);
var find = undefined;
for (var e of ext) {
var finds = all_files.filter(x => x.ext() === e);
if (finds.length > 1)
console.warn(`Found conflicting files when searching for '${file}': ${json.encode(finds)}. Returning the first one.`);
if (finds.length > 1) console.warn(`Found conflicting files when searching for '${file}': ${json.encode(finds)}. Returning the first one.`);
if (finds.length > 0) {
find = finds[0];
break;
@ -176,18 +171,17 @@ function find_ext(file, ext, root = "") {
var hashhit = 0;
var hashmiss = 0;
Object.defineProperty(Function.prototype, 'hashify', {
value: function() {
Object.defineProperty(Function.prototype, "hashify", {
value: function () {
var hash = new Map();
var fn = this;
function ret() {
if (!hash.has(arguments[0]))
hash.set(arguments[0], fn(...arguments));
if (!hash.has(arguments[0])) hash.set(arguments[0], fn(...arguments));
return hash.get(arguments[0]);
}
return ret;
}
},
});
Resources.find_image = function (file, root = "") {
@ -258,7 +252,7 @@ console.stackstr = function (skip = 0) {
};
console.stack = function (skip = 0) {
var stack = console.stackstr(skip+1);
var stack = console.stackstr(skip + 1);
console.log(stack);
return stack;
};
@ -301,9 +295,9 @@ globalThis.use = function use(file) {
profile.endcache();
return ret;
}
};
function stripped_use (file, script) {
function stripped_use(file, script) {
file = Resources.find_script(file);
if (use_cache[file]) {
@ -316,12 +310,11 @@ function stripped_use (file, script) {
var fn = os.eval(file, script);
var ret = fn();
profile.endcache();
return ret;
}
function bare_use(file)
{
function bare_use(file) {
var script = io.slurp(file);
if (!script) return;
script = `(function() { var self = this; ${script}; })`;
@ -337,17 +330,15 @@ debug.enabled = true;
bare_use("scripts/base.js");
bare_use("scripts/profile.js");
prosperon.release = function()
{
prosperon.release = function () {
profile.enabled = false;
console.enabled = false;
debug.enabled = false;
}
};
bare_use("preconfig.js");
if (!profile.enabled)
use = stripped_use;
if (!profile.enabled) use = stripped_use;
Object.assign(globalThis, use("scripts/prosperon.js"));
@ -359,4 +350,3 @@ app.interval(_ => {
repl.hotreload();
profile.endframe();
}, 1);

View file

@ -1,12 +1,11 @@
globalThis.entityreport = {};
var timer_update = function(dt)
{
var timer_update = function (dt) {
this.fn();
}
};
function obj_unique_name(name, obj) {
name = name.replaceAll('.', '_');
name = name.replaceAll(".", "_");
if (!(name in obj)) return name;
var t = 1;
var n = name + t;
@ -18,7 +17,7 @@ function obj_unique_name(name, obj) {
}
function unique_name(list, name = "new_object") {
var str = name.replaceAll('.', '_');
var str = name.replaceAll(".", "_");
var n = 1;
var t = str;
while (list.indexOf(t) !== -1) {
@ -26,23 +25,22 @@ function unique_name(list, name = "new_object") {
n++;
}
return t;
};
}
var entity = {
drawlayer: -1,
get_comp_by_name(name) {
var comps = [];
for (var c of Object.values(this.components))
if (c.comp === name) comps.push(c);
for (var c of Object.values(this.components)) if (c.comp === name) comps.push(c);
if (comps.length) return comps;
return undefined;
},
rigidify() {
this.body = os.make_body(this.transform);
},
path_from(o) {
var p = this.toString();
var c = this.master;
@ -53,15 +51,17 @@ var entity = {
if (c === world) p = "world." + p;
return p;
},
drawlayer: 0,
full_path() { return this.path_from(world); },
full_path() {
return this.path_from(world);
},
clear() {
for (var k in this.objects) {
this.objects[k].kill();
};
}
this.objects = {};
},
@ -72,54 +72,75 @@ var entity = {
delay(fn, seconds) {
var timers = this.timers;
var stop = function() {
var stop = function () {
timers.remove(stop);
execute = undefined;
stop = undefined;
rm?.();
rm = undefined;
update = undefined;
}
};
function execute() {
fn();
stop?.();
}
stop.remain = seconds;
stop.seconds = seconds;
stop.pct = function() { return 1 - (stop.remain/stop.seconds); };
stop.pct = function () {
return 1 - stop.remain / stop.seconds;
};
function update(dt) {
profile.frame("timer");
if (stop) { // TODO: This seems broken
stop.remain -= dt;
if (stop.remain <= 0) execute();
if (stop) {
// TODO: This seems broken
stop.remain -= dt;
if (stop.remain <= 0) execute();
}
profile.endframe();
}
var rm = Register.update.register(update);
timers.push(stop);
return stop;
},
cry(file) { return audio.cry(file); },
get pos() { return this.transform.pos; },
set pos(x) { this.transform.pos = x; },
get angle() { return this.transform.angle; },
set angle(x) { this.transform.angle = x; },
get scale() { return this.transform.scale; },
set scale(x) { this.transform.scale = x; },
move(vec) { this.pos = this.pos.add(vec); },
rotate(x) { this.transform.rotate(x, [0,0,-1]); },
grow(vec) {
if (typeof vec === 'number') vec = [vec,vec];
this.scale = this.scale.map((x,i) => x*vec[i]);
cry(file) {
return audio.cry(file);
},
get pos() {
return this.transform.pos;
},
set pos(x) {
this.transform.pos = x;
},
get angle() {
return this.transform.angle;
},
set angle(x) {
this.transform.angle = x;
},
get scale() {
return this.transform.scale;
},
set scale(x) {
this.transform.scale = x;
},
move(vec) {
this.pos = this.pos.add(vec);
},
rotate(x) {
this.transform.rotate(x, [0, 0, -1]);
},
grow(vec) {
if (typeof vec === "number") vec = [vec, vec];
this.scale = this.scale.map((x, i) => x * vec[i]);
},
/* Reparent 'this' to be 'parent's child */
reparent(parent) {
assert(parent, `Tried to reparent ${this.toString()} to nothing.`);
@ -127,28 +148,26 @@ var entity = {
console.warn(`not reparenting ... ${this.master} is the same as ${parent}`);
return;
}
var name = unique_name(Object.keys(parent), this.name);
this.name = name;
this.master?.remove_obj(this);
this.master = parent;
parent.objects[this.guid] = this;
parent[name] = this;
Object.hide(parent, name);
},
remove_obj(obj) {
if (this.objects)
delete this.objects[obj.guid];
else
console.warn(`Object ${this.guid} has no objects file.`);
if (this.objects) delete this.objects[obj.guid];
else console.warn(`Object ${this.guid} has no objects file.`);
delete this[obj.name];
Object.unhide(this, obj.name);
},
spawn(text, config, callback) {
var ent = class_use(text, config, entity, function(ent) {
var ent = class_use(text, config, entity, function (ent) {
ent.transform = os.make_transform();
ent.guid = prosperon.guid();
ent.components = {};
@ -157,7 +176,7 @@ var entity = {
ent.ur = {};
ent.urname = text;
});
/*
/*
if (!text)
ent.ur = emptyur;
else if (text instanceof Object) {// assume it's an ur
@ -192,15 +211,15 @@ var entity = {
profile.cache("ENTITY TIME", ent.ur.name);
*/
ent.reparent(this);
for (var [prop, p] of Object.entries(ent)) {
if (!p) continue;
if (typeof p !== 'object') continue;
if (typeof p !== "object") continue;
if (!p.comp) continue;
ent[prop] = component[p.comp](ent);
Object.merge(ent[prop], p);
ent.components[prop] = ent[prop];
};
}
check_registers(ent);
@ -209,20 +228,20 @@ var entity = {
ent._started = true;
if (ent.start instanceof Function) ent.start();
}
Object.hide(ent, 'ur', 'components', 'objects', 'timers', 'guid', 'master', 'guid');
Object.hide(ent, "ur", "components", "objects", "timers", "guid", "master", "guid");
ent._ed = {
selectable: true,
dirty: false,
inst: false,
urdiff: {}
urdiff: {},
};
Object.hide(ent, '_ed');
Object.hide(ent, "_ed");
ent.sync();
if (!Object.empty(ent.objects)) {
var o = ent.objects;
delete ent.objects;
@ -235,46 +254,63 @@ var entity = {
ent.rename_obj(n.toString(), i);
}
}
if (ent.tag) game.tag_add(ent.tag, ent);
if (callback) callback(ent);
ent.ur.fresh ??= json.decode(json.encode(ent));
ent.ur.fresh.objects = {};
for (var i in ent.objects)
ent.ur.fresh.objects[i] = ent.objects[i].instance_obj();
for (var i in ent.objects) ent.ur.fresh.objects[i] = ent.objects[i].instance_obj();
profile.endcache();
return ent;
},
disable() { for (var x of this.components) x.disable(); },
enable() { for (var x of this.components) x.enable(); },
this2screen(pos) { return game.camera.world2view(this.this2world(pos)); },
screen2this(pos) { return this.world2this(game.camera.view2world(pos)); },
disable() {
for (var x of this.components) x.disable();
},
enable() {
for (var x of this.components) x.enable();
},
this2screen(pos) {
return game.camera.world2view(this.this2world(pos));
},
screen2this(pos) {
return this.world2this(game.camera.view2world(pos));
},
/* Make a unique object the same as its prototype */
revert() { Object.merge(this, this.ur.fresh); },
revert() {
Object.merge(this, this.ur.fresh);
},
name: "new_object",
toString() { return this.name; },
toString() {
return this.name;
},
width() {
var bb = this.boundingbox();
return bb.r - bb.l;
},
height() {
var bb = this.boundingbox();
return bb.t - bb.b;
},
flipx() { return this.scale.x < 0; },
flipy() { return this.scale.y < 0; },
mirror(plane) { this.scale = Vector.reflect(this.scale, plane); },
flipx() {
return this.scale.x < 0;
},
flipy() {
return this.scale.y < 0;
},
mirror(plane) {
this.scale = Vector.reflect(this.scale, plane);
},
/* Bounding box of the object in world dimensions */
boundingbox() {
@ -283,46 +319,42 @@ var entity = {
t: 0,
r: 0,
b: 0,
l: 0
l: 0,
});
for (var key in this.components) {
if ('boundingbox' in this.components[key])
boxes.push(this.components[key].boundingbox());
if ("boundingbox" in this.components[key]) boxes.push(this.components[key].boundingbox());
}
for (var key in this.objects)
boxes.push(this.objects[key].boundingbox());
for (var key in this.objects) boxes.push(this.objects[key].boundingbox());
var bb = boxes.shift();
for (var x of boxes) bb = bbox.expand(bb, x);
bb = bbox.move(bb, this.pos);
return bb ? bb : bbox.fromcwh([0, 0], [0, 0]);
},
toJSON() {
return {guid:this.guid};
return { guid: this.guid };
},
/* The unique components of this object. Its diff. */
json_obj(depth=0) {
json_obj(depth = 0) {
var fresh = this.ur.fresh;
var thiso = json.decode(json.encode(this)); // TODO: SLOW. Used to ignore properties in toJSON of components.
var d = ediff(thiso, fresh);
d ??= {};
fresh.objects ??= {};
var curobjs = {};
for (var o in this.objects)
curobjs[o] = this.objects[o].instance_obj();
for (var o in this.objects) curobjs[o] = this.objects[o].instance_obj();
var odiff = ediff(curobjs, fresh.objects);
if (odiff)
d.objects = curobjs;
if (odiff) d.objects = curobjs;
delete d.pos;
delete d.angle;
delete d.scale;
@ -330,7 +362,7 @@ var entity = {
delete d.angularvelocity;
return d;
},
/* The object needed to store an object as an instance of a master */
instance_obj() {
var t = os.make_transform();
@ -338,65 +370,65 @@ var entity = {
t.ur = this.ur.name;
return t;
},
transform() {
var t = {};
t.pos = this.get_pos(this.master).map(x => Math.places(x, 0));
t.angle = Math.places(this.get_angle(this.master), 4);
t.scale = this.get_scale(this.master).map(x => Math.places(x, 2));;
t.scale = this.get_scale(this.master).map(x => Math.places(x, 2));
return t;
},
dup(diff) {
dup(diff) {
var n = this.master.spawn(this.ur);
Object.totalmerge(n, this.transform());
return n;
},
kill() {
if (this.__kill) return;
this.__kill = true;
this.timers.forEach(x => x());
delete this.timers;
Event.rm_obj(this);
input.do_uncontrol(this);
if (this.master) {
this.master.remove_obj(this);
this.master = undefined;
}
for (var key in this.components) {
this.components[key].kill?.();
this.components[key].gameobject = undefined;
this.components[key].enabled = false;
delete this.components[key];
}
delete this.components;
this.clear();
if (this.stop instanceof Function) this.stop();
if (typeof this.garbage === 'function') this.garbage();
if (typeof this.then === 'function') this.then();
if (typeof this.garbage === "function") this.garbage();
if (typeof this.then === "function") this.then();
game.tag_clear_guid(this.guid);
rmactor(this);
for (var i in this) {
if (this[i] instanceof Object || this[i] instanceof Function) delete this[i];
}
},
make_objs(objs) {
for (var prop in objs) {
say(`spawning ${json.encode(objs[prop])}`);
var newobj = this.spawn(objs[prop]);
}
},
rename_obj(name, newname) {
if (!this.objects[name]) {
console.warn(`No object with name ${name}. Could not rename to ${newname}.`);
@ -406,18 +438,19 @@ dup(diff) {
Object.hide(this, name);
return;
}
if (this.objects[newname])
return;
if (this.objects[newname]) return;
this.objects[newname] = this.objects[name];
this[newname] = this[name];
this[newname].toString = function() { return newname; };
this[newname].toString = function () {
return newname;
};
Object.hide(this, newname);
delete this.objects[name];
delete this[name];
return this.objects[newname];
},
add_component(comp, data) {
var name = prosperon.guid();
this.components[name] = comp(this);
@ -438,10 +471,8 @@ var gameobject = {
if (!lur) return;
var lur = lur.objects[this.toString()];
var d = ediff(this._ed.urdiff, lur);
if (!d || Object.empty(d))
this._ed.inst = true;
else
this._ed.inst = false;
if (!d || Object.empty(d)) this._ed.inst = true;
else this._ed.inst = false;
},
namestr() {
@ -460,9 +491,9 @@ var gameobject = {
/* pin this object to the to object */
pin(to) {
var p = joint.pin(this,to);
var p = joint.pin(this, to);
},
slide(to, a = [0,0], b = [0,0], min = 0, max = 50) {
slide(to, a = [0, 0], b = [0, 0], min = 0, max = 50) {
var p = joint.slide(this, to, a, b, min, max);
p.max_force = 500;
p.break();
@ -471,10 +502,10 @@ var gameobject = {
var p = joint.pivot(this, to, piv);
},
/* groove is on to, from local points a and b, anchored to this at local anchor */
groove(to, a, b, anchor = [0,0]) {
groove(to, a, b, anchor = [0, 0]) {
var p = joint.groove(to, this, a, b, anchor);
},
damped_spring(to, length = Vector.length(this.pos,to.pos), stiffness = 1, damping = 1) {
damped_spring(to, length = Vector.length(this.pos, to.pos), stiffness = 1, damping = 1) {
var dc = 2 * Math.sqrt(stiffness * this.mass);
var p = joint.damped_spring(this, to, [0, 0], [0, 0], stiffness, damping * dc);
},
@ -506,7 +537,7 @@ var gameobject = {
this.rpos = newpos;
for (var o of this.objects) o.move(move);
},
set_angle(x, relative = world) {
var newangle = relative.angle + x;
var diff = newangle - this.angle;
@ -516,37 +547,42 @@ var gameobject = {
obj.set_pos(Vector.rotate(obj.get_pos(obj.master), diff), obj.master);
}
},
set_scale(x, relative = world) {
if (typeof x === 'number') x = [x,x,x];
var newscale = relative.scale.map((s,i) => x[i]*s);
var pct = this.scale.map((s,i) => newscale[i]/s);
if (typeof x === "number") x = [x, x, x];
var newscale = relative.scale.map((s, i) => x[i] * s);
var pct = this.scale.map((s, i) => newscale[i] / s);
this.rscale = newscale;
for (var obj of this.objects) {
obj.grow(pct);
obj.set_pos(obj.get_pos(obj.master).map((x,i) => x*pct[i]), obj.master);
};
obj.set_pos(
obj.get_pos(obj.master).map((x, i) => x * pct[i]),
obj.master,
);
}
},
get_pos(relative = world) {
if (relative === world) return this.pos;
return relative.world2this(this.pos);
//return this.pos.sub(relative.pos);
},
get_angle(relative = world) {
if (relative === world) return this.angle;
return this.angle - relative.angle;
},
get_scale(relative = world) {
if (relative === world) return this.scale;
var masterscale = relative.scale;
return this.scale.map((x,i) => x/masterscale[i]);
return this.scale.map((x, i) => x / masterscale[i]);
},
in_air() { return this.in_air(); },
in_air() {
return this.in_air();
},
/* Velocity and angular velocity of the object */
phys_obj() {
var phys = {};
@ -554,13 +590,13 @@ var gameobject = {
phys.angularvelocity = this.angularvelocity;
return phys;
},
set category(n) {
if (n === 0) {
this.categories = n;
return;
}
var cat = (1 << (n-1));
var cat = 1 << (n - 1);
this.categories = cat;
},
get category() {
@ -568,16 +604,16 @@ var gameobject = {
var pos = 0;
var num = this.categories;
while (num > 0) {
if (num & 1) {
break;
}
pos++;
num >>>= 1;
if (num & 1) {
break;
}
pos++;
num >>>= 1;
}
return pos+1;
}
}
return pos + 1;
},
};
entity.spawn.doc = `Spawn an entity of type 'ur' on this entity. Returns the spawned entity.`;
@ -598,12 +634,12 @@ gameobject.doc = {
set_pos: `Function to set the position of the object in world coordinates.`,
worldangle: `Function to get the angle of the entity in the world.`,
rotate: `Function to rotate this object by x degrees.`,
move: 'Move an object by x,y,z. If the first parameter is an array, uses up to the first three array values.',
move: "Move an object by x,y,z. If the first parameter is an array, uses up to the first three array values.",
pulse: `Apply an impulse to this body in world coordinates. Impulse is a short force.`,
shove: `Apply a force to this body in world coordinates. Should be used over many frames.`,
shove_at: 'Apply a force to this body, at a position relative to itself.',
max_velocity: 'The max linear velocity this object can travel.',
max_angularvelocity: 'The max angular velocity this object can rotate.',
shove_at: "Apply a force to this body, at a position relative to itself.",
max_velocity: "The max linear velocity this object can travel.",
max_angularvelocity: "The max angular velocity this object can rotate.",
on_ground: `Return true if the object is on the ground.`,
spawn: `Create an instance of a supplied ur-type on this object. Optionally provide a data object to modify the created entity.`,
hide: `Make this object invisible.`,
@ -616,28 +652,27 @@ gameobject.doc = {
transform: `Return an object representing the transform state of this object.`,
kill: `Remove this object from the world.`,
master: "The entity this entity belongs to.",
delay: 'Run the given function after the given number of seconds has elapsed.',
cry: 'Make a sound. Can only make one at a time.',
add_component: 'Add a component to the object by name.',
pin: 'Pin joint to another object. Acts as if a rigid rod is between the two objects.',
slide: 'Slide joint, similar to a pin but with min and max allowed distances.',
pivot: 'Pivot joint to an object, with the pivot given in world coordinates.',
groove: 'Groove joint. The groove is on to, from to local coordinates a and b, with this object anchored at anchor.',
damped_spring: 'Damped spring to another object. Length is the distance it wants to be, stiffness is the spring constant, and damping is the damping ratio. 1 is critical, < 1 is underdamped, > 1 is overdamped.',
damped_rotary_spring: 'Similar to damped spring but for rotation. Rest angle is the attempted angle.',
rotary_limit: 'Limit the angle relative to the to body between min and max.',
ratchet: 'Like a socket wrench, relative to to. ratch is the distance between clicks.',
gear: 'Keeps the angular velocity ratio of this body and to constant. Ratio is the gear ratio.',
motor: 'Keeps the relative angular velocity of this body to to at a constant rate. The most simple idea is for one of the bodies to be static, to the other is kept at rate.',
layer: 'Bitmask for collision layers.',
drawlayer: 'Layer for drawing. Higher numbers draw above lower ones.',
warp_filter: 'Bitmask for selecting what warps should affect this entity.',
delay: "Run the given function after the given number of seconds has elapsed.",
cry: "Make a sound. Can only make one at a time.",
add_component: "Add a component to the object by name.",
pin: "Pin joint to another object. Acts as if a rigid rod is between the two objects.",
slide: "Slide joint, similar to a pin but with min and max allowed distances.",
pivot: "Pivot joint to an object, with the pivot given in world coordinates.",
groove: "Groove joint. The groove is on to, from to local coordinates a and b, with this object anchored at anchor.",
damped_spring: "Damped spring to another object. Length is the distance it wants to be, stiffness is the spring constant, and damping is the damping ratio. 1 is critical, < 1 is underdamped, > 1 is overdamped.",
damped_rotary_spring: "Similar to damped spring but for rotation. Rest angle is the attempted angle.",
rotary_limit: "Limit the angle relative to the to body between min and max.",
ratchet: "Like a socket wrench, relative to to. ratch is the distance between clicks.",
gear: "Keeps the angular velocity ratio of this body and to constant. Ratio is the gear ratio.",
motor: "Keeps the relative angular velocity of this body to to at a constant rate. The most simple idea is for one of the bodies to be static, to the other is kept at rate.",
layer: "Bitmask for collision layers.",
drawlayer: "Layer for drawing. Higher numbers draw above lower ones.",
warp_filter: "Bitmask for selecting what warps should affect this entity.",
};
global.ur = {};
if (io.exists(`${io.dumpfolder}/ur.json`))
ur = json.decode(io.slurp(`${io.dumpfolder}/ur.json`));
if (io.exists(`${io.dumpfolder}/ur.json`)) ur = json.decode(io.slurp(`${io.dumpfolder}/ur.json`));
else {
ur = {};
ur._list = [];
@ -655,79 +690,72 @@ ur {
/* Apply an ur u to an entity e */
/* u is given as */
function apply_ur(u, ent) {
if (typeof u !== 'string') {
if (typeof u !== "string") {
console.warn("Must give u as a string.");
return;
}
var urs = u.split('.');
var urs = u.split(".");
if (!urs.every(u => ur[u])) {
console.error(`Attempted to make ur combo ${u} but not every ur in the chain exists.`);
return;
}
for (var u of urs) {
var text = u.text;
var data = u.data;
if (typeof text === 'string')
use(text, ent);
else if (Array.isArray(text))
for (var path of text) use(path,ent);
if (typeof data === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
if (typeof text === "string") use(text, ent);
else if (Array.isArray(text)) for (var path of text) use(path, ent);
if (typeof data === "string") Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (Array.isArray(data)) {
for (var path of data) {
if (typeof path === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (path instanceof Object)
Object.merge(ent,path);
};
if (typeof path === "string") Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (path instanceof Object) Object.merge(ent, path);
}
}
}
}
var emptyur = {
name: "empty"
}
name: "empty",
};
var getur = function(text, data)
{
var getur = function (text, data) {
if (!text && !data) {
console.info('empty ur');
console.info("empty ur");
return {
name: "empty"
name: "empty",
};
}
var urstr = text;
if (data)
urstr += "+" + data;
if (data) urstr += "+" + data;
if (!ur[urstr]) {
ur[urstr] = {
name: urstr,
text: text,
data: data
}
data: data,
};
}
return ur[urstr];
}
};
var ur_from_file = function(file) {
var ur_from_file = function (file) {
var urname = file.name();
if (ur[urname]) {
console.warn(`Tried to make another ur with the name ${urname} from ${file}, but it already exists.`);
return undefined;
}
var newur = {
name: urname
name: urname,
};
ur[urname] = newur;
ur._list.push(urname);
return newur;
}
};
game.loadurs = function() {
game.loadurs = function () {
return;
ur = {};
ur._list = [];
@ -739,12 +767,12 @@ game.loadurs = function() {
var urjson = json.decode(uur);
Object.assign(newur, urjson);
}
for (var file of io.glob("**.jso").filter(f => !ur[f.name()])) {
if (file[0] === '.' || file[0] === '_') continue;
if (file[0] === "." || file[0] === "_") continue;
var newur = ur_from_file(file);
if (!newur) continue;
newur.text = file;
newur.text = file;
var data = file.set_ext(".json");
if (io.exists(data)) {
@ -755,9 +783,8 @@ game.loadurs = function() {
};
game.ur = {};
game.ur.load = function(str) {}
game.ur.add_data = function(str, data)
{
game.ur.load = function (str) {};
game.ur.add_data = function (str, data) {
var nur = ur[str];
if (!nur) {
console.warn(`Cannot add data to the ur ${str}.`);
@ -768,17 +795,16 @@ game.ur.add_data = function(str, data)
if (ur.data) arr.push(ur.data);
ur.data = arr;
}
ur.data.push(data);
}
game.ur.save = function(str)
{
ur.data.push(data);
};
game.ur.save = function (str) {
var nur = ur[str];
if (!nur) {
console.warn(`Cannot save ur ${str}.`);
return;
}
}
};
return { entity }
return { entity };

View file

@ -1,81 +1,70 @@
var shape = {};
shape.box = {};
shape.box.points = function(ll, ur)
{
return [ll, ll.add([ur.x-ll.x,0]), ur, ll.add([0,ur.y-ll.y])];
}
shape.box.points = function (ll, ur) {
return [ll, ll.add([ur.x - ll.x, 0]), ur, ll.add([0, ur.y - ll.y])];
};
shape.sphere = {};
shape.circle = {};
shape.sphere.volume = function(r) { return Math.pi*r*r*r*4/3; };
shape.sphere.random = function(r,theta = [0,1], phi = [-0.5,0.5])
{
if (typeof r === 'number') r = [r,r];
if (typeof theta === 'number') theta = [theta,theta];
if (typeof phi === 'number') phi = [phi,phi];
var ra = Math.random_range(r[0],r[1]);
var ta = Math.turn2rad(Math.random_range(theta[0],theta[1]));
var pa = Math.turn2rad(Math.random_range(phi[0],phi[1]));
return [
ra*Math.sin(ta)*Math.cos(pa),
ra*Math.sin(ta)*Math.sin(pa),
ra*Math.cos(ta)
];
}
shape.sphere.volume = function (r) {
return (Math.pi * r * r * r * 4) / 3;
};
shape.sphere.random = function (r, theta = [0, 1], phi = [-0.5, 0.5]) {
if (typeof r === "number") r = [r, r];
if (typeof theta === "number") theta = [theta, theta];
if (typeof phi === "number") phi = [phi, phi];
shape.circle.area = function(r) { return Math.pi*r*r; };
shape.circle.random = function(r,theta)
{
return shape.sphere.random(r,theta).xz;
}
var ra = Math.random_range(r[0], r[1]);
var ta = Math.turn2rad(Math.random_range(theta[0], theta[1]));
var pa = Math.turn2rad(Math.random_range(phi[0], phi[1]));
return [ra * Math.sin(ta) * Math.cos(pa), ra * Math.sin(ta) * Math.sin(pa), ra * Math.cos(ta)];
};
shape.box = function(w,h) {
shape.circle.area = function (r) {
return Math.pi * r * r;
};
shape.circle.random = function (r, theta) {
return shape.sphere.random(r, theta).xz;
};
shape.box = function (w, h) {
w /= 2;
h /= 2;
var points = [
[w,h],
[-w,h],
[-w,-h],
[w,-h]
[w, h],
[-w, h],
[-w, -h],
[w, -h],
];
return points;
};
shape.ngon = function(radius, n) {
return shape.arc(radius,360,n);
shape.ngon = function (radius, n) {
return shape.arc(radius, 360, n);
};
shape.arc = function(radius, angle, n, start = 0) {
shape.arc = function (radius, angle, n, start = 0) {
start = Math.deg2rad(start);
if (angle >= 360)
angle = 360;
if (angle >= 360) angle = 360;
if (n <= 1) return [];
var points = [];
angle = Math.deg2rad(angle);
var arclen = angle/n;
for (var i = 0; i < n; i++)
points.push(Vector.rotate([radius,0], start + (arclen*i)));
var arclen = angle / n;
for (var i = 0; i < n; i++) points.push(Vector.rotate([radius, 0], start + arclen * i));
return points;
};
shape.circle.points = function(radius, n) {
shape.circle.points = function (radius, n) {
if (n <= 1) return [];
return shape.arc(radius, 360, n);
};
shape.corners2points = function(ll, ur)
{
return [
ll,
ll.add([ur.x,0]),
ur,
ll.add([0,ur.y]),
];
}
shape.corners2points = function (ll, ur) {
return [ll, ll.add([ur.x, 0]), ur, ll.add([0, ur.y])];
};
return {shape};
return { shape };

View file

@ -1,31 +1,30 @@
input.keycodes = {
32: "space",
45: "minus",
256: "escape",
32: "space",
45: "minus",
256: "escape",
257: "enter",
258: "tab",
259: "backspace",
260: "insert",
261: "delete",
262: "right",
262: "right",
263: "left",
264: "down",
264: "down",
265: "up",
266: "pgup",
266: "pgup",
267: "pgdown",
268: "home",
269: "end",
};
input.codekeys = {};
for (var code in input.keycodes)
input.codekeys[input.keycodes[code]] = code;
for (var code in input.keycodes) input.codekeys[input.keycodes[code]] = code;
var mod = {
shift: 0,
ctrl: 0,
alt: 0,
super: 0
super: 0,
};
/*
@ -35,32 +34,32 @@ pressed
down
*/
function keycode(name) { return charCodeAt(name); }
function keycode(name) {
return charCodeAt(name);
}
function keyname_extd(key) {
if (!parseInt(key)) return key;
function keyname_extd(key)
{
if (!parseInt(key)) return key;
if (key > 289 && key < 302) {
var num = key-289;
var num = key - 289;
return `f${num}`;
}
if (key >= 320 && key <= 329) {
var num = key-320;
var num = key - 320;
return `kp${num}`;
}
if (input.keycodes[key]) return input.keycodes[key];
if (key >= 32 && key <= 126) return String.fromCharCode(key).lc();
return undefined;
}
var downkeys = {};
function modstr()
{
function modstr() {
var s = "";
if (mod.ctrl) s += "C-";
if (mod.alt) s += "M-";
@ -68,98 +67,89 @@ function modstr()
return s;
}
prosperon.keydown = function(key, repeat)
{
prosperon.keydown = function (key, repeat) {
downkeys[key] = true;
if (key == 341 || key == 345)
mod.ctrl = 1;
else if (key == 342 || key == 346)
mod.alt = 1;
else if (key == 343 || key == 347)
mod.super = 1;
else if (key == 340 || key == 344)
mod.shift = 1;
if (key == 341 || key == 345) mod.ctrl = 1;
else if (key == 342 || key == 346) mod.alt = 1;
else if (key == 343 || key == 347) mod.super = 1;
else if (key == 340 || key == 344) mod.shift = 1;
else {
var emacs = modstr() + keyname_extd(key);
if (repeat)
player[0].raw_input(emacs, "rep");
else
player[0].raw_input(emacs, "pressed");
if (repeat) player[0].raw_input(emacs, "rep");
else player[0].raw_input(emacs, "pressed");
}
}
};
prosperon.keyup = function(key)
{
prosperon.keyup = function (key) {
delete downkeys[key];
if (key == 341 || key == 345)
mod.ctrl = 0;
else if (key == 342 || key == 346)
mod.alt = 0;
else if (key == 343 || key == 347)
mod.super = 0;
else if (key == 340 || key == 344)
mod.shift = 0;
if (key == 341 || key == 345) mod.ctrl = 0;
else if (key == 342 || key == 346) mod.alt = 0;
else if (key == 343 || key == 347) mod.super = 0;
else if (key == 340 || key == 344) mod.shift = 0;
else {
var emacs = modstr() + keyname_extd(key);
player[0].raw_input(emacs, "released");
}
}
};
prosperon.droppedfile = function(path)
{
prosperon.droppedfile = function (path) {
player[0].raw_input("drop", "pressed", path);
}
};
var mousepos = [0,0];
var mousepos = [0, 0];
prosperon.textinput = function(c){
prosperon.textinput = function (c) {
player[0].raw_input("char", "pressed", c);
};
prosperon.mousemove = function(pos, dx){
prosperon.mousemove = function (pos, dx) {
mousepos = pos;
mousepos.y = window.size.y - mousepos.y;
player[0].mouse_input("move", pos, dx);
};
prosperon.mousescroll = function(dx){
prosperon.mousescroll = function (dx) {
player[0].mouse_input(modstr() + "scroll", dx);
};
prosperon.mousedown = function(b){
prosperon.mousedown = function (b) {
player[0].raw_input(modstr() + input.mouse.button[b], "pressed");
downkeys[input.mouse.button[b]] = true;
};
prosperon.mouseup = function(b){
prosperon.mouseup = function (b) {
player[0].raw_input(input.mouse.button[b], "released");
delete downkeys[input.mouse.button[b]];
};
input.mouse = {};
input.mouse.screenpos = function() { return mousepos.slice(); };
input.mouse.worldpos = function() { return game.camera.view2world(mousepos); };
input.mouse.disabled = function() { input.mouse_mode(1); };
input.mouse.normal = function() { input.mouse_mode(0); };
input.mouse.mode = function(m) {
if (input.mouse.custom[m])
input.cursor_img(input.mouse.custom[m]);
else
input.mouse_cursor(m);
input.mouse.screenpos = function () {
return mousepos.slice();
};
input.mouse.set_custom_cursor = function(img, mode = input.mouse.cursor.default) {
if (!img)
delete input.mouse.custom[mode];
input.mouse.worldpos = function () {
return game.camera.view2world(mousepos);
};
input.mouse.disabled = function () {
input.mouse_mode(1);
};
input.mouse.normal = function () {
input.mouse_mode(0);
};
input.mouse.mode = function (m) {
if (input.mouse.custom[m]) input.cursor_img(input.mouse.custom[m]);
else input.mouse_cursor(m);
};
input.mouse.set_custom_cursor = function (img, mode = input.mouse.cursor.default) {
if (!img) delete input.mouse.custom[mode];
else {
input.cursor_img(img);
input.mouse.custom[mode] = img;
}
};
input.mouse.button = { /* left, right, middle mouse */
0: "lm",
input.mouse.button = {
/* left, right, middle mouse */ 0: "lm",
1: "rm",
2: "mm"
2: "mm",
};
input.mouse.custom = [];
input.mouse.cursor = {
@ -173,7 +163,7 @@ input.mouse.cursor = {
nwse: 7,
nesw: 8,
resize: 9,
no: 10
no: 10,
};
input.mouse.doc = {};
@ -183,14 +173,14 @@ input.mouse.disabled.doc = "Set the mouse to hidden. This locks it to the game a
input.mouse.normal.doc = "Set the mouse to show again after hiding.";
input.keyboard = {};
input.keyboard.down = function(code) {
if (typeof code === 'number') return downkeys[code];
if (typeof code === 'string') return (downkeys[code.uc().charCodeAt()] || downkeys[code.lc().charCodeAt()]);
input.keyboard.down = function (code) {
if (typeof code === "number") return downkeys[code];
if (typeof code === "string") return downkeys[code.uc().charCodeAt()] || downkeys[code.lc().charCodeAt()];
return undefined;
}
};
input.state2str = function(state) {
if (typeof state === 'string') return state;
input.state2str = function (state) {
if (typeof state === "string") return state;
switch (state) {
case 0:
return "down";
@ -199,10 +189,10 @@ input.state2str = function(state) {
case 2:
return "released";
}
}
};
input.print_pawn_kbm = function(pawn) {
if (!('inputs' in pawn)) return;
input.print_pawn_kbm = function (pawn) {
if (!("inputs" in pawn)) return;
var str = "";
for (var key in pawn.inputs) {
if (!pawn.inputs[key].doc) continue;
@ -217,13 +207,11 @@ joysticks["wasd"] = {
uy: "w",
dy: "s",
ux: "d",
dx: "a"
dx: "a",
};
input.procdown = function()
{
for (var k in downkeys)
player[0].raw_input(keyname_extd(k), "down");
input.procdown = function () {
for (var k in downkeys) player[0].raw_input(keyname_extd(k), "down");
for (var i in joysticks) {
var joy = joysticks[i];
@ -231,10 +219,10 @@ input.procdown = function()
var y = joy.uy - joy.dy;
player[0].joy_input(i, joysticks[i]);
}
}
};
input.print_md_kbm = function(pawn) {
if (!('inputs' in pawn)) return;
input.print_md_kbm = function (pawn) {
if (!("inputs" in pawn)) return;
var str = "";
str += "|control|description|\n|---|---|\n";
@ -247,8 +235,8 @@ input.print_md_kbm = function(pawn) {
return str;
};
input.has_bind = function(pawn, bind) {
return (typeof pawn.inputs?.[bind] === 'function');
input.has_bind = function (pawn, bind) {
return typeof pawn.inputs?.[bind] === "function";
};
input.action = {
@ -263,28 +251,36 @@ input.action = {
actions: [],
};
input.tabcomplete = function(val, list) {
if (!val) return val;
list.dofilter(function(x) { return x.startsWith(val); });
input.tabcomplete = function (val, list) {
if (!val) return val;
list.dofilter(function (x) {
return x.startsWith(val);
});
if (list.length === 1) {
return list[0];
}
var ret = undefined;
var i = val.length;
while (!ret && !Object.empty(list)) {
var char = list[0][i];
if (!list.every(function(x) { return x[i] === char; }))
ret = list[0].slice(0, i);
else {
i++;
list.dofilter(function(x) { return x.length-1 > i; });
}
}
if (list.length === 1) {
return list[0];
}
return ret ? ret : val;
}
var ret = undefined;
var i = val.length;
while (!ret && !Object.empty(list)) {
var char = list[0][i];
if (
!list.every(function (x) {
return x[i] === char;
})
)
ret = list[0].slice(0, i);
else {
i++;
list.dofilter(function (x) {
return x.length - 1 > i;
});
}
}
return ret ? ret : val;
};
/* May be a human player; may be an AI player */
@ -300,24 +296,22 @@ var Player = {
mouse_input(type, ...args) {
for (var pawn of this.pawns.reversed()) {
if (typeof pawn.inputs?.mouse?.[type] === 'function') {
pawn.inputs.mouse[type].call(pawn,...args);
pawn.inputs.post?.call(pawn);
if (!pawn.inputs.fallthru)
return;
if (typeof pawn.inputs?.mouse?.[type] === "function") {
pawn.inputs.mouse[type].call(pawn, ...args);
pawn.inputs.post?.call(pawn);
if (!pawn.inputs.fallthru) return;
}
}
},
char_input(c) {
for (var pawn of this.pawns.reversed()) {
if (typeof pawn.inputs?.char === 'function') {
if (typeof pawn.inputs?.char === "function") {
pawn.inputs.char.call(pawn, c);
pawn.inputs.post?.call(pawn);
if (!pawn.inputs.fallthru)
return;
pawn.inputs.post?.call(pawn);
if (!pawn.inputs.fallthru) return;
}
};
}
},
joy_input(name, joystick) {
@ -329,11 +323,11 @@ var Player = {
var x = 0;
if (input.keyboard.down(joystick.ux)) x++;
if (input.keyboard.down(joystick.dx)) x--;
var y = 0;
var y = 0;
if (input.keyboard.down(joystick.uy)) y++;
if (input.keyboard.down(joystick.dy)) y--;
pawn.inputs.joystick[name](x,y);
pawn.inputs.joystick[name](x, y);
}
},
@ -341,62 +335,56 @@ var Player = {
for (var pawn of this.pawns.reversed()) {
if (!pawn.inputs) {
console.error(`pawn no longer has inputs object.`);
continue;
continue;
}
var block = pawn.inputs.block;
if (!pawn.inputs[cmd]) {
if (pawn.inputs.block) return;
continue;
continue;
}
var fn = null;
switch (state) {
case 'pressed':
fn = pawn.inputs[cmd];
break;
case 'rep':
fn = pawn.inputs[cmd].rep ? pawn.inputs[cmd] : null;
break;
case 'released':
fn = pawn.inputs[cmd].released;
break;
case 'down':
if (typeof pawn.inputs[cmd].down === 'function')
fn = pawn.inputs[cmd].down;
else if (pawn.inputs[cmd].down)
fn = pawn.inputs[cmd];
case "pressed":
fn = pawn.inputs[cmd];
break;
case "rep":
fn = pawn.inputs[cmd].rep ? pawn.inputs[cmd] : null;
break;
case "released":
fn = pawn.inputs[cmd].released;
break;
case "down":
if (typeof pawn.inputs[cmd].down === "function") fn = pawn.inputs[cmd].down;
else if (pawn.inputs[cmd].down) fn = pawn.inputs[cmd];
}
if (typeof fn === 'function')
fn.call(pawn, ... args);
if (typeof fn === "function") fn.call(pawn, ...args);
if (!pawn.inputs)
if (block) return;
else continue;
else continue;
if (state === 'released')
pawn.inputs.release_post?.call(pawn);
if (state === "released") pawn.inputs.release_post?.call(pawn);
if (!pawn.inputs.fallthru) return;
if (pawn.inputs.block) return;
if (pawn.inputs.block) return;
}
},
obj_controlled(obj) {
for (var p in Player.players) {
if (p.pawns.contains(obj))
return true;
if (p.pawns.contains(obj)) return true;
}
return false;
},
print_pawns() {
for (var pawn of this.pawns.reversed())
say(pawn.toString());
for (var pawn of this.pawns.reversed()) say(pawn.toString());
},
create() {
@ -404,7 +392,7 @@ var Player = {
n.pawns = [];
n.gamepads = [];
this.players.push(n);
this[this.players.length-1] = n;
this[this.players.length - 1] = n;
return n;
},
@ -423,12 +411,11 @@ var Player = {
},
};
input.do_uncontrol = function(pawn)
{
Player.players.forEach(function(p) {
input.do_uncontrol = function (pawn) {
Player.players.forEach(function (p) {
p.pawns = p.pawns.filter(x => x !== pawn);
});
}
};
for (var i = 0; i < 4; i++) {
Player.create();
@ -443,5 +430,5 @@ Player.doc.players = "A list of current players.";
var player = Player;
return {
player
player,
};

View file

@ -5,17 +5,16 @@ var panel;
var selected = undefined;
mum.inputs = {};
mum.inputs.lm = function()
{
mum.inputs.lm = function () {
if (!selected) return;
if (!selected.action) return;
selected.action();
}
};
mum.base = {
pos: null, // If set, puts the cursor to this position before drawing the element
offset:[0,0], // Move x,y to the right and down before drawing
padding:[0,0], // Pad inwards after drawing, to prepare for the next element
offset: [0, 0], // Move x,y to the right and down before drawing
padding: [0, 0], // Pad inwards after drawing, to prepare for the next element
font: "fonts/c64.ttf",
selectable: false,
selected: false,
@ -23,33 +22,33 @@ mum.base = {
scale: 1,
angle: 0,
inset: null,
anchor: [0,1], // where to draw the item from, relative to the cursor. [0,1] is from the top left corner. [1,0] is from the bottom right
anchor: [0, 1], // where to draw the item from, relative to the cursor. [0,1] is from the top left corner. [1,0] is from the bottom right
background_image: null,
slice: null, // pass to slice an image as a 9 slice. see render.slice9 for its format
hover: {
color: Color.red,
},
text_shadow: {
pos: [0,0],
pos: [0, 0],
color: Color.white,
},
border: 0, // Draw a border around the element. For text, an outline.
overflow: "wrap", // how to deal with overflow from parent element
wrap: -1,
text_align: "left", /* left, center, right */
text_align: "left" /* left, center, right */,
shader: null, // Use this shader, instead of the engine provided one
color: Color.white,
opacity:1,
width:0,
height:0,
opacity: 1,
width: 0,
height: 0,
max_width: Infinity,
max_height: Infinity,
image_repeat: false,
image_repeat_offset: [0,0],
debug: false, /* set to true to draw debug boxes */
image_repeat_offset: [0, 0],
debug: false /* set to true to draw debug boxes */,
hide: false,
tooltip: null,
}
};
// data is passed into each function, and various stats are generated
// drawpos: the point to start the drawing from
@ -57,22 +56,23 @@ mum.base = {
// bound: a boundingbox around the drawn UI element
// extent: a boundingbox around the total extents of the element (ie before padding)
function show_debug() { return prosperon.debug && mum.debug; }
function show_debug() {
return prosperon.debug && mum.debug;
}
mum.debug = false;
var post = function() {};
var post = function () {};
var posts = [];
mum.style = mum.base;
var cursor = [0,0];
var cursor = [0, 0];
var pre = function(data)
{
var pre = function (data) {
if (data.hide) return true;
data.__proto__ = mum.style;
if (data.pos) cursor = data.pos.slice();
data.drawpos = cursor.slice().add(data.offset);
@ -81,87 +81,77 @@ var pre = function(data)
data.color[3] = data.opacity;
}
data.wh = [data.width,data.height];
}
data.wh = [data.width, data.height];
};
var anchor_calc = function(data)
{
var aa = [0,1].sub(data.anchor);
data.drawpos = data.drawpos.add([data.width,data.height]).scale(aa);
}
var anchor_calc = function (data) {
var aa = [0, 1].sub(data.anchor);
data.drawpos = data.drawpos.add([data.width, data.height]).scale(aa);
};
var end = function(data)
{
var end = function (data) {
cursor = cursor.add(data.padding);
post(data);
}
};
mum.list = function(fn, data = {})
{
mum.list = function (fn, data = {}) {
if (pre(data)) return;
var aa = [0,1].sub(data.anchor);
cursor = cursor.add([data.width,data.height].scale(aa)).add(data.offset).add(data.padding);
var aa = [0, 1].sub(data.anchor);
cursor = cursor.add([data.width, data.height].scale(aa)).add(data.offset).add(data.padding);
posts.push(post);
post = mum.list.post.bind(data);
if (show_debug())
render.boundingbox({
t:cursor.y,
b:cursor.y-data.height,
l:cursor.x,
r:cursor.x+data.width
t: cursor.y,
b: cursor.y - data.height,
l: cursor.x,
r: cursor.x + data.width,
});
//if (data.background_image) mum.image(null, Object.create(data))
//if (data.background_image) mum.image(null, Object.create(data))
if (data.background_image) {
var imgpos = data.pos.slice();
imgpos.y -= data.height/2;
imgpos.x -= data.width/2;
var imgscale = [data.width,data.height];
if (data.slice)
render.slice9(game.texture(data.background_image), imgpos, data.slice, imgscale);
else
render.image(game.texture(data.background_image), imgpos, [data.width,data.height]);
imgpos.y -= data.height / 2;
imgpos.x -= data.width / 2;
var imgscale = [data.width, data.height];
if (data.slice) render.slice9(game.texture(data.background_image), imgpos, data.slice, imgscale);
else render.image(game.texture(data.background_image), imgpos, [data.width, data.height]);
}
fn();
data.bb.l -= data.padding.x;
data.bb.r += data.padding.x;
data.bb.t += data.padding.y;
data.bb.b -= data.padding.y;
if (show_debug())
render.boundingbox(data.bb);
if (show_debug()) render.boundingbox(data.bb);
post = posts.pop();
end(data);
}
};
mum.list.post = function(e)
{
cursor.y -= (e.bb.t - e.bb.b);
mum.list.post = function (e) {
cursor.y -= e.bb.t - e.bb.b;
cursor.y -= e.padding.y;
if (this.bb)
this.bb = bbox.expand(this.bb,e.bb)
else
this.bb = e.bb;
}
mum.label = function(str, data = {})
{
if (this.bb) this.bb = bbox.expand(this.bb, e.bb);
else this.bb = e.bb;
};
mum.label = function (str, data = {}) {
if (pre(data)) return;
render.set_font(data.font, data.font_size);
data.bb = render.text_bb(str, data.scale, -1, cursor);
data.wh = bbox.towh(data.bb);
var aa = [0,1].sub(data.anchor);
data.drawpos.y -= (data.bb.t-cursor.y);
var aa = [0, 1].sub(data.anchor);
data.drawpos.y -= data.bb.t - cursor.y;
data.drawpos = data.drawpos.add(data.wh.scale(aa)).add(data.offset);
data.bb = render.text_bb(str, data.scale, data.wrap, data.drawpos);
@ -175,87 +165,72 @@ mum.label = function(str, data = {})
}
data.bb = render.text(str, data.drawpos, data.scale, data.color, data.wrap);
if (show_debug())
render.boundingbox(data.bb);
end(data);
}
mum.image = function(path, data = {})
{
if (show_debug()) render.boundingbox(data.bb);
end(data);
};
mum.image = function (path, data = {}) {
if (pre(data)) return;
path ??= data.background_image;
var tex = path;
if (typeof path === 'string')
tex = game.texture(path);
if (typeof path === "string") tex = game.texture(path);
if (!data.height)
if (data.width)
data.height = tex.height * (data.width/tex.width);
else
data.height = tex.height;
if (data.width) data.height = tex.height * (data.width / tex.width);
else data.height = tex.height;
if (!data.width)
if (data.height)
data.width = tex.width * (data.height/tex.height);
else
data.height = tex.height;
if (data.height) data.width = tex.width * (data.height / tex.height);
else data.height = tex.height;
if (!data.width) data.width = tex.width;
if (!data.height) data.height = tex.height;
var aa = [0,1].sub(data.anchor);
data.drawpos = data.drawpos.add(aa.scale([data.width,data.height]));
if (data.slice)
render.slice9(tex, data.drawpos, data.slice, [data.width,data.height]);
else
data.bb = render.image(tex, data.drawpos, [data.width, data.height]);
end(data);
}
var aa = [0, 1].sub(data.anchor);
data.drawpos = data.drawpos.add(aa.scale([data.width, data.height]));
mum.rectangle = function(data = {})
{
if (data.slice) render.slice9(tex, data.drawpos, data.slice, [data.width, data.height]);
else data.bb = render.image(tex, data.drawpos, [data.width, data.height]);
end(data);
};
mum.rectangle = function (data = {}) {
if (pre(data)) return;
var aa = [0,0].sub(data.anchor);
data.drawpos = data.drawpos.add(aa.scale([data.width,data.height]));
render.rectangle(data.drawpos, data.drawpos.add([data.width,data.height]), data.color);
var aa = [0, 0].sub(data.anchor);
data.drawpos = data.drawpos.add(aa.scale([data.width, data.height]));
render.rectangle(data.drawpos, data.drawpos.add([data.width, data.height]), data.color);
end(data);
}
};
var btnbb;
var btnpost = function()
{
var btnpost = function () {
btnbb = data.bb;
}
};
mum.button = function(str, data = {padding:[4,4], color:Color.black})
{
mum.button = function (str, data = { padding: [4, 4], color: Color.black }) {
if (pre(data)) return;
posts.push(post);
post = btnpost;
if (typeof str === 'string')
render.text(str, cursor.add(data.padding), data.scale, data.color);
else
str();
if (typeof str === "string") render.text(str, cursor.add(data.padding), data.scale, data.color);
else str();
if (data.action && data.hover && bbox.pointin(btnbb, input.mouse.screenpos())) {
data.hover.__proto__ = data;
data = data.hover;
}
render.rectangle([btnbb.l-data.padding.x, btnbb.b-data.padding.y], [btnbb.r+data.padding.y, btnbb.t+data.padding.y], data.color);
render.rectangle([btnbb.l - data.padding.x, btnbb.b - data.padding.y], [btnbb.r + data.padding.y, btnbb.t + data.padding.y], data.color);
data.bb = btnbb;
post = posts.pop();
end(data);
}
};
mum.window = function(fn, data = {})
{
mum.window = function (fn, data = {}) {
if (pre(data)) return;
render.rectangle(cursor, cursor.add(data.size), data.color);
@ -263,28 +238,34 @@ mum.window = function(fn, data = {})
cursor = cursor.add(data.padding);
fn();
end(data);
}
};
mum.ex_hud = function()
{
mum.label("TOP LEFT", {pos:[0,game.size.y], anchor:[0,1]});
mum.label("BOTTOM LEFT", {pos:[0,0], anchor:[0,0]});
mum.label("TOP RIGHT", {pos:game.size, anchor:[1,1]});
mum.label("BOTTOM RIGHT", {pos:[game.size.x, 0], anchor:[1,0]});
}
mum.ex_hud = function () {
mum.label("TOP LEFT", { pos: [0, game.size.y], anchor: [0, 1] });
mum.label("BOTTOM LEFT", { pos: [0, 0], anchor: [0, 0] });
mum.label("TOP RIGHT", { pos: game.size, anchor: [1, 1] });
mum.label("BOTTOM RIGHT", { pos: [game.size.x, 0], anchor: [1, 0] });
};
mum.drawinput = undefined;
var ptext = "";
var panpan = {
draw() {
mum.rectangle({pos:[0,0], anchor:[0,0], height:20, width: window.size.x, padding:[10,16], color:Color.black});
mum.rectangle({
pos: [0, 0],
anchor: [0, 0],
height: 20,
width: window.size.x,
padding: [10, 16],
color: Color.black,
});
mum.label("input level: ");
mum.label(ptext, {offset:[50,0], color:Color.red});
mum.label(ptext, { offset: [50, 0], color: Color.red });
},
inputs: {
block: true,
char(c) {
ptext += c
ptext += c;
},
enter() {
delete mum.drawinput;
@ -295,18 +276,18 @@ var panpan = {
player[0].uncontrol(panpan);
},
backspace() {
ptext = ptext.slice(0,ptext.length-1);
}
ptext = ptext.slice(0, ptext.length - 1);
},
},
}
};
mum.textinput = function (fn, str = "") {
mum.drawinput = panpan.draw;
ptext = str;
player[0].control(panpan);
panpan.inputs.enter = function() {
panpan.inputs.enter = function () {
fn(ptext);
delete mum.drawinput;
player[0].uncontrol(panpan);
}
}
};
};

View file

@ -1,4 +1,6 @@
this.hud = function()
{
mum.label("No game yet! Make game.js to get started!", {pos:game.size.scale(0.5), anchor:[0.5,0.5]});
}
this.hud = function () {
mum.label("No game yet! Make game.js to get started!", {
pos: game.size.scale(0.5),
anchor: [0.5, 0.5],
});
};

View file

@ -7,44 +7,38 @@ emitter.spawn_timer = 0;
emitter.pps = 0;
emitter.color = Color.white;
emitter.draw = function()
{
emitter.draw = function () {
var pars = Object.values(this.particles);
if (pars.length === 0) return;
render.use_shader(this.shader);
render.use_mat(this);
render.make_particle_ssbo(pars, this.ssbo);
render.draw(this.shape, this.ssbo, pars.length);
}
};
emitter.kill = function()
{
emitter.kill = function () {
emitters.remove(this);
}
};
var std_step = function(p)
{
var std_step = function (p) {
if (p.time < this.grow_for) {
var s = Math.lerp(0, this.scale, p.time/this.grow_for);
p.transform.scale = [s,s,s];
}
else if (p.time > (p.life - this.shrink_for)) {
var s = Math.lerp(0,this.scale,(p.life-p.time)/this.shrink_for);
p.transform.scale=[s,s,s];
} else
p.transform.scale = [this.scale,this.scale,this.scale];
}
var s = Math.lerp(0, this.scale, p.time / this.grow_for);
p.transform.scale = [s, s, s];
} else if (p.time > p.life - this.shrink_for) {
var s = Math.lerp(0, this.scale, (p.life - p.time) / this.shrink_for);
p.transform.scale = [s, s, s];
} else p.transform.scale = [this.scale, this.scale, this.scale];
};
emitter.step_hook = std_step;
emitter.spawn = function(t)
{
emitter.spawn = function (t) {
t ??= this.transform;
var par = this.dead.shift();
if (par) {
par.body.pos = t.pos;
par.transform.scale = [this.scale,this.scale,this.scale];
par.transform.scale = [this.scale, this.scale, this.scale];
this.particles[par.id] = par;
par.time = 0;
this.spawn_hook?.(par);
@ -56,25 +50,24 @@ emitter.spawn = function(t)
transform: os.make_transform(),
life: this.life,
time: 0,
color: this.color
color: this.color,
};
par.body = os.make_body(par.transform);
par.body.pos = t.pos;
par.transform.scale = [this.scale,this.scale,this.scale];
par.transform.scale = [this.scale, this.scale, this.scale];
par.id = prosperon.guid();
this.particles[par.id] = par;
this.spawn_hook(par);
}
};
emitter.step = function(dt)
{
emitter.step = function (dt) {
// update spawning particles
if (this.on && this.pps > 0) {
this.spawn_timer += dt;
var pp = 1/this.pps;
var pp = 1 / this.pps;
while (this.spawn_timer > pp) {
this.spawn_timer -= pp;
this.spawn();
@ -92,16 +85,15 @@ emitter.step = function(dt)
delete this.particles[p.id];
}
}
}
};
emitter.burst = function(count, t) {
emitter.burst = function (count, t) {
for (var i = 0; i < count; i++) this.spawn(t);
}
};
var emitters = [];
var make_emitter = function()
{
var make_emitter = function () {
var e = Object.create(emitter);
e.ssbo = render.make_textssbo();
e.shape = shape.centered_quad;
@ -109,17 +101,14 @@ var make_emitter = function()
e.dead = [];
emitters.push(e);
return e;
};
function update_emitters(dt) {
for (var e of emitters) e.step(dt);
}
function update_emitters(dt)
{
for (var e of emitters)
e.step(dt);
}
function draw_emitters()
{
function draw_emitters() {
for (var e of emitters) e.draw();
}
return {make_emitter, update_emitters, draw_emitters};
return { make_emitter, update_emitters, draw_emitters };

View file

@ -9,27 +9,25 @@ var HIT = {
};
*/
physics.pos_query = function(pos, start = world, give = 10) {
physics.pos_query = function (pos, start = world, give = 10) {
var ret;
ret = physics.point_query_nearest(pos, 0);
if (ret)
return ret.entity;
if (ret) return ret.entity;
return game.all_objects(function(o) {
return game.all_objects(function (o) {
var dist = Vector.length(o.pos.sub(pos));
if (dist <= give) return o;
});
}
};
physics.box_point_query = function(box,points) {
physics.box_point_query = function (box, points) {
if (!box || !points) return [];
var bbox = bbox.fromcwh(box.pos,box.wh);
var bbox = bbox.fromcwh(box.pos, box.wh);
var inside = [];
for (var i in points)
if (bbox.pointin(bbox,points[i])) inside.push[i];
for (var i in points) if (bbox.pointin(bbox, points[i])) inside.push[i];
return inside;
}
};
Object.assign(physics, {
dynamic: 0,
@ -37,8 +35,8 @@ Object.assign(physics, {
static: 2,
com(pos) {
if (!Array.isArray(pos)) return [0,0];
return pos.reduce((a,i) => a.add(i)).map(g => g/pos.length);
if (!Array.isArray(pos)) return [0, 0];
return pos.reduce((a, i) => a.add(i)).map(g => g / pos.length);
},
});
@ -53,8 +51,8 @@ physics.gravity.strength = 500;
physics.damp = physics.make_damp();
physics.damp.mask = ~1;
physics.delta = 1/240;
physics.delta = 1 / 240;
return {
physics
}
physics,
};

View file

@ -10,47 +10,48 @@
memory - can see how much memory is allocated and from where
*/
var t_units = [ "ns", "us", "ms", "s", "ks", "Ms" ];
var t_units = ["ns", "us", "ms", "s", "ks", "Ms"];
function calc_cpu (fn, times, diff = 0) {
function calc_cpu(fn, times, diff = 0) {
var series = [];
for (var i = 0; i < times; i++) {
var st = profile.now();
fn (i);
fn(i);
series.push(profile.now() - st - diff);
}
return series;
}
function empty_fn () {}
function empty_fn() {}
profile.cpu = function profile_cpu(fn, times = 1, q = "unnamed") {
var retgather = gathering_cpu;
profile.gather_stop();
var empty = calc_cpu(empty_fn, 100000);
var mean = Math.mean(empty);
var series = calc_cpu(fn,times, mean);
var series = calc_cpu(fn, times, mean);
var elapsed = Math.sum(series);
var avgt = profile.best_t(elapsed/series.length);
var avgt = profile.best_t(elapsed / series.length);
var totalt = profile.best_t(elapsed);
say(`profile [${q}]: ${avgt} ± ${profile.best_t(Math.ci(series))} [${totalt} for ${times} loops]`);
say(`result of function is ${fn()}`);
if (retgather)
profile.start_prof_gather();
}
profile.ms = function(t) { return profile.secs(t)*1000; }
if (retgather) profile.start_prof_gather();
};
profile.ms = function (t) {
return profile.secs(t) * 1000;
};
var callgraph = {};
profile.rawstacks = {};
profile.cpu_cg = callgraph;
function add_callgraph (fn, line, time) {
function add_callgraph(fn, line, time) {
var cc = callgraph[line];
if (!cc) {
var cc = {};
@ -70,87 +71,82 @@ var start_gather = profile.now();
profile.cpu_start = undefined;
profile.clear_cpu = function()
{
profile.clear_cpu = function () {
callgraph = {};
}
};
profile.start_cpu_gather = function(gathertime = 5) // gather cpu frames for 'time' seconds
{
profile.start_cpu_gather = function (gathertime = 5) {
// gather cpu frames for 'time' seconds
if (profile.cpu_start) return;
profile.cpu_start = profile.now();
var st = profile.cpu_start;
profile.gather(hittar, function() {
var time = profile.now()-st;
profile.gather(hittar, function () {
var time = profile.now() - st;
var err = new Error();
var stack = err.stack.split("\n").slice(1);
var rawstack = stack.join('\n');
var stack = err.stack.split("\n").slice(1);
var rawstack = stack.join("\n");
profile.rawstacks[rawstack] ??= {
time: 0,
hits: 0
hits: 0,
};
profile.rawstacks[rawstack].hits++;
profile.rawstacks[rawstack].time += time;
stack = stack.map(x => x.slice(7).split(' '));
var fns = stack.map(x => x[0]).filter(x=>x);
stack = stack.map(x => x.slice(7).split(" "));
var fns = stack.map(x => x[0]).filter(x => x);
var lines = stack.map(x => x[1]).filter(x => x);
lines = lines.map(x => x.slice(1,x.length-1));
for (var i = 0; i < fns.length; i++)
add_callgraph(fns[i], lines[i], time);
lines = lines.map(x => x.slice(1, x.length - 1));
for (var i = 0; i < fns.length; i++) add_callgraph(fns[i], lines[i], time);
st = profile.now();
if (profile.secs(st-profile.cpu_start) < gathertime)
profile.gather_rate(Math.variate(hittar,hitpct));
if (profile.secs(st - profile.cpu_start) < gathertime) profile.gather_rate(Math.variate(hittar, hitpct));
else {
profile.gather_stop();
profile.cpu_start = undefined;
var e = Object.values(callgraph);
e = e.sort((a,b) => {
e = e.sort((a, b) => {
if (a.time > b.time) return -1;
return 1;
});
return 1;
});
for (var x of e) {
var ffs = x.line.split(':');
x.timestr = profile.best_t(x.time);
var pct = profile.secs(x.time)/gathertime*100;
x.timeper = x.time / x.hits;
x.timeperstr = profile.best_t(x.timeper);
x.fncall = get_line(ffs[0], ffs[1]);
x.log =`${x.line}::${x.fn}:: ${x.timestr} (${pct.toPrecision(3)}%) (${x.hits} hits) --> ${get_line(ffs[0], ffs[1])}`;
}
for (var x of e) {
var ffs = x.line.split(":");
x.timestr = profile.best_t(x.time);
var pct = (profile.secs(x.time) / gathertime) * 100;
x.timeper = x.time / x.hits;
x.timeperstr = profile.best_t(x.timeper);
x.fncall = get_line(ffs[0], ffs[1]);
x.log = `${x.line}::${x.fn}:: ${x.timestr} (${pct.toPrecision(3)}%) (${x.hits} hits) --> ${get_line(ffs[0], ffs[1])}`;
}
profile.cpu_instr = e;
profile.cpu_instr = e;
}
});
}
};
function push_time(arr, ob, max)
{
function push_time(arr, ob, max) {
arr.push({
time:profile.now(),
ob
time: profile.now(),
ob,
});
}
profile.cpu_frames = [];
profile.last_cpu_frame = undefined;
profile.cpu_frame = function()
{
profile.gather(Math.random_range(300,600), function() {
profile.cpu_frame = function () {
profile.gather(Math.random_range(300, 600), function () {
var err = new Error();
profile.last_cpu_frame = err.stack;//.split('\n').slicconsole.stack(2);
profile.last_cpu_frame = err.stack; //.split('\n').slicconsole.stack(2);
profile.gather_stop();
});
}
};
var filecache = {};
function get_line (file, line) {
function get_line(file, line) {
var text = filecache[file];
if (!text) {
var f = io.slurp(file);
@ -158,17 +154,19 @@ function get_line (file, line) {
filecache[file] = "undefined";
return filecache[file];
}
filecache[file] = io.slurp(file).split('\n');
filecache[file] = io.slurp(file).split("\n");
text = filecache[file];
}
if (typeof text === 'string') return text;
text = text[Number (line) - 1];
if (typeof text === "string") return text;
text = text[Number(line) - 1];
if (!text) return "NULL";
return text.trim();
}
profile.stop_cpu_instr = function () { return; }
profile.stop_cpu_instr = function () {
return;
};
profile.best_t = function (t) {
var qq = 0;
@ -193,26 +191,23 @@ profile.report = function (start, msg = "[undefined report]") {
var frame_avg = false;
profile.frame_avg_t = 72000;
profile.start_frame_avg = function()
{
profile.start_frame_avg = function () {
if (frame_avg) return;
profile_frames = {};
profile_frame_ts = [];
profile_cframe = profile_frames;
pframe = 0;
frame_avg = true;
}
};
profile.stop_frame_avg = function()
{
profile.stop_frame_avg = function () {
frame_avg = false;
}
};
profile.toggle_frame_avg = function()
{
profile.toggle_frame_avg = function () {
if (frame_avg) profile.stop_frame_avg();
else profile.start_frame_avg();
}
};
var profile_framer = {
series: [],
@ -223,52 +218,46 @@ var profile_cframe = undefined;
var pframe = 0;
var profile_stack = [];
profile.frame = function profile_frame(title)
{
profile.frame = function profile_frame(title) {
if (profile.cpu_start) return;
if (!frame_avg) return;
if (!profile_cframe) {
profile_cframe = {};
profile_framer.series.push({
time:profile.now(),
data:profile_cframe
time: profile.now(),
data: profile_cframe,
});
} else
profile_stack.push(profile_cframe);
} else profile_stack.push(profile_cframe);
profile_cframe[title] ??= {};
profile_cframe = profile_cframe[title];
profile_cframe.time = profile.now();
}
};
profile.endframe = function profile_endframe()
{
profile.endframe = function profile_endframe() {
if (!frame_avg) return;
profile_cframe.time = profile.now() - profile_cframe.time;
profile_cframe = profile_frame_ts.pop();
}
};
var print_frame = function(frame, indent, title)
{
var print_frame = function (frame, indent, title) {
say(indent + `${title} ::::: ${profile.best_t(Math.mean(frame._times))} ± ${profile.best_t(Math.ci(frame._times))} (${frame._times.length} hits)`);
for (var i in frame) {
if (i === '_times') continue;
if (i === "_times") continue;
print_frame(frame[i], indent + " ", i);
}
}
};
profile.print_frame_avg = function()
{
profile.print_frame_avg = function () {
say("===FRAME AVERAGES===\n");
var indent = "";
for (var i in profile_frames)
print_frame(profile_frames[i], "", 'frame');
for (var i in profile_frames) print_frame(profile_frames[i], "", "frame");
say("\n");
}
};
/*
Cache reporting is to measure how long specific events take, that are NOT every frame
@ -283,47 +272,48 @@ var cachest = 0;
var cachegroup;
var cachetitle;
profile.cache_reporting = function() { return cache_reporting; }
profile.cache_toggle = function() { cache_reporting = !cache_reporting; }
profile.cache_dump = function() {
profile.cache_reporting = function () {
return cache_reporting;
};
profile.cache_toggle = function () {
cache_reporting = !cache_reporting;
};
profile.cache_dump = function () {
report_cache = {};
}
};
profile.cache = function profile_cache(group, title)
{
profile.cache = function profile_cache(group, title) {
if (!cache_reporting) return;
cachest = profile.now();
cachegroup = group;
cachetitle = title;
}
};
profile.endcache = function profile_endcache(tag = "")
{
profile.endcache = function profile_endcache(tag = "") {
addreport(cachegroup, cachetitle + tag, cachest);
}
};
function addreport(group, line, start) {
if (typeof group !== 'string') group = 'UNGROUPED';
if (typeof group !== "string") group = "UNGROUPED";
report_cache[group] ??= {};
var cache = report_cache[group];
cache[line] ??= [];
var t = profile.now();
cache[line].push(t - start);
return t;
};
}
function printreport (cache, name) {
var report = `==${name}==` +
"\n";
function printreport(cache, name) {
var report = `==${name}==` + "\n";
var reports = [];
for (var i in cache) {
var time = cache[i].reduce((a, b) => a + b);
reports.push({
time : time,
name : i,
hits : cache[i].length,
avg : time / cache[i].length
time: time,
name: i,
hits: cache[i].length,
avg: time / cache[i].length,
});
}
reports = reports.sort((a, b) => {
@ -331,47 +321,40 @@ function printreport (cache, name) {
return -1;
});
for (var rep of reports)
report += `${rep.name} ${profile.best_t(rep.avg)} (${
rep.hits} hits) (total ${profile.best_t(rep.time)})\n`;
for (var rep of reports) report += `${rep.name} ${profile.best_t(rep.avg)} (${rep.hits} hits) (total ${profile.best_t(rep.time)})\n`;
return report;
};
}
profile.data = {};
profile.curframe = 0;
function prof_add_stats (obj, stat) {
function prof_add_stats(obj, stat) {
for (var i in stat) {
obj[i] ??= [];
if (obj[i].last() !== stat[i]) obj[i][profile.curframe] = stat[i];
}
}
profile.pushdata = function(arr, val)
{
if (arr.last() !== val)
arr[profile.curframe] = val;
}
profile.pushdata = function (arr, val) {
if (arr.last() !== val) arr[profile.curframe] = val;
};
profile.capture_data = function()
{
profile.capture_data = function () {
prof_add_stats(profile.data.memory, os.mem());
prof_add_stats(profile.data.gfx, imgui.framestats());
prof_add_stats(profile.data.actors, actor.__stats());
profile.curframe++;
}
};
profile.best_mem = function(bytes)
{
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
profile.best_mem = function (bytes) {
var sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes == 0) return "0 Bytes";
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return (bytes / Math.pow(1024, i)).toPrecision(3) + ' ' + sizes[i];
}
return (bytes / Math.pow(1024, i)).toPrecision(3) + " " + sizes[i];
};
profile.cleardata = function()
{
profile.cleardata = function () {
profile.data.gpu = {};
profile.data.physics = {};
profile.data.script = {};
@ -383,19 +366,18 @@ profile.cleardata = function()
render: [],
physics: [],
};
}
};
profile.cleardata();
profile.last_mem = undefined;
profile.mems = [];
profile.gcs = [];
profile.print_gc = function()
{
profile.print_gc = function () {
var gc = os.check_gc();
if (!gc) return;
profile.data.gc ??= [];
profile.data.gc[profile.curframe] = gc;
}
};
return {profile};
return { profile };

View file

@ -2,14 +2,14 @@ globalThis.gamestate = {};
global.check_registers = function (obj) {
for (var reg in Register.registries) {
if (typeof obj[reg] === 'function') {
if (typeof obj[reg] === "function") {
var fn = obj[reg].bind(obj);
fn.layer = obj[reg].layer;
var name = obj.ur ? obj.ur.name : obj.toString();
obj.timers.push(Register.registries[reg].register(fn, name));
}
}
for (var k in obj) {
if (!k.startsWith("on_")) continue;
var signal = k.fromfirst("on_");
@ -70,7 +70,7 @@ game.engine_start = function (s) {
prosperon.camera = prosperon.make_camera();
var camera = prosperon.camera;
camera.transform.pos = [0,0,-100];
camera.transform.pos = [0, 0, -100];
camera.mode = "keep";
camera.break = "fit";
camera.size = game.size;
@ -87,11 +87,11 @@ game.engine_start = function (s) {
var appcam = prosperon.appcam;
appcam.near = 0;
appcam.size = window.size;
appcam.transform.pos = [window.size.x,window.size.y,-100];
appcam.transform.pos = [window.size.x, window.size.y, -100];
prosperon.screencolor = render.screencolor();
globalThis.imgui = render.imgui_init();
s();
shape.quad = {
@ -109,13 +109,13 @@ game.engine_start = function (s) {
count: 3,
index: os.make_buffer([0, 2, 1], 1),
};
shape.centered_quad = {
pos: os.make_buffer([-0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5], 0),
verts: 4,
uv: os.make_buffer([0,1,1,1,0,0,1,0],2),
index: os.make_buffer([0,1,2,2,1,3],1),
count: 6
uv: os.make_buffer([0, 1, 1, 1, 0, 0, 1, 0], 2),
index: os.make_buffer([0, 1, 2, 2, 1, 3], 1),
count: 6,
};
render.init();
@ -128,12 +128,11 @@ game.engine_start = function (s) {
game.startengine = 0;
prosperon.release_mode = function()
{
prosperon.release_mode = function () {
prosperon.debug = false;
mum.debug = false;
debug.kill();
}
};
prosperon.debug = true;
game.timescale = 1;
@ -142,8 +141,7 @@ var eachobj = function (obj, fn) {
var val = fn(obj);
if (val) return val;
for (var o in obj.objects) {
if (obj.objects[o] === obj)
console.error(`Object ${obj.toString()} is referenced by itself.`);
if (obj.objects[o] === obj) console.error(`Object ${obj.toString()} is referenced by itself.`);
val = eachobj(obj.objects[o], fn);
if (val) return val;
}
@ -179,8 +177,7 @@ game.doc.pause = "Pause game simulation.";
game.doc.play = "Resume or start game simulation.";
game.doc.camera = "Current camera.";
game.tex_hotreload = function()
{
game.tex_hotreload = function () {
for (var path in game.texture.cache) {
if (io.mod(path) > game.texture.time_cache[path]) {
var tex = game.texture.cache[path];
@ -188,17 +185,17 @@ game.tex_hotreload = function()
os.texture_swap(path, game.texture.cache[path]);
for (var sprite of Object.values(allsprites)) {
if (sprite.texture == tex) {
sprite.tex_sync();
}
sprite.tex_sync();
}
}
}
}
}
};
game.texture = function (path) {
if (!path) return game.texture("icons/no_text.gif");
path = Resources.find_image(path);
if (!io.exists(path)) {
console.error(`Missing texture: ${path}`);
game.texture.cache[path] = game.texture("icons/no_tex.gif");
@ -229,8 +226,7 @@ prosperon.semver.valid = function (v, range) {
if (range[0] === "~") {
range[0] = range[0].slice(1);
for (var i = 0; i < 2; i++)
if (parseInt(v[i]) < parseInt(range[i])) return false;
for (var i = 0; i < 2; i++) if (parseInt(v[i]) < parseInt(range[i])) return false;
return true;
}
@ -251,8 +247,7 @@ prosperon.semver.cmp = function (v1, v2) {
return 0;
};
prosperon.semver.cmp.doc =
"Compare two semantic version numbers, given like X.X.X.";
prosperon.semver.cmp.doc = "Compare two semantic version numbers, given like X.X.X.";
prosperon.semver.valid.doc = `Test if semantic version v is valid, given a range.
Range is given by a semantic versioning number, prefixed with nothing, a ~, or a ^.
~ means that MAJOR and MINOR must match exactly, but any PATCH greater or equal is valid.
@ -277,7 +272,9 @@ prosperon.quit = function () {
window.size = [640, 480];
window.mode = "keep";
window.toggle_fullscreen = function() { window.fullscreen = !window.fullscreen; }
window.toggle_fullscreen = function () {
window.fullscreen = !window.fullscreen;
};
window.set_icon.doc = "Set the icon of the window using the PNG image at path.";
@ -285,14 +282,13 @@ window.doc = {};
window.doc.dimensions = "Window width and height packaged in an array [width,height]";
window.doc.title = "Name in the title bar of the window.";
window.doc.boundingbox = "Boundingbox of the window, with top and right being its height and width.";
window.__proto__.toJSON = function()
{
window.__proto__.toJSON = function () {
return {
size: this.size,
fullscreen: this.fullscreen,
title: this.title
};
}
title: this.title,
};
};
global.mixin("scripts/input");
global.mixin("scripts/std");
@ -302,7 +298,7 @@ global.mixin("scripts/tween");
global.mixin("scripts/ai");
global.mixin("scripts/particle");
global.mixin("scripts/physics");
global.mixin("scripts/geometry")
global.mixin("scripts/geometry");
/*
Factory for creating registries. Register one with 'X.register',
@ -317,44 +313,43 @@ var Register = {
n.register = function (fn, oname) {
if (!(fn instanceof Function)) return;
var guid = prosperon.guid();
var dofn = function(...args) {
profile.cache(name,oname);
var dofn = function (...args) {
profile.cache(name, oname);
var st = profile.now();
fn(...args);
profile.endcache();
}
profile.endcache();
};
fns.push(dofn);
dofn.layer = fn.layer;
dofn.layer ??= 0;
fns.sort((a,b) => a.layer > b.layer);
fns.sort((a, b) => a.layer > b.layer);
return function () {
fns.remove(dofn);
};
};
if (!flush) {
prosperon[name] = function(...args) {
prosperon[name] = function (...args) {
fns.forEach(fn => fn(...args));
}
}
else
prosperon[name] = function(...args) {
};
} else
prosperon[name] = function (...args) {
var layer = undefined;
for (var fn of fns) {
if (layer !== fn.layer) {
flush();
layer = fn.layer;
}
fn();
if (layer !== fn.layer) {
flush();
layer = fn.layer;
}
fn();
}
}
};
prosperon[name].fns = fns;
n.clear = function () {
fns = [];
@ -384,11 +379,11 @@ var Event = {
},
unobserve(name, obj) {
this.events[name] = this.events[name].filter((x) => x[0] !== obj);
this.events[name] = this.events[name].filter(x => x[0] !== obj);
},
rm_obj(obj) {
Object.keys(this.events).forEach((name) => Event.unobserve(name, obj));
Object.keys(this.events).forEach(name => Event.unobserve(name, obj));
},
notify(name, ...args) {
@ -435,5 +430,5 @@ return {
sim,
frame_t,
physlag,
Event
}
Event,
};

File diff suppressed because it is too large Load diff

View file

@ -3,13 +3,13 @@ var repl = {};
var file = "repl.jj";
var last = 0;
repl.hotreload = function() {
repl.hotreload = function () {
if (io.mod(file) > last) {
say("REPL:::");
last = io.mod(file);
var script = io.slurp(file);
eval(script);
}
}
};
return {repl:repl};
return { repl: repl };

View file

@ -1,12 +1,12 @@
/* This file runs after the audio system is initiated */
Object.readonly(audio, 'samplerate');
Object.readonly(audio, 'channels');
Object.readonly(audio, 'buffer_frames');
Object.readonly(audio, "samplerate");
Object.readonly(audio, "channels");
Object.readonly(audio, "buffer_frames");
var sources = [];
audio.play = function(file,bus = audio.bus.master) {
audio.play = function (file, bus = audio.bus.master) {
var filename = file;
file = Resources.find_sound(file);
if (!file) {
@ -20,7 +20,7 @@ audio.play = function(file,bus = audio.bus.master) {
src.type = "source";
sources.push(src);
return src;
}
};
audio.bus = {};
audio.bus.master = dspsound.master();
audio.dsp = {};
@ -30,47 +30,45 @@ audio.bus.master.__proto__.type = "bus";
audio.bus.master.name = "master";
var plugin_node = audio.bus.master.plugin;
audio.bus.master.__proto__.plugin = function(to)
{
audio.bus.master.__proto__.plugin = function (to) {
this.tos ??= [];
this.tos.push(to);
to.ins ??= [];
to.ins.push(this);
plugin_node.call(this, to);
}
};
var unplug_node = audio.bus.master.unplug;
audio.bus.master.__proto__.unplug = function()
{
audio.bus.master.__proto__.unplug = function () {
if (this.tos) {
for (var node of this.tos)
node.ins.remove(this);
for (var node of this.tos) node.ins.remove(this);
this.tos = [];
}
unplug_node.call(this);
}
audio.dsp.mix().__proto__.imgui = function()
{
unplug_node.call(this);
};
audio.dsp.mix().__proto__.imgui = function () {
imgui.pushid(this.memid());
this.volume = imgui.slider("Volume", this.volume);
this.off = imgui.checkbox("Mute", this.off);
imgui.popid();
}
};
audio.cry = function(file, bus = audio.bus.sfx)
{
audio.cry = function (file, bus = audio.bus.sfx) {
file = Resources.find_sound(file);
var player = audio.play(file, bus);
if (!player) return;
player.ended = function() { player.unplug(); player = undefined; }
player.ended = function () {
player.unplug();
player = undefined;
};
return player.ended;
}
};
// This function is called when every audio source is finished
var killer = Register.appupdate.register(function() {
var killer = Register.appupdate.register(function () {
for (var src of sources) {
if (!src.loop && (src.frame < src.lastframe || src.frame === src.frames())) {
src.unplug();
@ -83,10 +81,9 @@ var killer = Register.appupdate.register(function() {
var song;
// Play 'file' for new song, cross fade for seconds
audio.music = function(file, fade = 0.5) {
audio.music = function (file, fade = 0.5) {
if (!file) {
if (song)
song.volume = 0;
if (song) song.volume = 0;
return;
}
file = Resources.find_sound(file);
@ -99,20 +96,20 @@ audio.music = function(file, fade = 0.5) {
if (!song) {
song = audio.play(file, audio.bus.music);
song.volume = 1;
// tween(song,'volume', 1, fade);
// tween(song,'volume', 1, fade);
return;
}
var temp = audio.play(file, audio.bus.music);
if (!temp) return;
temp.volume = 1;
var temp2 = song;
// tween(temp, 'volume', 1, fade);
// tween(temp2, 'volume', 0, fade);
// tween(temp, 'volume', 1, fade);
// tween(temp2, 'volume', 0, fade);
song = temp;
song.loop = true;
}
};
audio.bus.music = audio.dsp.mix();
audio.bus.music.plugin(audio.bus.master);
@ -122,16 +119,16 @@ audio.bus.sfx = audio.dsp.mix();
audio.bus.sfx.plugin(audio.bus.master);
audio.bus.sfx.name = "sfx";
audio.dsp.allpass = function(secs, decay) {
audio.dsp.allpass = function (secs, decay) {
var composite = {};
var fwd = audio.dsp.fwd_delay(secs,-decay);
var fbk = audio.dsp.delay(secs,decay);
var fwd = audio.dsp.fwd_delay(secs, -decay);
var fbk = audio.dsp.delay(secs, decay);
composite.id = fwd.id;
composite.plugin = composite.plugin.bind(fbk);
composite.unplug = dsp_node.unplug.bind(fbk);
fwd.plugin(fbk);
return composite;
}
};
audio.dsp.doc = {
delay: "Delays the input by secs, multiplied by decay",
@ -146,27 +143,35 @@ audio.dsp.doc = {
pitchshift: "Shift sound by octaves",
noise: "Plain randon noise",
pink: "Pink noise",
red: "Red noise"
red: "Red noise",
};
audio.dsp.obscure('doc');
audio.dsp.obscure("doc");
Object.mixin(audio.bus.master.__proto__, {
get db() { return 20*Math.log10(Math.abs(this.volume)); },
set db(x) { x = Math.clamp(x,-100,0); this.volume = Math.pow(10, x/20); },
get volume() { return this.gain; },
set volume(x) { this.gain = x; },
get db() {
return 20 * Math.log10(Math.abs(this.volume));
},
set db(x) {
x = Math.clamp(x, -100, 0);
this.volume = Math.pow(10, x / 20);
},
get volume() {
return this.gain;
},
set volume(x) {
this.gain = x;
},
});
audio.bus.master.__proto__.toJSON = function()
{
audio.bus.master.__proto__.toJSON = function () {
return {
volume: this.volume,
off: this.off,
pan: this.pan,
pass: this.pass
pass: this.pass,
};
}
};
/*Object.mixin(audio.dsp.source().__proto__, {
length() { return this.frames()/audio.samplerate(); },
@ -175,4 +180,4 @@ audio.bus.master.__proto__.toJSON = function()
});
*/
return {audio};
return { audio };

View file

@ -1,117 +1,121 @@
var Spline = {};
Spline.sample_angle = function(type, points, angle) {
Spline.sample_angle = function (type, points, angle) {
if (type === 0) return spline.catmull(points, angle);
else if (type === 1) return spline.bezier(points,angle);
else if (type === 1) return spline.bezier(points, angle);
return undefined;
}
};
Spline.bezier_loop = function(cp)
{
cp.push(Vector.reflect_point(cp.at(-2),cp.at(-1)));
cp.push(Vector.reflect_point(cp[1],cp[0]));
Spline.bezier_loop = function (cp) {
cp.push(Vector.reflect_point(cp.at(-2), cp.at(-1)));
cp.push(Vector.reflect_point(cp[1], cp[0]));
cp.push(cp[0].slice());
return cp;
}
};
Spline.bezier_node_count = function(cp)
{
Spline.bezier_node_count = function (cp) {
if (cp.length === 4) return 2;
return 2 + (cp.length-4)/3;
}
return 2 + (cp.length - 4) / 3;
};
Spline.is_bezier = function(t) { return t === Spline.type.bezier; }
Spline.is_catmull = function(t) { return t === Spline.type.catmull; }
Spline.is_bezier = function (t) {
return t === Spline.type.bezier;
};
Spline.is_catmull = function (t) {
return t === Spline.type.catmull;
};
Spline.bezier2catmull = function(b)
{
Spline.bezier2catmull = function (b) {
var c = [];
for (var i = 0; i < b.length; i += 3)
c.push(b[i]);
for (var i = 0; i < b.length; i += 3) c.push(b[i]);
return c;
}
};
Spline.catmull2bezier = function(c)
{
Spline.catmull2bezier = function (c) {
var b = [];
for (var i = 1; i < c.length-2; i++) {
for (var i = 1; i < c.length - 2; i++) {
b.push(c[i].slice());
b.push(c[i+1].sub(c[i-1]).scale(0.25).add(c[i]));
b.push(c[i].sub(c[i+2]).scale(0.25).add(c[i+1]));
b.push(
c[i + 1]
.sub(c[i - 1])
.scale(0.25)
.add(c[i]),
);
b.push(
c[i]
.sub(c[i + 2])
.scale(0.25)
.add(c[i + 1]),
);
}
b.push(c[c.length-2]);
b.push(c[c.length - 2]);
return b;
}
};
Spline.catmull_loop = function(cp)
{
Spline.catmull_loop = function (cp) {
cp = cp.slice();
cp.unshift(cp.last());
cp.push(cp[1]);
cp.push(cp[2]);
return cp;
}
};
Spline.catmull_caps = function(cp)
{
Spline.catmull_caps = function (cp) {
if (cp.length < 2) return;
cp = cp.slice();
cp.unshift(cp[0].sub(cp[1]).add(cp[0]));
cp.push(cp.last().sub(cp.at(-2).add(cp.last())));
return cp;
}
};
Spline.catmull_caps.doc = "Given a set of control points cp, return the necessary caps added to the spline.";
Spline.catmull2bezier.doc = "Given a set of control points C for a camtull-rom type curve, return a set of cubic bezier points to give the same curve."
Spline.catmull2bezier.doc = "Given a set of control points C for a camtull-rom type curve, return a set of cubic bezier points to give the same curve.";
Spline.type = {
catmull: 0,
bezier: 1,
bspline: 2,
cubichermite: 3
cubichermite: 3,
};
Spline.bezier_tan_partner = function(points, i)
{
if (i%3 === 0) return undefined;
var partner_i = (i%3) === 2 ? i-1 : i+1;
Spline.bezier_tan_partner = function (points, i) {
if (i % 3 === 0) return undefined;
var partner_i = i % 3 === 2 ? i - 1 : i + 1;
return points[i];
}
};
Spline.bezier_cp_mirror = function(points, i)
{
if (i%3 === 0) return undefined;
var partner_i = (i%3) === 2 ? i+2 : i-2;
var node_i = (i%3) === 2 ? i+1 : i-1;
Spline.bezier_cp_mirror = function (points, i) {
if (i % 3 === 0) return undefined;
var partner_i = i % 3 === 2 ? i + 2 : i - 2;
var node_i = i % 3 === 2 ? i + 1 : i - 1;
if (partner_i >= points.length || node_i >= points.length) return;
points[partner_i] = points[node_i].sub(points[i]).add(points[node_i]);
}
};
Spline.bezier_point_handles = function(points, i)
{
if (!Spline.bezier_is_node(points,i)) return [];
var a = i-1;
var b = i+1;
var c = []
if (a > 0)
c.push(a);
if (b < points.length)
c.push(b);
return c;
}
Spline.bezier_nodes = function(points)
{
Spline.bezier_point_handles = function (points, i) {
if (!Spline.bezier_is_node(points, i)) return [];
var a = i - 1;
var b = i + 1;
var c = [];
for (var i = 0; i < points.length; i+=3)
c.push(points[i].slice());
if (a > 0) c.push(a);
if (b < points.length) c.push(b);
return c;
}
};
Spline.bezier_is_node = function(points, i) { return i%3 === 0; }
Spline.bezier_is_handle = function(points, i) { return !Spline.bezier_is_node(points,i); }
Spline.bezier_nodes = function (points) {
var c = [];
for (var i = 0; i < points.length; i += 3) c.push(points[i].slice());
return {Spline};
return c;
};
Spline.bezier_is_node = function (points, i) {
return i % 3 === 0;
};
Spline.bezier_is_handle = function (points, i) {
return !Spline.bezier_is_node(points, i);
};
return { Spline };

View file

@ -1,28 +1,34 @@
os.cwd.doc = "Get the absolute path of the current working directory.";
os.env.doc = "Return the value of the environment variable v.";
os.platform = "steam";
if (os.sys() === 'windows')
os.user = os.env("USERNAME");
else
os.user = os.env("USER");
if (os.sys() === "windows") os.user = os.env("USERNAME");
else os.user = os.env("USER");
var appy = {};
appy.inputs = {};
if (os.sys() === 'macos') {
appy.inputs['S-q'] = os.quit;
if (os.sys() === "macos") {
appy.inputs["S-q"] = os.quit;
}
appy.inputs.f7 = function() { debug.meta = !debug.meta; }
appy.inputs.f8 = function() { debug.cheat = !debug.cheat; }
appy.inputs.f9 = function() { debug.console = !debug.console; }
appy.inputs.f10 = function() { debug.show = !debug.show; }
appy.inputs.f7 = function () {
debug.meta = !debug.meta;
};
appy.inputs.f8 = function () {
debug.cheat = !debug.cheat;
};
appy.inputs.f9 = function () {
debug.console = !debug.console;
};
appy.inputs.f10 = function () {
debug.show = !debug.show;
};
appy.inputs.f11 = window.toggle_fullscreen;
appy.inputs.f11.doc = "Toggle window fullscreen.";
appy.inputs.f11.title = "Toggle Fullscreen";
appy.inputs['M-f4'] = os.quit;
appy.inputs["M-f4"] = os.quit;
player[0].control(appy);
//steam.appid = 480;
//steam.userid = 8437843;
@ -35,30 +41,32 @@ os.home = os.env("HOME");
};
*/
var otherpath = {
windows:`C:/Users/${os.user}/Saved Games`,
windows: `C:/Users/${os.user}/Saved Games`,
macos: `${os.home}/Library/Application Support`,
linux: `${os.home}/.local/share`
}
linux: `${os.home}/.local/share`,
};
os.prefpath = function() {
os.prefpath = function () {
return otherpath[os.sys()] + "/" + (game.title ? game.title : "Untitled Prosperon Game");
}
};
os.openurl = function(url) {
if (os.sys() === 'windows')
os.system(`start ${url}`);
else
os.system(`open ${url}`);
}
os.openurl = function (url) {
if (os.sys() === "windows") os.system(`start ${url}`);
else os.system(`open ${url}`);
};
var projectfile = "project.prosperon";
io.dumpfolder = '.prosperon';
io.dumpfolder = ".prosperon";
Resources.texture = {};
Resources.texture.dimensions = function(path) { texture.dimensions(path); }
Resources.texture.dimensions = function (path) {
texture.dimensions(path);
};
Resources.gif = {};
Resources.gif.frames = function(path) { return render.gif_frames(path); }
Resources.gif.frames = function (path) {
return render.gif_frames(path);
};
/*
io path rules. Starts with, meaning:
@ -68,30 +76,27 @@ Resources.gif.frames = function(path) { return render.gif_frames(path); }
*/
var tmpchm = io.chmod;
io.chmod = function(file,mode) {
return tmpchm(file,parseInt(mode,8));
}
io.chmod = function (file, mode) {
return tmpchm(file, parseInt(mode, 8));
};
var tmpslurp = io.slurp;
io.slurp = function(path)
{
io.slurp = function (path) {
path = Resources.replpath(path);
return tmpslurp(path);
}
};
var tmpslurpb = io.slurpbytes;
io.slurpbytes = function(path)
{
io.slurpbytes = function (path) {
path = Resources.replpath(path);
return tmpslurpb(path);
}
};
io.mkpath = function(dir)
{
io.mkpath = function (dir) {
if (!dir) return;
var mkstack = [];
while (!io.exists(dir)) {
mkstack.push(dir.fromlast('/'));
mkstack.push(dir.fromlast("/"));
dir = dir.dir();
}
for (var d of mkstack) {
@ -99,55 +104,54 @@ io.mkpath = function(dir)
say(`making ${dir}`);
io.mkdir(dir);
}
}
};
var tmpslurpw = io.slurpwrite;
io.slurpwrite = function(path, c)
{
io.slurpwrite = function (path, c) {
path = Resources.replpath(path);
io.mkpath(path.dir());
return tmpslurpw(path, c);
}
};
var tmpcp = io.cp;
io.cp = function(f1,f2)
{
io.cp = function (f1, f2) {
io.mkpath(f2.dir());
tmpcp(f1,f2);
}
tmpcp(f1, f2);
};
var tmprm = io.rm;
io.rm = function(f)
{
io.rm = function (f) {
tmprm(Resources.replpath(f));
}
};
io.globToRegex = function(glob) {
// Escape special regex characters
// Replace glob characters with regex equivalents
let regexStr = glob
.replace(/[\.\\]/g, '\\$&') // Escape literal backslashes and dots
.replace(/([^\*])\*/g, '$1[^/]*') // * matches any number of characters except /
.replace(/\*\*/g, '.*') // ** matches any number of characters, including none
.replace(/\[(.*?)\]/g, '[$1]') // Character sets
.replace(/\?/g, '.'); // ? matches any single character
io.globToRegex = function (glob) {
// Escape special regex characters
// Replace glob characters with regex equivalents
let regexStr = glob
.replace(/[\.\\]/g, "\\$&") // Escape literal backslashes and dots
.replace(/([^\*])\*/g, "$1[^/]*") // * matches any number of characters except /
.replace(/\*\*/g, ".*") // ** matches any number of characters, including none
.replace(/\[(.*?)\]/g, "[$1]") // Character sets
.replace(/\?/g, "."); // ? matches any single character
// Ensure the regex matches the whole string
regexStr = '^' + regexStr + '$';
// Ensure the regex matches the whole string
regexStr = "^" + regexStr + "$";
// Create and return the regex object
return new RegExp(regexStr);
}
// Create and return the regex object
return new RegExp(regexStr);
};
io.mixin({
extensions(ext) {
var paths = io.ls();
paths = paths.filter(function(str) { return str.ext() === ext; });
paths = paths.filter(function (str) {
return str.ext() === ext;
});
return paths;
},
glob(pat) {
var paths = io.ls('.');
var paths = io.ls(".");
var regex = io.globToRegex(pat);
paths = paths.filter(str => str.match(regex)).sort();
return paths;
@ -172,328 +176,409 @@ var Cmdline = {};
Cmdline.cmds = [];
Cmdline.orders = {};
Cmdline.register_cmd = function(flag, fn, doc) {
Cmdline.register_cmd = function (flag, fn, doc) {
Cmdline.cmds.push({
flag: flag,
fn: fn,
doc: doc
doc: doc,
});
};
Cmdline.register_order = function(order, fn, doc, usage = "") {
Cmdline.register_order = function (order, fn, doc, usage = "") {
Cmdline.orders[order] = fn;
fn.doc = doc;
fn.usage = `${order} ${usage}`;
}
};
Cmdline.register_order("edit", function() {
if (!io.exists(projectfile)) {
say("No game to edit. Try making one with 'prosperon init'.");
return;
}
window.size = [1280, 720];
window.mode = "full";
sim.pause();
game.engine_start(function() {
global.mixin("scripts/editor.js");
use("editorconfig.js");
use("config.js");
render.set_font("fonts/c64.ttf", 8);
editor.enter_editor();
});
}, "Edit the project in this folder. Give it the name of an UR to edit that specific object.", "?UR?");
Cmdline.register_order("init", function() {
if (io.exists(projectfile)) {
say("Already a game here.");
return;
}
io.mkdir(io.dumpfolder);
var project = {};
project.version = prosperon.version;
project.revision = prosperon.revision;
io.slurpwrite(projectfile, json.encode(project));
}, "Turn the directory into a Prosperon game.");
Cmdline.register_order("debug", function() {
Cmdline.orders.play([]);
}, "Play the game with debugging enabled.");
Cmdline.register_order("web", function() {
Cmdline.orders.play([]);
}, "Play the game in a web browser.");
Cmdline.register_order("play", function(argv) {
if (argv[0])
io.chdir(argv[0]);
// game.loadurs();
if (!io.exists(projectfile)) {
say("No game to play. Try making one with 'prosperon init'.");
return;
}
var project = json.decode(io.slurp(projectfile));
game.title = project.title;
game.size = [1280,720];
window.size = game.size;
if (io.exists("config.js"))
global.mixin("config.js");
else
console.warn('No config.js file found. Starting with default parameters.');
if (project.title) window.title = project.title;
game.engine_start(function() {
render.set_font("fonts/c64.ttf", 8);
if (io.exists("game.js"))
global.app = actor.spawn("game.js");
else
global.app = actor.spawn("scripts/nogame.js");
if (project.icon) window.set_icon(game.texture(project.icon));
game.camera = world.spawn("scripts/camera2d");
});
}, "Play the game present in this folder.");
Cmdline.register_order("pack", function(str) {
var packname;
if (str.length === 0)
packname = "game.zip";
else if (str.length > 1) {
console.warn("Give me a single filename for the pack.");
return;
} else
packname = str[0];
say(`Packing into ${packname}`);
io.pack_start(packname);
var files = io.ls('.');
files = files.filter(f => !f.startsWith('.git'));
files = files.filter(f => !f.startsWith('.nova'));
files = files.filter(f => !f.includes('.DS_Store'));
files = files.filter(f => !f.startsWith('.gitignore'));
say(files);
for (var f of files)
io.pack_add(f);
io.pack_end();
}, "Pack the game into the given name.", "NAME");
Cmdline.register_order("cdb", function(argv) {
var cdb = "game.zip";
if (!io.exists(cdb)) {
say(`No 'game.zip' present.`);
return;
}
if (argv.length === 0) {
say(`cdb name: ${cdb}`);
}
}, "CDB commands.");
Cmdline.register_order("qoa", function(argv) {
var sounds = Resources.sounds.filter(x => x !== "qoa");
for (var file of argv) {
if (!sounds.includes(file.ext())) continue;
say(`converting ${file}`);
io.save_qoa(file);
}
}, "Convert file(s) to qoa.");
Cmdline.register_order("about", function(argv) {
if (!argv[0]) {
say('About your game');
say(`Prosperon version ${prosperon.version}`);
say(`Total entities ${ur._list.length}`);
}
switch (argv[0]) {
case "entities":
for (var i of ur._list) say(i);
break;
}
}, "Get information about this game.");
Cmdline.register_order("ur", function(argv) {
// game.loadurs();
for (var i of ur._list.sort()) say(i);
}, "Get information about the ur types in your game.");
Cmdline.register_order("env", function(argv) {
if (argv.length > 2) return;
var gg = json.decode(io.slurp(projectfile));
if (argv.length === 0) {
say(json.encode(gg,null,1));
return;
}
if (argv.length === 1) {
var v = gg[argv[0]];
if (!v) {
say(`Value ${argv[0]} not found.`);
Cmdline.register_order(
"edit",
function () {
if (!io.exists(projectfile)) {
say("No game to edit. Try making one with 'prosperon init'.");
return;
}
say(`${argv[0]}:${v}`);
} else {
gg[argv[0]] = argv[1];
say(`Set ${argv[0]}:${v}`);
say(json.encode(gg,null,1));
io.slurpwrite(projectfile, json.encode(gg));
}
}, "Get or set game variables.");
Cmdline.register_order("unpack", function() {
say("Unpacking not implemented.");
}, "Unpack this binary's contents into this folder for editing.");
window.size = [1280, 720];
window.mode = "full";
sim.pause();
Cmdline.register_order("build", function() {
say("Building not implemented.");
}, "Build static assets for this project.");
game.engine_start(function () {
global.mixin("scripts/editor.js");
use("editorconfig.js");
use("config.js");
render.set_font("fonts/c64.ttf", 8);
editor.enter_editor();
});
},
"Edit the project in this folder. Give it the name of an UR to edit that specific object.",
"?UR?",
);
Cmdline.register_order("nota", function(argv) {
for (var file of argv) {
if (!io.exists(file)) {
say(`File ${file} does not exist.`);
continue;
Cmdline.register_order(
"init",
function () {
if (io.exists(projectfile)) {
say("Already a game here.");
return;
}
var obj = json.decode(io.slurp(file));
var nn = nota.encode(obj);
io.slurpwrite(file.set_ext(".nota"), nn);
}
}, "Create a nota file from a json.");
io.mkdir(io.dumpfolder);
var project = {};
project.version = prosperon.version;
project.revision = prosperon.revision;
io.slurpwrite(projectfile, json.encode(project));
},
"Turn the directory into a Prosperon game.",
);
Cmdline.register_order("json", function(argv) {
for (var file of argv) {
if (!io.exists(file)) {
say(`File ${file} does not exist.`);
continue;
Cmdline.register_order(
"debug",
function () {
Cmdline.orders.play([]);
},
"Play the game with debugging enabled.",
);
Cmdline.register_order(
"web",
function () {
Cmdline.orders.play([]);
},
"Play the game in a web browser.",
);
Cmdline.register_order(
"play",
function (argv) {
if (argv[0]) io.chdir(argv[0]);
// game.loadurs();
if (!io.exists(projectfile)) {
say("No game to play. Try making one with 'prosperon init'.");
return;
}
say(file.ext());
var obj = nota.decode(io.slurp(file));
var nn = json.encode(obj);
io.slurpwrite(file.set_ext(".json", nn));
}
}, "Create a JSON from a nota.");
Cmdline.register_order("api", function(obj) {
if (!obj[0]) {
Cmdline.print_order("api");
return;
}
var project = json.decode(io.slurp(projectfile));
game.title = project.title;
game.size = [1280, 720];
window.size = game.size;
if (io.exists("config.js")) global.mixin("config.js");
else console.warn("No config.js file found. Starting with default parameters.");
use("scripts/editor.js");
var api = debug.api.print_doc(obj[0]);
if (!api)
return;
if (project.title) window.title = project.title;
say(api);
}, "Print the API for an object as markdown. Give it a file to save the output to.", "OBJECT");
game.engine_start(function () {
render.set_font("fonts/c64.ttf", 8);
if (io.exists("game.js")) global.app = actor.spawn("game.js");
else global.app = actor.spawn("scripts/nogame.js");
Cmdline.register_order("input", function(pawn) {
use("scripts/editor.js");
say(`## Input for ${pawn}`);
eval(`say(input.print_md_kbm(${pawn}));`);
}, "Print input documentation for a given object as markdown. Give it a file to save the output to", "OBJECT ?FILE?");
if (project.icon) window.set_icon(game.texture(project.icon));
game.camera = world.spawn("scripts/camera2d");
});
},
"Play the game present in this folder.",
);
Cmdline.register_order("run", function(script) {
script = script.join(" ");
if (!script) {
say("Need something to run.");
return;
}
say(eval(script));
}, "Run a given script. SCRIPT can be the script itself, or a file containing the script", "SCRIPT");
Cmdline.register_order(
"pack",
function (str) {
var packname;
if (str.length === 0) packname = "game.zip";
else if (str.length > 1) {
console.warn("Give me a single filename for the pack.");
return;
} else packname = str[0];
say(`Packing into ${packname}`);
io.pack_start(packname);
var files = io.ls(".");
files = files.filter(f => !f.startsWith(".git"));
files = files.filter(f => !f.startsWith(".nova"));
files = files.filter(f => !f.includes(".DS_Store"));
files = files.filter(f => !f.startsWith(".gitignore"));
say(files);
for (var f of files) io.pack_add(f);
io.pack_end();
},
"Pack the game into the given name.",
"NAME",
);
Cmdline.register_order(
"cdb",
function (argv) {
var cdb = "game.zip";
if (!io.exists(cdb)) {
say(`No 'game.zip' present.`);
return;
}
if (argv.length === 0) {
say(`cdb name: ${cdb}`);
}
},
"CDB commands.",
);
Cmdline.register_order(
"qoa",
function (argv) {
var sounds = Resources.sounds.filter(x => x !== "qoa");
for (var file of argv) {
if (!sounds.includes(file.ext())) continue;
say(`converting ${file}`);
io.save_qoa(file);
}
},
"Convert file(s) to qoa.",
);
Cmdline.register_order(
"about",
function (argv) {
if (!argv[0]) {
say("About your game");
say(`Prosperon version ${prosperon.version}`);
say(`Total entities ${ur._list.length}`);
}
switch (argv[0]) {
case "entities":
for (var i of ur._list) say(i);
break;
}
},
"Get information about this game.",
);
Cmdline.register_order(
"ur",
function (argv) {
// game.loadurs();
for (var i of ur._list.sort()) say(i);
},
"Get information about the ur types in your game.",
);
Cmdline.register_order(
"env",
function (argv) {
if (argv.length > 2) return;
var gg = json.decode(io.slurp(projectfile));
if (argv.length === 0) {
say(json.encode(gg, null, 1));
return;
}
if (argv.length === 1) {
var v = gg[argv[0]];
if (!v) {
say(`Value ${argv[0]} not found.`);
return;
}
say(`${argv[0]}:${v}`);
} else {
gg[argv[0]] = argv[1];
say(`Set ${argv[0]}:${v}`);
say(json.encode(gg, null, 1));
io.slurpwrite(projectfile, json.encode(gg));
}
},
"Get or set game variables.",
);
Cmdline.register_order(
"unpack",
function () {
say("Unpacking not implemented.");
},
"Unpack this binary's contents into this folder for editing.",
);
Cmdline.register_order(
"build",
function () {
say("Building not implemented.");
},
"Build static assets for this project.",
);
Cmdline.register_order(
"nota",
function (argv) {
for (var file of argv) {
if (!io.exists(file)) {
say(`File ${file} does not exist.`);
continue;
}
var obj = json.decode(io.slurp(file));
var nn = nota.encode(obj);
io.slurpwrite(file.set_ext(".nota"), nn);
}
},
"Create a nota file from a json.",
);
Cmdline.register_order(
"json",
function (argv) {
for (var file of argv) {
if (!io.exists(file)) {
say(`File ${file} does not exist.`);
continue;
}
say(file.ext());
var obj = nota.decode(io.slurp(file));
var nn = json.encode(obj);
io.slurpwrite(file.set_ext(".json", nn));
}
},
"Create a JSON from a nota.",
);
Cmdline.register_order(
"api",
function (obj) {
if (!obj[0]) {
Cmdline.print_order("api");
return;
}
use("scripts/editor.js");
var api = debug.api.print_doc(obj[0]);
if (!api) return;
say(api);
},
"Print the API for an object as markdown. Give it a file to save the output to.",
"OBJECT",
);
Cmdline.register_order(
"input",
function (pawn) {
use("scripts/editor.js");
say(`## Input for ${pawn}`);
eval(`say(input.print_md_kbm(${pawn}));`);
},
"Print input documentation for a given object as markdown. Give it a file to save the output to",
"OBJECT ?FILE?",
);
Cmdline.register_order(
"run",
function (script) {
script = script.join(" ");
if (!script) {
say("Need something to run.");
return;
}
say(eval(script));
},
"Run a given script. SCRIPT can be the script itself, or a file containing the script",
"SCRIPT",
);
Cmdline.orders.script = Cmdline.orders.run;
Cmdline.print_order = function(fn)
{
if (typeof fn === 'string')
fn = Cmdline.orders[fn];
Cmdline.print_order = function (fn) {
if (typeof fn === "string") fn = Cmdline.orders[fn];
if (!fn) return;
say(`Usage: prosperon ${fn.usage}`);
say(fn.doc);
}
};
Cmdline.register_order("help", function(order) {
if (!Object.empty(order)) {
var orfn = Cmdline.orders[order];
if (!orfn) {
console.warn(`No command named ${order}.`);
Cmdline.register_order(
"help",
function (order) {
if (!Object.empty(order)) {
var orfn = Cmdline.orders[order];
if (!orfn) {
console.warn(`No command named ${order}.`);
return;
}
Cmdline.print_order(orfn);
return;
}
Cmdline.print_order(orfn);
return;
}
Cmdline.print_order("help");
Cmdline.print_order("help");
for (var cmd of Object.keys(Cmdline.orders).sort())
say(cmd);
for (var cmd of Object.keys(Cmdline.orders).sort()) say(cmd);
Cmdline.orders.version();
}, "Give help with a specific command.", "TOPIC");
Cmdline.orders.version();
},
"Give help with a specific command.",
"TOPIC",
);
Cmdline.register_order("version", function() {
say(`Prosperon version ${prosperon.version} [${prosperon.revision}]`);
}, "Display Prosperon info.");
Cmdline.register_order(
"version",
function () {
say(`Prosperon version ${prosperon.version} [${prosperon.revision}]`);
},
"Display Prosperon info.",
);
function cmd_args(cmdargs)
{
function cmd_args(cmdargs) {
var play = false;
var cmds = cmdargs.split(/\s+/).slice(1);
if (cmds.length === 0)
cmds[0] = "play";
if (cmds.length === 0) cmds[0] = "play";
else if (!Cmdline.orders[cmds[0]]) {
console.warn(`Command ${cmds[0]} not found. Playing instead.`);
cmds[0] = "play";
}
Cmdline.orders[cmds[0]](cmds.slice(1));
if (!game.startengine)
os.exit(0);
if (!game.startengine) os.exit(0);
}
Cmdline.register_order("clean", function(argv) {
say("Cleaning not implemented.");
}, "Clean up a given object file.", "JSON ...");
Cmdline.register_order(
"clean",
function (argv) {
say("Cleaning not implemented.");
},
"Clean up a given object file.",
"JSON ...",
);
Cmdline.register_order("test", function(argv) {
use("scripts/test.js");
}, "Run tests.");
Cmdline.register_order(
"test",
function (argv) {
use("scripts/test.js");
},
"Run tests.",
);
Cmdline.register_cmd("l", function(n) {
console.level = n;
}, "Set log level.");
Cmdline.register_cmd(
"l",
function (n) {
console.level = n;
},
"Set log level.",
);
function convertYAMLtoJSON(yamlString) {
const lines = yamlString.split('\n');
const lines = yamlString.split("\n");
const jsonObj = {};
let currentKey = '';
let currentValue = '';
let currentKey = "";
let currentValue = "";
let currentDepth = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line || line.startsWith('#')) {
if (!line || line.startsWith("#")) {
continue;
}
const depth = (line.match(/^\s+/g) || [''])[0].length;
const keyValue = line.split(':');
const depth = (line.match(/^\s+/g) || [""])[0].length;
const keyValue = line.split(":");
const key = keyValue[0].trim();
const value = keyValue[1].trim();
@ -507,8 +592,8 @@ function convertYAMLtoJSON(yamlString) {
currentValue = value;
} else {
jsonObj[currentKey] = convertYAMLtoJSON(currentValue);
currentKey = '';
currentValue = '';
currentKey = "";
currentValue = "";
i--; // To reprocess the current line with updated values
}
@ -526,6 +611,5 @@ return {
Resources,
Cmdline,
cmd_args,
convertYAMLtoJSON
convertYAMLtoJSON,
};

View file

@ -1,4 +1,4 @@
var aa = [1,2,3,4,5,6,7,8,9,10];
var aa = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/*
var ta = [1,2,3,4,5,6,7,8,9,10];

View file

@ -2,9 +2,5 @@ test.run("set transform scale", _ => {
var t = os.make_transform();
var s1 = t.scale.slice();
t.scale = s1;
return (t.scale.equal(s1));
return t.scale.equal(s1);
});

View file

@ -7,20 +7,16 @@ var pass = 0;
var fail = 0;
var failed = [];
test.run_suite = function(file)
{
test.run_suite = function (file) {
test = [];
pass = 0;
fail = 0;
failed = [];
};
}
test.run = function(name, fn)
{
var func = function() {
print(`${pass+fail+1}/${tests.length}: ${name} ... `);
test.run = function (name, fn) {
var func = function () {
print(`${pass + fail + 1}/${tests.length}: ${name} ... `);
var p = profile.now();
var b = fn();
p = profile.lap(p);
@ -29,12 +25,11 @@ test.run = function(name, fn)
};
func.testname = name;
tests.push(func);
}
};
say(`Testing ${tests.length} tests.`);
for (var t of tests) {
if (t())
pass++;
if (t()) pass++;
else {
fail++;
failed.push(t.testname);
@ -42,11 +37,10 @@ for (var t of tests) {
print("\n");
}
say(`Passed ${pass} tests and failed ${fail} [${(pass*100/(pass+fail)).toPrecision(4)}%].`);
say(`Passed ${pass} tests and failed ${fail} [${((pass * 100) / (pass + fail)).toPrecision(4)}%].`);
say(`Failed tests are:`);
for (var f of failed)
say(f);
for (var f of failed) say(f);
os.quit();
return {test};
return { test };

View file

@ -1,14 +1,14 @@
var texteditor = Object.copy(inputpanel, {
title: "text editor",
wh: [700,500],
_cursor:0, /* Text cursor: [char,line] */
get cursor() { return this._cursor; },
wh: [700, 500],
_cursor: 0 /* Text cursor: [char,line] */,
get cursor() {
return this._cursor;
},
set cursor(x) {
if (x > this.value.length)
x = this.value.length;
if (x < 0)
x = 0;
if (x > this.value.length) x = this.value.length;
if (x < 0) x = 0;
this._cursor = x;
this.line = this.get_line();
},
@ -31,31 +31,27 @@ var texteditor = Object.copy(inputpanel, {
},
copy(start, end) {
return this.value.slice(start,end);
return this.value.slice(start, end);
},
delete_line(p) {
var ls = this.line_start(p);
var le = this.line_end(p)+1;
this.cut_span(ls,le);
var le = this.line_end(p) + 1;
this.cut_span(ls, le);
this.to_line_start();
},
line_blank(p) {
var ls = this.line_start(p);
var le = this.line_end(p);
var line = this.value.slice(ls, le);
if (line.search(/[^\s]/g) === -1)
return true;
else
return false;
var line = this.value.slice(ls, le);
if (line.search(/[^\s]/g) === -1) return true;
else return false;
},
get_line() {
var line = 0;
for (var i = 0; i < this.cursor; i++)
if (this.value[i] === "\n")
line++;
for (var i = 0; i < this.cursor; i++) if (this.value[i] === "\n") line++;
return line;
},
@ -72,14 +68,11 @@ var texteditor = Object.copy(inputpanel, {
src: "NEW FILE",
guibody() {
return [
Mum.text({str:`EDITING ${this.src}`}),
Mum.text({str:this.value, caret:this.cursor, offset:[0,-16]}),
];
return [Mum.text({ str: `EDITING ${this.src}` }), Mum.text({ str: this.value, caret: this.cursor, offset: [0, -16] })];
},
insert_char(char) {
this.value = this.value.slice(0,this.cursor) + char + this.value.slice(this.cursor);
this.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor);
this.cursor++;
},
@ -98,117 +91,115 @@ var texteditor = Object.copy(inputpanel, {
cut_span(start, end) {
if (end < start) return;
this.savestate();
var ret = this.value.slice(start,end);
this.value = this.value.slice(0,start) + this.value.slice(end);
if (start > this.cursor)
return ret;
var ret = this.value.slice(start, end);
this.value = this.value.slice(0, start) + this.value.slice(end);
if (start > this.cursor) return ret;
this.cursor -= ret.length;
return ret;
},
next_word(pos) {
var v = this.value.slice(pos+1).search(/[^\w]\w/g);
var v = this.value.slice(pos + 1).search(/[^\w]\w/g);
if (v === -1) return pos;
return pos + v + 2;
},
prev_word(pos) {
while (this.value.slice(pos,pos+2).search(/[^\w]\w/g) === -1 && pos > 0)
pos--;
while (this.value.slice(pos, pos + 2).search(/[^\w]\w/g) === -1 && pos > 0) pos--;
return pos+1;
return pos + 1;
},
end_of_word(pos) {
var l = this.value.slice(pos).search(/\w[^\w]/g);
return l+pos;
return l + pos;
},
get inset() {
return this.cursor - this.value.prev('\n', this.cursor) - 1;
return this.cursor - this.value.prev("\n", this.cursor) - 1;
},
line_start(p) {
return this.value.prev('\n', p)+1;
return this.value.prev("\n", p) + 1;
},
line_end(p) {
return this.value.next('\n', p);
return this.value.next("\n", p);
},
next_line(p) {
return this.value.next('\n',p)+1;
return this.value.next("\n", p) + 1;
},
prev_line(p) {
return this.line_start(this.value.prev('\n', p));
return this.line_start(this.value.prev("\n", p));
},
to_line_start() {
this.cursor = this.value.prev('\n', this.cursor)+1;
this.cursor = this.value.prev("\n", this.cursor) + 1;
},
to_line_end() {
var p = this.value.next('\n', this.cursor);
if (p === -1)
this.to_file_end();
else
this.cursor = p;
var p = this.value.next("\n", this.cursor);
if (p === -1) this.to_file_end();
else this.cursor = p;
},
line_width(pos) {
var start = this.line_start(pos);
var end = this.line_end(pos);
if (end === -1)
end = this.value.length;
if (end === -1) end = this.value.length;
return end-start;
return end - start;
},
to_file_end() { this.cursor = this.value.length; },
to_file_end() {
this.cursor = this.value.length;
},
to_file_start() { this.cursor = 0; },
to_file_start() {
this.cursor = 0;
},
desired_inset: 0,
});
texteditor.inputs = {};
texteditor.inputs.char = function(char) {
(texteditor.inputs.char = function (char) {
this.insert_char(char);
this.keycb();
},
}),
(texteditor.inputs.enter = function () {
var white = this.line_starting_whitespace(this.cursor);
this.insert_char("\n");
texteditor.inputs.enter = function(){
var white = this.line_starting_whitespace(this.cursor);
this.insert_char('\n');
for (var i = 0; i < white; i++)
this.insert_char(" ");
};
for (var i = 0; i < white; i++) this.insert_char(" ");
});
texteditor.inputs.enter.rep = true;
texteditor.inputs.backspace = function(){
this.value = this.value.slice(0,this.cursor-1) + this.value.slice(this.cursor);
texteditor.inputs.backspace = function () {
this.value = this.value.slice(0, this.cursor - 1) + this.value.slice(this.cursor);
this.cursor--;
};
texteditor.inputs.backspace.rep = true;
texteditor.inputs['C-s'] = function() {
if (this.srctype === 'function') {
texteditor.inputs["C-s"] = function () {
if (this.srctype === "function") {
eval(`${this.src} = ${this.value}`);
}
};
texteditor.inputs['C-s'].doc = "Save edited text.";
texteditor.inputs["C-s"].doc = "Save edited text.";
texteditor.inputs['C-u'] = function() { this.popstate(); };
texteditor.inputs['C-u'].doc = "Undo.";
texteditor.inputs["C-u"] = function () {
this.popstate();
};
texteditor.inputs["C-u"].doc = "Undo.";
texteditor.inputs['C-q'] = function() {
texteditor.inputs["C-q"] = function () {
var ws = this.prev_word(this.cursor);
var we = this.end_of_word(this.cursor)+1;
var we = this.end_of_word(this.cursor) + 1;
var find = this.copy(ws, we);
var obj = editor.edit_level.varname2obj(find);
@ -217,109 +208,109 @@ texteditor.inputs['C-q'] = function() {
editor.selectlist.push(obj);
}
};
texteditor.inputs['C-q'].doc = "Select object of selected word.";
texteditor.inputs["C-q"].doc = "Select object of selected word.";
texteditor.inputs['C-o'] = function() {
this.insert_char('\n');
texteditor.inputs["C-o"] = function () {
this.insert_char("\n");
this.cursor--;
};
texteditor.inputs['C-o'].doc = "Insert newline.";
texteditor.inputs['C-o'].rep = true;
texteditor.inputs["C-o"].doc = "Insert newline.";
texteditor.inputs["C-o"].rep = true;
texteditor.inputs['M-o'] = function() {
while (this.line_blank(this.next_line(this.cursor)))
this.delete_line(this.next_line(this.cursor));
texteditor.inputs["M-o"] = function () {
while (this.line_blank(this.next_line(this.cursor))) this.delete_line(this.next_line(this.cursor));
while (this.line_blank(this.prev_line(this.cursor)))
this.delete_line(this.prev_line(this.cursor));
while (this.line_blank(this.prev_line(this.cursor))) this.delete_line(this.prev_line(this.cursor));
};
texteditor.inputs['M-o'].doc = "Delete surround blank lines.";
texteditor.inputs["M-o"].doc = "Delete surround blank lines.";
texteditor.inputs['C-d'] = function () { this.value = this.value.slice(0,this.cursor) + this.value.slice(this.cursor+1); };
texteditor.inputs['C-d'].doc = "Delete character.";
texteditor.inputs["C-d"] = function () {
this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + 1);
};
texteditor.inputs["C-d"].doc = "Delete character.";
texteditor.inputs['M-d'] = function() { this.cut_span(this.cursor, this.end_of_word(this.cursor)+1); };
texteditor.inputs['M-d'].doc = "Delete word.";
texteditor.inputs["M-d"] = function () {
this.cut_span(this.cursor, this.end_of_word(this.cursor) + 1);
};
texteditor.inputs["M-d"].doc = "Delete word.";
texteditor.inputs['C-a'] = function() {
texteditor.inputs["C-a"] = function () {
this.to_line_start();
this.desired_inset = this.inset;
};
texteditor.inputs['C-a'].doc = "To start of line.";
texteditor.inputs["C-a"].doc = "To start of line.";
texteditor.inputs['C-y'] = function() {
texteditor.inputs["C-y"] = function () {
if (this.killring.length === 0) return;
this.insert_char(this.killring.pop());
};
texteditor.inputs['C-y'].doc = "Insert from killring.";
texteditor.inputs["C-y"].doc = "Insert from killring.";
texteditor.inputs['C-e'] = function() {
texteditor.inputs["C-e"] = function () {
this.to_line_end();
this.desired_inset = this.inset;
};
texteditor.inputs['C-e'].doc = "To line end.";
texteditor.inputs["C-e"].doc = "To line end.";
texteditor.inputs['C-k'] = function() {
if (this.cursor === this.value.length-1) return;
var killamt = this.value.next('\n', this.cursor) - this.cursor;
var killed = this.cut_span(this.cursor-1, this.cursor+killamt);
texteditor.inputs["C-k"] = function () {
if (this.cursor === this.value.length - 1) return;
var killamt = this.value.next("\n", this.cursor) - this.cursor;
var killed = this.cut_span(this.cursor - 1, this.cursor + killamt);
this.killring.push(killed);
};
texteditor.inputs['C-k'].doc = "Kill from cursor to end of line.";
texteditor.inputs["C-k"].doc = "Kill from cursor to end of line.";
texteditor.inputs['M-k'] = function() {
var prevn = this.value.prev('\n', this.cursor);
texteditor.inputs["M-k"] = function () {
var prevn = this.value.prev("\n", this.cursor);
var killamt = this.cursor - prevn;
var killed = this.cut_span(prevn+1, prevn+killamt);
var killed = this.cut_span(prevn + 1, prevn + killamt);
this.killring.push(killed);
this.to_line_start();
};
texteditor.inputs['M-k'].doc = "Kill entire line the cursor is on.";
texteditor.inputs["M-k"].doc = "Kill entire line the cursor is on.";
texteditor.inputs['C-b'] = function() {
texteditor.inputs["C-b"] = function () {
this.cursor--;
this.desired_inset = this.inset;
};
texteditor.inputs['C-b'].rep = true;
texteditor.inputs['M-b'] = function() {
this.cursor = this.prev_word(this.cursor-2);
texteditor.inputs["C-b"].rep = true;
texteditor.inputs["M-b"] = function () {
this.cursor = this.prev_word(this.cursor - 2);
this.desired_inset = this.inset;
};
texteditor.inputs['M-b'].rep = true;
texteditor.inputs["M-b"].rep = true;
texteditor.inputs['C-f'] = function() {
texteditor.inputs["C-f"] = function () {
this.cursor++;
this.desired_inset = this.inset;
};
texteditor.inputs['C-f'].rep = true;
texteditor.inputs['M-f'] = function() {
texteditor.inputs["C-f"].rep = true;
texteditor.inputs["M-f"] = function () {
this.cursor = this.next_word(this.cursor);
this.desired_inset = this.inset;
};
texteditor.inputs['M-f'].rep = true;
texteditor.inputs["M-f"].rep = true;
texteditor.inputs['C-p'] = function() {
texteditor.inputs["C-p"] = function () {
if (this.cursor === 0) return;
this.desired_inset = Math.max(this.desired_inset, this.inset);
this.cursor = this.prev_line(this.cursor);
var newlinew = this.line_width(this.cursor);
this.cursor += Math.min(this.desired_inset, newlinew);
};
texteditor.inputs['C-p'].rep = true;
texteditor.inputs["C-p"].rep = true;
texteditor.inputs['M-p'] = function() {
while (this.line_blank(this.cursor))
this.cursor = this.prev_line(this.cursor);
texteditor.inputs["M-p"] = function () {
while (this.line_blank(this.cursor)) this.cursor = this.prev_line(this.cursor);
while (!this.line_blank(this.cursor))
this.cursor = this.prev_line(this.cursor);
while (!this.line_blank(this.cursor)) this.cursor = this.prev_line(this.cursor);
};
texteditor.inputs['M-p'].doc = "Go up to next line with text on it.";
texteditor.inputs['M-p'].rep = true;
texteditor.inputs["M-p"].doc = "Go up to next line with text on it.";
texteditor.inputs["M-p"].rep = true;
texteditor.inputs['C-n'] = function() {
if (this.cursor === this.value.length-1) return;
if (this.value.next('\n', this.cursor) === -1) {
texteditor.inputs["C-n"] = function () {
if (this.cursor === this.value.length - 1) return;
if (this.value.next("\n", this.cursor) === -1) {
this.to_file_end();
return;
}
@ -329,20 +320,16 @@ texteditor.inputs['C-n'] = function() {
var newlinew = this.line_width(this.cursor);
this.cursor += Math.min(this.desired_inset, newlinew);
};
texteditor.inputs['C-n'].rep = true;
texteditor.inputs["C-n"].rep = true;
texteditor.inputs['M-n'] = function() {
while (this.line_blank(this.cursor))
this.cursor = this.next_line(this.cursor);
while (!this.line_blank(this.cursor))
this.cursor = this.next_line(this.cursor);
texteditor.inputs["M-n"] = function () {
while (this.line_blank(this.cursor)) this.cursor = this.next_line(this.cursor);
while (!this.line_blank(this.cursor)) this.cursor = this.next_line(this.cursor);
};
texteditor.inputs['M-n'].doc = "Go down to next line with text on it.";
texteditor.inputs['M-n'].rep = true;
texteditor.inputs["M-n"].doc = "Go down to next line with text on it.";
texteditor.inputs["M-n"].rep = true;
texteditor.open_fn = function(fnstr)
{
texteditor.open_fn = function (fnstr) {
var fn = eval(fnstr);
if (!fn) {
console.warn(`${fnstr} is not a function.`);
@ -353,4 +340,4 @@ texteditor.open_fn = function(fnstr)
editor.openpanel(this);
this.value = fn;
this.cursor = 0;
}
};

View file

@ -1,40 +1,43 @@
/* Take numbers from 0 to 1 and remap them to easing functions */
var Ease = {
linear(t) { return t; },
linear(t) {
return t;
},
in(t) { return t*t; },
in(t) {
return t * t;
},
out(t) {
var d = 1-t;
return 1 - d*d
var d = 1 - t;
return 1 - d * d;
},
inout(t) {
var d = -2*t + 2;
var d = -2 * t + 2;
return t < 0.5 ? 2 * t * t : 1 - (d * d) / 2;
},
};
function make_easing_fns(num) {
var obj = {};
obj.in = function(t) {
return Math.pow(t,num);
obj.in = function (t) {
return Math.pow(t, num);
};
obj.out = function(t) {
obj.out = function (t) {
return 1 - Math.pow(1 - t, num);
};
var mult = Math.pow(2, num-1);
var mult = Math.pow(2, num - 1);
obj.inout = function(t) {
obj.inout = function (t) {
return t < 0.5 ? mult * Math.pow(t, num) : 1 - Math.pow(-2 * t + 2, num) / 2;
};
return obj;
};
}
Ease.quad = make_easing_fns(2);
Ease.cubic = make_easing_fns(3);
@ -52,7 +55,7 @@ Ease.expo = {
inout(t) {
return t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2;
}
},
};
Ease.bounce = {
@ -64,52 +67,57 @@ Ease.bounce = {
var n1 = 7.5625;
var d1 = 2.75;
if (t < 1 / d1) { return n1 * t * t; }
else if (t < 2 / d1) { return n1 * (t -= 1.5 / d1) * t + 0.75; }
else if (t < 2.5 / d1) { return n1 * (t -= 2.25 / d1) * t + 0.9375; }
else
return n1 * (t -= 2.625 / d1) * t + 0.984375;
if (t < 1 / d1) {
return n1 * t * t;
} else if (t < 2 / d1) {
return n1 * (t -= 1.5 / d1) * t + 0.75;
} else if (t < 2.5 / d1) {
return n1 * (t -= 2.25 / d1) * t + 0.9375;
} else return n1 * (t -= 2.625 / d1) * t + 0.984375;
},
inout(t) {
return t < 0.5 ? (1 - this.out(1 - 2 * t)) / 2 : (1 + this.out(2 * t - 1)) / 2;
}
},
};
Ease.sine = {
in(t) { return 1 - Math.cos((t * Math.PI)/2); },
in(t) {
return 1 - Math.cos((t * Math.PI) / 2);
},
out(t) { return Math.sin((t*Math.PI)/2); },
out(t) {
return Math.sin((t * Math.PI) / 2);
},
inout(t) { return -(Math.cos(Math.PI*t) - 1) / 2; }
inout(t) {
return -(Math.cos(Math.PI * t) - 1) / 2;
},
};
Ease.elastic = {
in(t) {
return t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10*t-10) * Math.sin((t * 10 - 10.75) * this.c4);
return t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * this.c4);
},
out(t) {
return t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10*t) * Math.sin((t * 10 - 0.75) * this.c4) + 1;
return t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * this.c4) + 1;
},
inout(t) {
t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ?
-(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2
: (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 + 1;
t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ? -(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 : (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 + 1;
},
};
Ease.elastic.c4 = 2*Math.PI/3;
Ease.elastic.c5 = 2*Math.PI / 4.5;
Ease.elastic.c4 = (2 * Math.PI) / 3;
Ease.elastic.c5 = (2 * Math.PI) / 4.5;
var tween = function(from, to, time, fn, endfn)
{
var tween = function (from, to, time, fn, endfn) {
var start = profile.secs(profile.now());
var update = function(dt) {
var update = function (dt) {
profile.frame("tween");
var elapsed = profile.secs(profile.now()) - start;
fn(from.lerp(to,elapsed/time));
fn(from.lerp(to, elapsed / time));
if (elapsed >= time) {
fn(to);
if (stop.then) stop.then();
@ -120,7 +128,7 @@ var tween = function(from, to, time, fn, endfn)
};
var stop = Register.update.register(update);
return stop;
}
};
var Tween = {
default: {
@ -133,22 +141,19 @@ var Tween = {
yoyo: go up and then back down
circle: go up and back down, looped
*/
time: 1, /* seconds to do */
time: 1 /* seconds to do */,
ease: Ease.linear,
whole: true, /* True if time is for the entire tween, false if each stage */
cb: function(){},
whole: true /* True if time is for the entire tween, false if each stage */,
cb: function () {},
},
start(obj, target, tvals, options)
{
start(obj, target, tvals, options) {
var defn = Object.create(this.default);
Object.assign(defn, options);
if (defn.loop === 'circle')
tvals.push(tvals[0]);
else if (defn.loop === 'yoyo') {
for (var i = tvals.length-2; i >= 0; i--)
tvals.push(tvals[i]);
if (defn.loop === "circle") tvals.push(tvals[0]);
else if (defn.loop === "yoyo") {
for (var i = tvals.length - 2; i >= 0; i--) tvals.push(tvals[i]);
}
defn.accum = 0;
@ -156,22 +161,19 @@ var Tween = {
var slices = tvals.length - 1;
var slicelen = 1 / slices;
defn.fn = function(dt) {
defn.fn = function (dt) {
defn.accum += dt;
if (defn.accum >= defn.time && defn.loop === 'hold') {
if (typeof target === 'string')
obj[target] = tvals[tvals.length-1];
else
target(tvals[tvals.length-1]);
if (defn.accum >= defn.time && defn.loop === "hold") {
if (typeof target === "string") obj[target] = tvals[tvals.length - 1];
else target(tvals[tvals.length - 1]);
defn.pause();
defn.cb.call(obj);
return;
defn.cb.call(obj);
return;
}
defn.pct = (defn.accum % defn.time) / defn.time;
if (defn.loop === 'none' && defn.accum >= defn.time)
defn.stop();
if (defn.loop === "none" && defn.accum >= defn.time) defn.stop();
var t = defn.whole ? defn.ease(defn.pct) : defn.pct;
@ -179,31 +181,30 @@ var Tween = {
var i = Math.trunc(nval);
nval -= i;
if (!defn.whole)
nval = defn.ease(nval);
if (!defn.whole) nval = defn.ease(nval);
if (typeof target === 'string')
obj[target] = tvals[i].lerp(tvals[i+1], nval);
else
target(tvals[i].lerp(tvals[i+1],nval));
if (typeof target === "string") obj[target] = tvals[i].lerp(tvals[i + 1], nval);
else target(tvals[i].lerp(tvals[i + 1], nval));
};
var playing = false;
defn.play = function() {
defn.play = function () {
if (playing) return;
defn._end = Register.update.register(defn.fn.bind(defn));
playing = true;
};
defn.restart = function() {
defn.restart = function () {
defn.accum = 0;
if (typeof target === 'string')
obj[target] = tvals[0];
else
target(tvals[0]);
if (typeof target === "string") obj[target] = tvals[0];
else target(tvals[0]);
};
defn.stop = function() { if (!playing) return; defn.pause(); defn.restart(); };
defn.pause = function() {
defn.stop = function () {
if (!playing) return;
defn.pause();
defn.restart();
};
defn.pause = function () {
defn._end();
if (!playing) return;
@ -216,4 +217,4 @@ var Tween = {
Tween.make = Tween.start;
return {Tween, Ease, tween};
return { Tween, Ease, tween };

View file

@ -1,63 +1,62 @@
var inputpanel = {
title: "untitled",
toString() { return this.title; },
toString() {
return this.title;
},
value: "",
on: false,
pos:[20,window.size.y-20],
wh:[100,100],
anchor: [0,1],
padding:[5,-15],
pos: [20, window.size.y - 20],
wh: [100, 100],
anchor: [0, 1],
padding: [5, -15],
gui() {
this.win ??= Mum.window({width:this.wh.x,height:this.wh.y, color:Color.black.alpha(0.1), anchor:this.anchor, padding:this.padding});
this.win ??= Mum.window({
width: this.wh.x,
height: this.wh.y,
color: Color.black.alpha(0.1),
anchor: this.anchor,
padding: this.padding,
});
var itms = this.guibody();
if (!Array.isArray(itms)) itms = [itms];
if (this.title)
this.win.items = [
Mum.column({items: [Mum.text({str:this.title}), ...itms ]})
];
else
this.win.items = itms;
this.win.draw([100, window.size.y-50]);
if (this.title) this.win.items = [Mum.column({ items: [Mum.text({ str: this.title }), ...itms] })];
else this.win.items = itms;
this.win.draw([100, window.size.y - 50]);
},
guibody() {
return [
Mum.text({str:this.value, color:Color.green}),
Mum.button({str:"SUBMIT", action:this.submit.bind(this)})
];
return [Mum.text({ str: this.value, color: Color.green }), Mum.button({ str: "SUBMIT", action: this.submit.bind(this) })];
},
open() {
this.on = true;
this.value = "";
this.start();
this.keycb();
},
start() {},
close() {
player[0].uncontrol(this);
this.on = false;
if ('on_close' in this)
this.on_close();
if ("on_close" in this) this.on_close();
},
action() {
},
action() {},
closeonsubmit: true,
submit() {
if (!this.submit_check()) return;
this.action();
if (this.closeonsubmit)
this.close();
if (this.closeonsubmit) this.close();
},
submit_check() { return true; },
submit_check() {
return true;
},
keycb() {},
@ -67,9 +66,9 @@ var inputpanel = {
this.value = "";
this.caret = 0;
},
input_backspace_pressrep() {
this.value = this.value.slice(0,-1);
this.value = this.value.slice(0, -1);
this.keycb();
},
};
@ -77,49 +76,62 @@ var inputpanel = {
inputpanel.inputs = {};
inputpanel.inputs.block = true;
inputpanel.inputs.post = function() { this.keycb(); }
inputpanel.inputs.post = function () {
this.keycb();
};
inputpanel.inputs.char = function(c) {
this.value = this.value.slice(0,this.caret) + c + this.value.slice(this.caret);
inputpanel.inputs.char = function (c) {
this.value = this.value.slice(0, this.caret) + c + this.value.slice(this.caret);
this.caret++;
}
inputpanel.inputs['C-d'] = function() { this.value = this.value.slice(0,this.caret) + this.value.slice(this.caret+1); };
inputpanel.inputs['C-d'].rep = true;
inputpanel.inputs.tab = function() {
};
inputpanel.inputs["C-d"] = function () {
this.value = this.value.slice(0, this.caret) + this.value.slice(this.caret + 1);
};
inputpanel.inputs["C-d"].rep = true;
inputpanel.inputs.tab = function () {
this.value = input.tabcomplete(this.value, this.assets);
this.caret = this.value.length;
}
inputpanel.inputs.escape = function() { this.close(); }
inputpanel.inputs['C-b'] = function() {
};
inputpanel.inputs.escape = function () {
this.close();
};
inputpanel.inputs["C-b"] = function () {
if (this.caret === 0) return;
this.caret--;
};
inputpanel.inputs['C-b'].rep = true;
inputpanel.inputs['C-u'] = function()
{
inputpanel.inputs["C-b"].rep = true;
inputpanel.inputs["C-u"] = function () {
this.value = this.value.slice(this.caret);
this.caret = 0;
}
inputpanel.inputs['C-f'] = function() {
};
inputpanel.inputs["C-f"] = function () {
if (this.caret === this.value.length) return;
this.caret++;
};
inputpanel.inputs['C-f'].rep = true;
inputpanel.inputs['C-a'] = function() { this.caret = 0; };
inputpanel.inputs['C-e'] = function() { this.caret = this.value.length; };
inputpanel.inputs.backspace = function() {
inputpanel.inputs["C-f"].rep = true;
inputpanel.inputs["C-a"] = function () {
this.caret = 0;
};
inputpanel.inputs["C-e"] = function () {
this.caret = this.value.length;
};
inputpanel.inputs.backspace = function () {
if (this.caret === 0) return;
this.value = this.value.slice(0,this.caret-1) + this.value.slice(this.caret);
this.value = this.value.slice(0, this.caret - 1) + this.value.slice(this.caret);
this.caret--;
};
inputpanel.inputs.backspace.rep = true;
inputpanel.inputs.enter = function() { this.submit(); }
inputpanel.inputs['C-k'] = function() {
this.value = this.value.slice(0,this.caret);
inputpanel.inputs.enter = function () {
this.submit();
};
inputpanel.inputs.lm = function() { gui.controls.check_submit(); }
inputpanel.inputs["C-k"] = function () {
this.value = this.value.slice(0, this.caret);
};
inputpanel.inputs.lm = function () {
gui.controls.check_submit();
};
var notifypanel = Object.copy(inputpanel, {
title: "notification",
@ -127,28 +139,32 @@ var notifypanel = Object.copy(inputpanel, {
action() {
this.close();
},
guibody() {
return Mum.column({items: [
Mum.text({str:this.msg}),
Mum.button({str:"OK", action:this.close.bind(this)})
]});
return Mum.column({
items: [Mum.text({ str: this.msg }), Mum.button({ str: "OK", action: this.close.bind(this) })],
});
},
});
var gen_notify = function(val, fn) {
var gen_notify = function (val, fn) {
var panel = Object.create(notifypanel);
panel.msg = val;
panel.yes = fn;
panel.inputs = {};
panel.inputs.y = function() { panel.yes(); panel.close(); };
panel.inputs.y = function () {
panel.yes();
panel.close();
};
panel.inputs.y.doc = "Confirm yes.";
panel.inputs.enter = function() { panel.close(); };
panel.inputs.enter = function () {
panel.close();
};
panel.inputs.enter.doc = "Close.";
return panel;
};
var listpanel = Object.copy(inputpanel, {
var listpanel = Object.copy(inputpanel, {
assets: [],
allassets: [],
mumlist: {},
@ -164,28 +180,31 @@ var listpanel = Object.copy(inputpanel, {
this.assets = this.allassets.slice();
this.caret = 0;
this.mumlist = [];
this.assets.forEach(function(x) {
this.mumlist[x] = Mum.text({str:x, action:this.action, color: Color.blue, hovered: {color:Color.red}, selectable:true});
this.assets.forEach(function (x) {
this.mumlist[x] = Mum.text({
str: x,
action: this.action,
color: Color.blue,
hovered: { color: Color.red },
selectable: true,
});
}, this);
},
keycb() {
if(this.value)
this.assets = this.allassets.filter(x => x.startsWith(this.value));
else
this.assets = this.allassets.slice();
for (var m in this.mumlist)
this.mumlist[m].hide = true;
this.assets.forEach(function(x) {
this.mumlist[x].hide = false;
if (this.value) this.assets = this.allassets.filter(x => x.startsWith(this.value));
else this.assets = this.allassets.slice();
for (var m in this.mumlist) this.mumlist[m].hide = true;
this.assets.forEach(function (x) {
this.mumlist[x].hide = false;
}, this);
},
guibody() {
var a = [Mum.text({str:this.value,color:Color.green, caret:this.caret})];
var a = [Mum.text({ str: this.value, color: Color.green, caret: this.caret })];
var b = a.concat(Object.values(this.mumlist));
return Mum.column({items:b, offset:[0,-10]});
return Mum.column({ items: b, offset: [0, -10] });
},
});
return {inputpanel, gen_notify, notifypanel, listpanel}
return { inputpanel, gen_notify, notifypanel, listpanel };