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

45 lines
1.7 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-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-05-18 12:53:01 +02:00
this.tickDurationElement = makeDiv(this.element, null, ["tickDuration"], "Update time: 0.5ms");
}
2020-05-18 17:40:20 +02:00
initialize() {
this.lastTick = 0;
this.visible = false;
this.domAttach = new DynamicDomAttach(this.root, this.element);
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.toggleFPSInfo).add(() => this.toggle());
}
toggle() {
this.visible = !this.visible;
this.domAttach.update(this.visible);
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();
if (now - this.lastTick > 0.25 && this.visible) {
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-05-18 12:53:01 +02:00
}
}