mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Merge branch 'master' of https://github.com/tobspr/shapez.io
This commit is contained in:
@@ -92,6 +92,12 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
*/
|
||||
this.currentDirectionLockSide = 0;
|
||||
|
||||
/**
|
||||
* Whether the side for direction lock has not yet been determined.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.currentDirectionLockSideIndeterminate = true;
|
||||
|
||||
this.initializeBindings();
|
||||
}
|
||||
|
||||
@@ -204,6 +210,17 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
||||
const mouseTile = worldPos.toTileSpace();
|
||||
|
||||
// Figure initial direction
|
||||
const dx = Math.abs(this.lastDragTile.x - mouseTile.x);
|
||||
const dy = Math.abs(this.lastDragTile.y - mouseTile.y);
|
||||
if (dx === 0 && dy === 0) {
|
||||
// Back at the start. Try a new direction.
|
||||
this.currentDirectionLockSideIndeterminate = true;
|
||||
} else if (this.currentDirectionLockSideIndeterminate) {
|
||||
this.currentDirectionLockSideIndeterminate = false;
|
||||
this.currentDirectionLockSide = dx <= dy ? 0 : 1;
|
||||
}
|
||||
|
||||
if (this.currentDirectionLockSide === 0) {
|
||||
return new Vector(this.lastDragTile.x, mouseTile.y);
|
||||
} else {
|
||||
|
||||
@@ -2,43 +2,121 @@ import { BaseHUDPart } from "../base_hud_part";
|
||||
import { makeDiv, round3Digits, round2Digits } from "../../../core/utils";
|
||||
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||
import { KEYMAPPINGS } from "../../key_action_mapper";
|
||||
import { Vector } from "../../../core/vector";
|
||||
import { TrackedState } from "../../../core/tracked_state";
|
||||
|
||||
/** @enum {string} */
|
||||
const enumDebugOverlayMode = { disabled: "disabled", regular: "regular", detailed: "detailed" };
|
||||
|
||||
/**
|
||||
* Specifies which mode follows after which mode
|
||||
* @enum {enumDebugOverlayMode}
|
||||
*/
|
||||
const enumDebugOverlayModeNext = {
|
||||
[enumDebugOverlayMode.disabled]: enumDebugOverlayMode.regular,
|
||||
[enumDebugOverlayMode.regular]: enumDebugOverlayMode.detailed,
|
||||
[enumDebugOverlayMode.detailed]: enumDebugOverlayMode.disabled,
|
||||
};
|
||||
|
||||
const UPDATE_INTERVAL_SECONDS = 0.25;
|
||||
|
||||
export class HUDDebugInfo extends BaseHUDPart {
|
||||
createElements(parent) {
|
||||
this.element = makeDiv(parent, "ingame_HUD_DebugInfo", []);
|
||||
|
||||
this.tickRateElement = makeDiv(this.element, null, ["tickRate"], "Ticks /s: 120");
|
||||
this.fpsElement = makeDiv(this.element, null, ["fps"], "FPS: 60");
|
||||
this.tickDurationElement = makeDiv(this.element, null, ["tickDuration"], "Update time: 0.5ms");
|
||||
const tickRateElement = makeDiv(this.element, null, ["tickRate"]);
|
||||
this.trackedTickRate = new TrackedState(str => (tickRateElement.innerText = str));
|
||||
|
||||
const tickDurationElement = makeDiv(this.element, null, ["tickDuration"]);
|
||||
this.trackedTickDuration = new TrackedState(str => (tickDurationElement.innerText = str));
|
||||
|
||||
const fpsElement = makeDiv(this.element, null, ["fps"]);
|
||||
this.trackedFPS = new TrackedState(str => (fpsElement.innerText = str));
|
||||
|
||||
const mousePositionElement = makeDiv(this.element, null, ["mousePosition"]);
|
||||
this.trackedMousePosition = new TrackedState(str => (mousePositionElement.innerHTML = str));
|
||||
|
||||
const cameraPositionElement = makeDiv(this.element, null, ["cameraPosition"]);
|
||||
this.trackedCameraPosition = new TrackedState(str => (cameraPositionElement.innerHTML = str));
|
||||
|
||||
this.versionElement = makeDiv(this.element, null, ["version"], "version unknown");
|
||||
}
|
||||
|
||||
initialize() {
|
||||
this.lastTick = 0;
|
||||
|
||||
this.visible = false;
|
||||
this.trackedMode = new TrackedState(this.onModeChanged, this);
|
||||
this.domAttach = new DynamicDomAttach(this.root, this.element);
|
||||
|
||||
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.toggleFPSInfo).add(() => this.toggle());
|
||||
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.toggleFPSInfo).add(() => this.cycleModes());
|
||||
|
||||
// Set initial mode
|
||||
this.trackedMode.set(enumDebugOverlayMode.disabled);
|
||||
}
|
||||
|
||||
toggle() {
|
||||
this.visible = !this.visible;
|
||||
this.domAttach.update(this.visible);
|
||||
/**
|
||||
* Called when the mode changed
|
||||
* @param {enumDebugOverlayMode} mode
|
||||
*/
|
||||
onModeChanged(mode) {
|
||||
this.element.setAttribute("data-mode", mode);
|
||||
this.versionElement.innerText = `${G_BUILD_VERSION} @ ${G_APP_ENVIRONMENT} @ ${G_BUILD_COMMIT_HASH}`;
|
||||
}
|
||||
|
||||
update() {
|
||||
const now = this.root.time.realtimeNow();
|
||||
if (now - this.lastTick > 0.25 && this.visible) {
|
||||
this.lastTick = now;
|
||||
this.tickRateElement.innerText = "Tickrate: " + this.root.dynamicTickrate.currentTickRate;
|
||||
this.fpsElement.innerText =
|
||||
"FPS: " +
|
||||
/**
|
||||
* Updates the labels
|
||||
*/
|
||||
updateLabels() {
|
||||
this.trackedTickRate.set("Tickrate: " + this.root.dynamicTickrate.currentTickRate);
|
||||
this.trackedFPS.set(
|
||||
"FPS: " +
|
||||
Math.round(this.root.dynamicTickrate.averageFps) +
|
||||
" (" +
|
||||
round2Digits(1000 / this.root.dynamicTickrate.averageFps) +
|
||||
" ms)";
|
||||
this.tickDurationElement.innerText =
|
||||
"Tick Dur: " + round3Digits(this.root.dynamicTickrate.averageTickDuration) + "ms";
|
||||
" ms)"
|
||||
);
|
||||
this.trackedTickDuration.set(
|
||||
"Tick: " + round3Digits(this.root.dynamicTickrate.averageTickDuration) + "ms"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the detailed information
|
||||
*/
|
||||
updateDetailedInformation() {
|
||||
const mousePos = this.root.app.mousePosition || new Vector(0, 0);
|
||||
const mouseTile = this.root.camera.screenToWorld(mousePos).toTileSpace();
|
||||
const cameraTile = this.root.camera.center.toTileSpace();
|
||||
|
||||
this.trackedMousePosition.set(`Pos: <code>${mouseTile.x}</code> / <code>${mouseTile.y}</code>`);
|
||||
this.trackedCameraPosition.set(`Center: <code>${cameraTile.x}</code> / <code>${cameraTile.y}</code>`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cycles through the different modes
|
||||
*/
|
||||
cycleModes() {
|
||||
this.trackedMode.set(enumDebugOverlayModeNext[this.trackedMode.get()]);
|
||||
}
|
||||
|
||||
update() {
|
||||
const visible = this.trackedMode.get() !== enumDebugOverlayMode.disabled;
|
||||
this.domAttach.update(visible);
|
||||
|
||||
if (!visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Periodically update the text
|
||||
const now = this.root.time.realtimeNow();
|
||||
if (now - this.lastTick > UPDATE_INTERVAL_SECONDS) {
|
||||
this.lastTick = now;
|
||||
this.updateLabels();
|
||||
}
|
||||
|
||||
// Also update detailed information if required
|
||||
if (this.trackedMode.get() === enumDebugOverlayMode.detailed) {
|
||||
this.updateDetailedInformation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user