This repository has been archived on 2021-02-20. You can view files and clone it, but cannot push or open issues or pull requests.
shapez.io/src/js/game/hud/parts/debug_info.js

87 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-05-18 12:53:01 +02:00
import { BaseHUDPart } from "../base_hud_part";
2020-05-18 17:40:20 +02:00
import { makeDiv, round3Digits, round2Digits } from "../../../core/utils";
import { DynamicDomAttach } from "../dynamic_dom_attach";
import { KEYMAPPINGS } from "../../key_action_mapper";
2020-06-30 03:53:06 +02:00
import { Vector } from "../../../core/vector";
2020-05-18 12:53:01 +02:00
2020-07-02 00:05:25 +02:00
/** @enum {string} */
2020-07-01 23:41:01 +02:00
export const enumDebugOverlayMode = { disabled: "disabled", regular: "regular", detailed: "detailed" };
2020-07-01 22:53:51 +02:00
2020-05-18 12:53:01 +02:00
export class HUDDebugInfo extends BaseHUDPart {
createElements(parent) {
this.element = makeDiv(parent, "ingame_HUD_DebugInfo", []);
this.tickRateElement = makeDiv(this.element, null, ["tickRate"], "Ticks /s: 120");
2020-05-18 17:40:20 +02:00
this.fpsElement = makeDiv(this.element, null, ["fps"], "FPS: 60");
2020-06-30 03:53:06 +02:00
this.tickDurationElement = makeDiv(this.element, null, ["tickDuration"], "Tick dur: 0.5ms");
this.mousePositionElement = makeDiv(this.element, null, ["mousePosition"], "Pos: 0 0");
this.cameraPositionElement = makeDiv(this.element, null, ["cameraPosition"], "Center: 0 0");
this.versionElement = makeDiv(this.element, null, ["version"], "version unknown");
2020-05-18 12:53:01 +02:00
}
2020-05-18 17:40:20 +02:00
initialize() {
this.lastTick = 0;
2020-07-01 22:53:51 +02:00
this.mode = enumDebugOverlayMode.disabled;
this.domAttach = new DynamicDomAttach(this.root, this.element);
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.toggleFPSInfo).add(() => this.toggle());
}
2020-06-30 03:53:06 +02:00
updateFullText() {
2020-07-06 11:02:31 +02:00
this.element.setAttribute("data-mode", this.mode);
2020-06-30 03:53:06 +02:00
let version = `version ${G_BUILD_VERSION}`;
if (this.full) {
version += ` @ ${G_APP_ENVIRONMENT} @ ${G_BUILD_COMMIT_HASH}`;
}
this.versionElement.innerText = version;
}
updatePositions() {
let mousePos = this.root.app.mousePosition || new Vector(0, 0);
mousePos = this.root.camera.screenToWorld(mousePos).toTileSpace();
const cameraPos = this.root.camera.center.toTileSpace();
this.mousePositionElement.innerText = `Pos: ${mousePos.x} ${mousePos.y}`;
this.cameraPositionElement.innerText = `Center: ${cameraPos.x} ${cameraPos.y}`;
}
toggle() {
2020-07-01 22:53:51 +02:00
switch (this.mode) {
case enumDebugOverlayMode.detailed:
this.mode = enumDebugOverlayMode.disabled;
break;
case enumDebugOverlayMode.regular:
this.mode = enumDebugOverlayMode.detailed;
break;
default:
this.mode = enumDebugOverlayMode.regular;
break;
2020-06-30 03:53:06 +02:00
}
this.updateFullText();
2020-07-06 11:02:31 +02:00
this.domAttach.update(this.mode !== enumDebugOverlayMode.disabled);
2020-05-18 17:40:20 +02:00
}
2020-05-18 12:53:01 +02:00
update() {
2020-05-18 17:40:20 +02:00
const now = this.root.time.realtimeNow();
2020-07-06 11:02:31 +02:00
if (!this.visible) return;
2020-07-06 11:02:31 +02:00
if (now - this.lastTick > 0.25) {
2020-05-18 17:40:20 +02:00
this.lastTick = now;
this.tickRateElement.innerText = "Tickrate: " + this.root.dynamicTickrate.currentTickRate;
this.fpsElement.innerText =
"FPS: " +
Math.round(this.root.dynamicTickrate.averageFps) +
2020-05-18 17:40:20 +02:00
" (" +
round2Digits(1000 / this.root.dynamicTickrate.averageFps) +
" ms)";
this.tickDurationElement.innerText =
"Tick Dur: " + round3Digits(this.root.dynamicTickrate.averageTickDuration) + "ms";
}
2020-06-30 03:53:06 +02:00
this.updatePositions();
2020-05-18 12:53:01 +02:00
}
}