Pause on window focus loss

This commit is contained in:
Dalibor Čarapić 2025-02-10 17:28:46 +01:00
parent 1d7b4070f6
commit a4c90028a9
2 changed files with 22 additions and 0 deletions

View File

@ -46,6 +46,7 @@ import { GemType } from './classes/Definitions';
} }
Engine.latestCommit = await fetch('/latest_commit').then((res) => res.text()); Engine.latestCommit = await fetch('/latest_commit').then((res) => res.text());
window.addEventListener('resize', resize); window.addEventListener('resize', resize);
resize(); resize();
await Assets.LoadAssets(); await Assets.LoadAssets();
GameUIConstants.init(); GameUIConstants.init();
@ -71,4 +72,21 @@ import { GemType } from './classes/Definitions';
return 'You are about to leave.'; return 'You are about to leave.';
}; };
else Engine.TestSuite(); else Engine.TestSuite();
let gamePausedDueToBlur = false;
window.addEventListener('blur', () => {
console.log('blur');
if (Engine.GameScene && !Engine.GameScene.isPaused) {
Engine.GameScene.PauseGame();
gamePausedDueToBlur = true;
}
});
window.addEventListener('focus', () => {
console.log('focus');
if (Engine.GameScene && gamePausedDueToBlur && Engine.GameScene.isPaused) {
gamePausedDueToBlur = false;
Engine.GameScene.UnpauseGame();
}
});
})(); })();

View File

@ -52,10 +52,12 @@ export class GameScene extends Scene {
y: 0, y: 0,
zIndex: 120, zIndex: 120,
}); });
private windowTitle: string;
constructor(name: string) { constructor(name: string) {
super(); super();
Engine.GameScene = this; Engine.GameScene = this;
this.windowTitle = document.title;
GameAssets.Missions.forEach((mission, index) => { GameAssets.Missions.forEach((mission, index) => {
if (mission.name == name) { if (mission.name == name) {
this.mission = mission; this.mission = mission;
@ -283,10 +285,12 @@ export class GameScene extends Scene {
public PauseGame() { public PauseGame() {
this.isPaused = true; this.isPaused = true;
this.ticker.stop(); this.ticker.stop();
document.title = '[PAUSED] ' + this.windowTitle;
} }
public UnpauseGame() { public UnpauseGame() {
this.isPaused = false; this.isPaused = false;
this.ticker.start(); this.ticker.start();
document.title = this.windowTitle;
} }
public ShowPauseDialog() { public ShowPauseDialog() {
const gamePausedDialog = new GamePausedDialog(); const gamePausedDialog = new GamePausedDialog();