prosperon/scripts/debug.js

232 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

2024-09-26 11:36:09 -05:00
debug.build = function (fn) {
if (!debug.show) return;
fn();
};
2024-08-24 08:04:41 -05:00
debug.show = true;
debug.urnames = false;
2024-08-24 18:46:21 -05:00
debug.termout = true;
2024-08-25 15:29:35 -05:00
debug.console = false;
2024-08-26 11:13:26 -05:00
debug.cheat = false;
2024-09-03 14:08:46 -05:00
debug.meta = false;
2024-09-04 14:34:45 -05:00
debug.showprofiler = false;
2024-09-03 14:08:46 -05:00
2024-09-26 11:36:09 -05:00
debug.fn_break = function (fn, obj = globalThis) {
if (typeof fn !== "function") return;
var newfn = function () {
2024-03-19 23:01:31 -05:00
console.log("broke");
fn();
};
2024-09-26 11:36:09 -05:00
obj[fn.name] = newfn;
};
2023-08-17 20:13:17 -05:00
2024-03-19 23:01:31 -05:00
debug.draw_phys = false;
debug.draw_bb = false;
debug.draw_gizmos = false;
debug.draw_names = false;
2024-08-15 12:26:37 -05:00
debug.sprite_nums = false;
2024-09-26 11:36:09 -05:00
debug.draw = function () {
if (this.draw_phys)
game.all_objects(function (x) {
debug.draw_gameobject(x);
});
2024-03-19 23:01:31 -05:00
if (this.draw_bb)
2024-09-26 11:36:09 -05:00
game.all_objects(function (x) {
debug.boundingbox(x.boundingbox(), Color.debug.boundingbox.alpha(0.05));
});
2023-08-17 20:13:17 -05:00
2024-03-19 23:01:31 -05:00
if (this.draw_gizmos)
2024-09-26 11:36:09 -05:00
game.all_objects(function (x) {
2024-03-19 23:01:31 -05:00
if (!x.icon) return;
2024-04-03 17:17:32 -05:00
gui.image(x.icon, game.camera.world2view(x.pos));
2024-03-19 23:01:31 -05:00
});
2024-03-19 23:01:31 -05:00
if (this.draw_names)
2024-09-26 11:36:09 -05:00
game.all_objects(function (x) {
render.text(x, game.camera.view2screen(x.pos).add([0, 32]), 1, Color.debug.names);
2024-03-19 23:01:31 -05:00
});
2023-09-12 00:02:57 -05:00
2024-03-19 23:01:31 -05:00
if (debug.gif.rec) {
2024-09-26 11:36:09 -05:00
render.text("REC", [0, 40], 1);
render.text(time.timecode(time.timenow() - debug.gif.start_time, debug.gif.fps), [0, 30], 1);
2024-03-19 23:01:31 -05:00
}
2024-09-26 11:36:09 -05:00
2024-03-19 23:01:31 -05:00
return;
2024-09-26 11:36:09 -05:00
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);
};
var assert = function (op, str = `assertion failed [value '${op}']`) {
if (!op) console.panic(str);
};
2023-12-18 17:12:05 -06:00
var Gizmos = {
pick_gameobject_points(worldpos, gameobject, points) {
2024-09-26 11:36:09 -05:00
var idx = Math.grab_from_points(worldpos, points.map(gameobject.this2world, gameobject), 25);
2023-12-18 06:45:27 -06:00
if (idx === -1) return undefined;
return idx;
},
};
/* These controls are available during editing, and during play of debug builds */
2024-03-19 23:01:31 -05:00
debug.inputs = {};
2024-09-26 11:36:09 -05:00
debug.inputs.f1 = function () {
debug.draw_phys = !debug.draw_phys;
};
2024-03-19 23:01:31 -05:00
debug.inputs.f1.doc = "Draw physics debugging aids.";
2024-09-26 11:36:09 -05:00
debug.inputs.f3 = function () {
debug.draw_bb = !debug.draw_bb;
};
2024-03-19 23:01:31 -05:00
debug.inputs.f3.doc = "Toggle drawing bounding boxes.";
2024-09-26 11:36:09 -05:00
debug.inputs.f4 = function () {
2024-03-19 23:01:31 -05:00
debug.draw_names = !debug.draw_names;
debug.draw_gizmos = !debug.draw_gizmos;
};
2024-03-19 23:01:31 -05:00
debug.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
2024-03-19 14:39:19 -05:00
2024-08-25 00:18:30 -05:00
var gif = {
2024-09-26 11:36:09 -05:00
w: 640 /* Max width */,
h: 480 /* Max height */,
stretch: false /* True if you want to stretch */,
cpf: 4,
depth: 16,
2023-09-12 00:02:57 -05:00
file: "out.gif",
rec: false,
secs: 6,
start_time: 0,
fps: 0,
start() {
var w = this.w;
var h = this.h;
if (!this.stretch) {
2024-09-26 11:36:09 -05:00
var win = window.height / window.width;
var gif = h / w;
if (gif > win) h = w * win;
else w = h / win;
}
2024-09-26 11:36:09 -05:00
// cmd(131, w, h, this.cpf, this.depth);
this.rec = true;
2024-09-26 11:36:09 -05:00
this.fps = (1 / this.cpf) * 100;
this.start_time = time.now();
timer.oneshot(this.stop.bind(this), this.secs, this, true);
},
stop() {
if (!this.rec) return;
2024-09-26 11:36:09 -05:00
// cmd(132, this.file);
this.rec = false;
},
2023-09-12 00:02:57 -05:00
};
2024-09-26 11:36:09 -05:00
debug.inputs.f8 = function () {
var now = new Date();
2024-03-19 23:01:31 -05:00
debug.gif.file = now.toISOString() + ".gif";
debug.gif.start();
};
2024-09-26 11:36:09 -05:00
debug.inputs.f9 = function () {
2024-03-19 23:01:31 -05:00
debug.gif.stop();
2024-09-26 11:36:09 -05:00
};
2023-09-12 00:02:57 -05:00
2024-09-26 11:36:09 -05:00
debug.inputs.f10 = function () {
time.timescale = 0.1;
};
2024-03-19 23:01:31 -05:00
debug.inputs.f10.doc = "Toggle timescale to 1/10.";
2024-09-26 11:36:09 -05:00
debug.inputs.f10.released = function () {
time.timescale = 1.0;
};
debug.inputs.f12 = function () {
gui.defaults.debug = !gui.defaults.debug;
console.warn("gui toggle debug");
};
2024-04-03 17:17:32 -05:00
debug.inputs.f12.doc = "Toggle drawing gui debugging aids.";
2024-09-26 11:36:09 -05:00
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.";
2024-03-19 23:01:31 -05:00
debug.api = {};
2024-09-26 11:36:09 -05:00
debug.api.doc_entry = function (obj, key) {
if (typeof key !== "string") {
2024-02-23 16:05:30 -06:00
console.warn("Cannot print a key that isn't a string.");
return undefined;
}
2024-09-26 11:36:09 -05:00
var title = key;
2024-09-26 11:36:09 -05:00
var o = obj[key];
2024-09-26 11:36:09 -05:00
if (typeof o === "undefined" && obj.impl && typeof obj.impl[key] !== "undefined") o = obj.impl[key];
var t = typeof o;
2024-02-23 16:05:30 -06:00
if (Array.isArray(o)) t = "array";
2024-09-26 11:36:09 -05:00
else if (t === "function") {
title = o.toString().tofirst(")") + ")";
title = title.fromfirst("(");
2024-02-23 16:05:30 -06:00
title = key + "(" + title;
if (o.doc) doc = o.doc;
t = "";
2024-09-26 11:36:09 -05:00
} else if (t === "undefined") t = "";
2024-02-23 16:05:30 -06:00
if (t) t = "**" + t + "**\n";
2024-02-23 16:05:30 -06:00
var doc = "";
if (o.doc) doc = o.doc;
else if (obj.doc && obj.doc[key]) doc = obj.doc[key];
else if (Array.isArray(o)) doc = json.encode(o);
2024-07-03 16:38:29 -05:00
return `#### ${title}
${t}
${doc}
`;
2024-09-26 11:36:09 -05:00
};
2024-09-26 11:36:09 -05:00
debug.api.print_doc = function (name) {
2024-02-23 16:05:30 -06:00
var obj = name;
2024-09-26 11:36:09 -05:00
if (typeof name === "string") {
2024-02-23 16:05:30 -06:00
obj = eval(name);
if (!obj) {
console.warn(`Cannot print the API of '${name}', as it was not found.`);
return undefined;
}
obj = globalThis[name];
}
2024-09-26 11:36:09 -05:00
obj = eval(name);
2024-02-23 16:05:30 -06:00
if (!Object.isObject(obj)) {
console.warn("Cannot print the API of something that isn't an object.");
return undefined;
}
2024-02-23 16:05:30 -06:00
if (!obj) {
console.warn(`Object '${name}' does not exist.`);
return;
}
2024-09-26 11:36:09 -05:00
2024-02-23 16:05:30 -06:00
var mdoc = "# " + name + "\n";
if (obj.doc?.doc) mdoc += obj.doc.doc + "\n";
2024-09-26 11:36:09 -05:00
else if (typeof obj.doc === "string") mdoc += obj.doc + "\n";
2024-07-03 16:38:29 -05:00
var keys = Object.keys(obj);
for (var key of keys) {
2024-09-26 11:36:09 -05:00
if (key === "doc") continue;
if (key === "toString") continue;
2024-02-23 16:05:30 -06:00
2024-03-19 23:01:31 -05:00
mdoc += debug.api.doc_entry(obj, key) + "\n";
}
return mdoc;
2024-09-26 11:36:09 -05:00
};
return {
2024-03-19 23:01:31 -05:00
debug,
2024-03-01 11:45:06 -06:00
Gizmos,
2024-09-26 11:36:09 -05:00
assert,
};