2023-04-22 16:44:26 -05:00
|
|
|
var Gizmos = {
|
|
|
|
pick_gameobject_points(worldpos, gameobject, points) {
|
|
|
|
var idx = grab_from_points(worldpos, points.map(gameobject.this2world,gameobject), 25);
|
2023-09-27 12:36:32 -05:00
|
|
|
if (idx === -1) return undefined;
|
2023-04-22 16:44:26 -05:00
|
|
|
return points[idx];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-05-27 10:13:20 -05:00
|
|
|
var Shape = {
|
|
|
|
circle(pos, radius, color) {
|
|
|
|
cmd(115, pos, radius, color);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
var Debug = {
|
2023-05-16 13:31:19 -05:00
|
|
|
draw_grid(width, span, color) {
|
|
|
|
color = color ? color : Color.green;
|
|
|
|
cmd(47, width, span, color);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
point(pos, size, color) {
|
|
|
|
color = color ? color : Color.blue;
|
2023-08-23 17:18:34 -05:00
|
|
|
Shape.circle(pos, size, color);
|
|
|
|
// cmd(51, pos, size,color);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
arrow(start, end, color, capsize) {
|
|
|
|
color = color ? color : Color.red;
|
|
|
|
if (!capsize)
|
|
|
|
capsize = 4;
|
|
|
|
cmd(81, start, end, color, capsize);
|
|
|
|
},
|
2023-06-05 10:32:45 -05:00
|
|
|
|
|
|
|
poly(points, color) {
|
|
|
|
cmd_points(0,points,color);
|
|
|
|
},
|
2023-06-06 15:49:55 -05:00
|
|
|
|
|
|
|
boundingbox(bb, color) {
|
2023-08-14 17:20:30 -05:00
|
|
|
color ??= Color.white;
|
2023-06-06 15:49:55 -05:00
|
|
|
cmd_points(0, bb2points(bb), color);
|
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
box(pos, wh, color) {
|
2023-06-07 17:26:46 -05:00
|
|
|
color ??= Color.white;
|
2023-04-22 16:44:26 -05:00
|
|
|
cmd(53, pos, wh, color);
|
|
|
|
},
|
|
|
|
|
|
|
|
numbered_point(pos, n) {
|
|
|
|
Debug.point(world2screen(pos), 3);
|
2023-10-05 17:30:17 -05:00
|
|
|
GUI.text(n, world2screen(pos).add([0,4]), 1);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
phys_drawing: false,
|
|
|
|
draw_phys(on) {
|
|
|
|
this.phys_drawing = on;
|
|
|
|
cmd(4, this.phys_drawing);
|
|
|
|
},
|
|
|
|
|
|
|
|
draw_obj_phys(obj) {
|
|
|
|
cmd(82, obj.body);
|
|
|
|
},
|
|
|
|
|
|
|
|
register_call(fn, obj) {
|
2023-08-28 17:00:53 -05:00
|
|
|
Register.debug.register(fn,obj);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
2023-05-29 10:47:30 -05:00
|
|
|
line(points, color, type, thickness) {
|
|
|
|
thickness ??= 1;
|
2023-09-06 17:48:08 -05:00
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
if (!type)
|
|
|
|
type = 0;
|
|
|
|
|
|
|
|
if (!color)
|
|
|
|
color = Color.white;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
2023-05-29 10:47:30 -05:00
|
|
|
cmd(83, points, color, thickness);
|
2023-04-22 16:44:26 -05:00
|
|
|
}
|
|
|
|
},
|
2023-08-17 20:13:17 -05:00
|
|
|
|
|
|
|
draw_bb: false,
|
|
|
|
draw_gizmos: false,
|
|
|
|
draw_names: false,
|
|
|
|
|
|
|
|
draw() {
|
|
|
|
if (this.draw_bb)
|
2023-09-23 12:35:02 -05:00
|
|
|
Game.objects.forEach(function(x) { Debug.boundingbox(x.boundingbox(), Color.Debug.boundingbox.alpha(0.05)); });
|
2023-08-17 20:13:17 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
if (Game.paused()) GUI.text("PAUSED", [0,0],1);
|
2023-08-17 20:13:17 -05:00
|
|
|
|
|
|
|
if (this.draw_gizmos)
|
|
|
|
Game.objects.forEach(function(x) {
|
|
|
|
if (!x.icon) return;
|
|
|
|
gui_img(x.icon, world2screen(x.pos));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.draw_names)
|
|
|
|
Game.objects.forEach(function(x) {
|
2023-09-23 12:35:02 -05:00
|
|
|
GUI.text(x, world2screen(x.pos).add([0,32]), 1, Color.Debug.names);
|
2023-08-17 20:13:17 -05:00
|
|
|
});
|
2023-08-24 16:22:52 -05:00
|
|
|
|
2023-09-12 12:45:54 -05:00
|
|
|
if (Debug.Options.gif.rec) {
|
2023-09-12 00:02:57 -05:00
|
|
|
gui_text("REC", [0,40], 1);
|
2023-09-12 12:45:54 -05:00
|
|
|
gui_text(Time.seconds_to_timecode(Time.time - Debug.Options.gif.start_time, Debug.Options.gif.fps), [0,30], 1);
|
|
|
|
}
|
2023-09-12 00:02:57 -05:00
|
|
|
|
2023-10-04 08:18:09 -05:00
|
|
|
GUI.text(Game.playing() ? "PLAYING"
|
2023-08-27 21:57:19 -05:00
|
|
|
: Game.stepping() ?
|
|
|
|
"STEP" :
|
|
|
|
Game.paused() ?
|
2023-08-28 17:00:53 -05:00
|
|
|
"PAUSED; EDITING" :
|
|
|
|
"EDIT", [0, 0], 1);
|
2023-08-17 20:13:17 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Debug.Options = { };
|
|
|
|
Debug.Options.Color = {
|
|
|
|
set trigger(x) { cmd(17,x); },
|
|
|
|
set debug(x) { cmd(16, x); },
|
2023-04-22 16:44:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var Gizmos = {
|
|
|
|
pick_gameobject_points(worldpos, gameobject, points) {
|
|
|
|
var idx = grab_from_points(worldpos, points.map(gameobject.this2world,gameobject), 25);
|
|
|
|
if (idx === -1) return null;
|
|
|
|
return points[idx];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-07 08:41:09 -05:00
|
|
|
var Profile = {
|
2023-09-11 02:46:12 -05:00
|
|
|
tick_now() { return cmd(127); },
|
|
|
|
ns(ticks) { return cmd(128, ticks); },
|
|
|
|
us(ticks) { return cmd(129, ticks); },
|
|
|
|
ms(ticks) { return cmd(130, ticks); },
|
|
|
|
cpu(fn, times, q) {
|
2023-06-07 08:41:09 -05:00
|
|
|
times ??= 1;
|
2023-09-11 02:46:12 -05:00
|
|
|
q ??= "ns";
|
|
|
|
var start = Profile.tick_now();
|
2023-06-07 08:41:09 -05:00
|
|
|
for (var i = 0; i < times; i++)
|
|
|
|
fn();
|
2023-09-11 02:46:12 -05:00
|
|
|
var elapsed = Profile.tick_now() - start;
|
|
|
|
Log.say(`Profiled in ${Profile[q](elapsed)/times} avg ${q}.`);
|
2023-06-07 08:41:09 -05:00
|
|
|
},
|
2023-08-17 20:13:17 -05:00
|
|
|
|
|
|
|
get fps() { return sys_cmd(8); },
|
2023-06-07 08:41:09 -05:00
|
|
|
};
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-28 17:00:53 -05:00
|
|
|
/* These controls are available during editing, and during play of debug builds */
|
2023-08-24 16:22:52 -05:00
|
|
|
var DebugControls = {};
|
|
|
|
DebugControls.inputs = {};
|
|
|
|
DebugControls.inputs.f1 = function () { Debug.draw_phys(!Debug.phys_drawing); };
|
|
|
|
DebugControls.inputs.f1.doc = "Draw physics debugging aids.";
|
2023-09-20 08:11:14 -05:00
|
|
|
//DebugControls.inputs.f3 = function() { Debug.draw_bb = !Debug.draw_bb; };
|
|
|
|
//DebugControls.inputs.f3.doc = "Toggle drawing bounding boxes.";
|
2023-08-24 16:22:52 -05:00
|
|
|
DebugControls.inputs.f4 = function() {
|
2023-10-11 17:22:41 -05:00
|
|
|
// Debug.draw_names = !Debug.draw_names;
|
|
|
|
// Debug.draw_gizmos = !Debug.draw_gizmos;
|
2023-08-24 16:22:52 -05:00
|
|
|
};
|
|
|
|
DebugControls.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
|
2023-09-12 00:02:57 -05:00
|
|
|
|
|
|
|
Debug.Options.gif = {
|
2023-09-12 12:45:54 -05:00
|
|
|
w: 640, /* Max width */
|
|
|
|
h: 480, /* Max height */
|
|
|
|
stretch: false, /* True if you want to stretch */
|
2023-09-13 07:31:00 -05:00
|
|
|
cpf: 4,
|
2023-09-14 12:49:29 -05:00
|
|
|
depth: 16,
|
2023-09-12 00:02:57 -05:00
|
|
|
file: "out.gif",
|
|
|
|
rec: false,
|
2023-09-12 12:45:54 -05:00
|
|
|
secs: 6,
|
|
|
|
start_time: 0,
|
|
|
|
fps: 0,
|
|
|
|
start() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd(131, w, h, this.cpf, this.depth);
|
|
|
|
this.rec = true;
|
|
|
|
this.fps = (1/this.cpf)*100;
|
|
|
|
this.start_time = Time.time;
|
|
|
|
|
2023-09-13 07:31:00 -05:00
|
|
|
timer.oneshot(this.stop.bind(this), this.secs, this, true);
|
2023-09-12 12:45:54 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if (!this.rec) return;
|
|
|
|
cmd(132, this.file);
|
|
|
|
this.rec = false;
|
|
|
|
},
|
2023-09-12 00:02:57 -05:00
|
|
|
};
|
2023-09-12 12:45:54 -05:00
|
|
|
|
2023-09-25 12:29:04 -05:00
|
|
|
DebugControls.inputs.f8 = function() {
|
|
|
|
var now = new Date();
|
|
|
|
Debug.Options.gif.file = now.toISOString() + ".gif";
|
|
|
|
Debug.Options.gif.start();
|
|
|
|
};
|
|
|
|
DebugControls.inputs.f9 = function() {
|
|
|
|
Debug.Options.gif.stop();
|
|
|
|
}
|
2023-09-12 00:02:57 -05:00
|
|
|
|
2023-08-24 16:22:52 -05:00
|
|
|
DebugControls.inputs.f10 = function() { Time.timescale = 0.1; };
|
|
|
|
DebugControls.inputs.f10.doc = "Toggle timescale to 1/10.";
|
2023-08-27 21:57:19 -05:00
|
|
|
DebugControls.inputs.f10.released = function () { Time.timescale = 1.0; };
|
2023-08-24 16:22:52 -05:00
|
|
|
DebugControls.inputs.f12 = function() { GUI.defaults.debug = !GUI.defaults.debug; Log.warn("GUI toggle debug");};
|
|
|
|
DebugControls.inputs.f12.doc = "Toggle drawing GUI debugging aids.";
|
|
|
|
|
|
|
|
DebugControls.inputs['M-1'] = Render.normal;
|
|
|
|
Render.normal.doc = "Render mode for enabling all shaders and lighting effects.";
|
|
|
|
DebugControls.inputs['M-2'] = Render.wireframe;
|
|
|
|
Render.wireframe.doc = "Render mode to see wireframes of all models.";
|
2023-08-17 20:13:17 -05:00
|
|
|
|
2023-08-28 17:00:53 -05:00
|
|
|
DebugControls.inputs['C-M-f'] = function() {};
|
|
|
|
DebugControls.inputs['C-M-f'].doc = "Enter camera fly mode.";
|
|
|
|
|
2023-08-17 20:13:17 -05:00
|
|
|
var Time = {
|
|
|
|
set timescale(x) { cmd(3, x); },
|
2023-08-28 17:00:53 -05:00
|
|
|
get timescale() { return cmd(121); },
|
2023-08-17 20:13:17 -05:00
|
|
|
set updateMS(x) { cmd(6, x); },
|
|
|
|
set physMS(x) { cmd(7, x); },
|
|
|
|
set renderMS(x) { cmd(5, x); },
|
2023-08-29 17:11:36 -05:00
|
|
|
|
2023-09-12 12:45:54 -05:00
|
|
|
get time() { return cmd(133); },
|
|
|
|
|
|
|
|
seconds_to_timecode(secs, fps)
|
|
|
|
{
|
|
|
|
var s = Math.trunc(secs);
|
|
|
|
secs -= s;
|
|
|
|
var f = Math.trunc(fps * secs);
|
|
|
|
return `${s}:${f}`;
|
|
|
|
},
|
|
|
|
|
2023-08-29 17:11:36 -05:00
|
|
|
pause() {
|
|
|
|
Time.timescale = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
play() {
|
|
|
|
if (!Time.stash) {
|
|
|
|
Log.warn("Tried to resume time without calling Time.pause first.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Time.timescale = Time.stash;
|
|
|
|
},
|
2023-05-24 20:45:50 -05:00
|
|
|
};
|
|
|
|
|
2023-08-28 17:00:53 -05:00
|
|
|
Player.players[0].control(DebugControls);
|
|
|
|
Register.gui.register(Debug.draw, Debug);
|