From a4c90028a967c786c639fe822331d3cf9222032f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20=C4=8Carapi=C4=87?= Date: Mon, 10 Feb 2025 17:28:46 +0100 Subject: [PATCH] Pause on window focus loss --- src/main.ts | 18 ++++++++++++++++++ src/scenes/Game.ts | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/src/main.ts b/src/main.ts index 6311fad..3543950 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,6 +46,7 @@ import { GemType } from './classes/Definitions'; } Engine.latestCommit = await fetch('/latest_commit').then((res) => res.text()); window.addEventListener('resize', resize); + resize(); await Assets.LoadAssets(); GameUIConstants.init(); @@ -71,4 +72,21 @@ import { GemType } from './classes/Definitions'; return 'You are about to leave.'; }; 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(); + } + }); })(); diff --git a/src/scenes/Game.ts b/src/scenes/Game.ts index f821f02..ff00725 100644 --- a/src/scenes/Game.ts +++ b/src/scenes/Game.ts @@ -52,10 +52,12 @@ export class GameScene extends Scene { y: 0, zIndex: 120, }); + private windowTitle: string; constructor(name: string) { super(); Engine.GameScene = this; + this.windowTitle = document.title; GameAssets.Missions.forEach((mission, index) => { if (mission.name == name) { this.mission = mission; @@ -283,10 +285,12 @@ export class GameScene extends Scene { public PauseGame() { this.isPaused = true; this.ticker.stop(); + document.title = '[PAUSED] ' + this.windowTitle; } public UnpauseGame() { this.isPaused = false; this.ticker.start(); + document.title = this.windowTitle; } public ShowPauseDialog() { const gamePausedDialog = new GamePausedDialog();