diff --git a/frontend/src/js/main.ts b/frontend/src/js/main.ts index 65208719e1687e5e64ab46e2f7bde2e115059379..ea3e272199d6f480bd224dfe07e7e804aebf4386 100644 --- a/frontend/src/js/main.ts +++ b/frontend/src/js/main.ts @@ -23,4 +23,24 @@ const config = { scene: [BackgroundScene, WelcomeScene, FightScene, GameOverScene], }; -new Phaser.Game(config); +const game = new Phaser.Game(config); + +const pauseGame = function (this: Phaser.Game) { + this.scene + .getScenes(true) + .filter((scene) => !scene.scene.isPaused()) + .forEach((scene) => scene.scene.pause()); +}; + +game.events.on(Phaser.Core.Events.BLUR, pauseGame, game); +game.events.on(Phaser.Core.Events.HIDDEN, pauseGame, game); + +const resumeGame = function (this: Phaser.Game) { + this.scene + .getScenes(false) + .filter((scene) => scene.scene.isPaused()) + .forEach((scene) => scene.scene.resume()); +}; + +game.events.on(Phaser.Core.Events.FOCUS, resumeGame, game); +game.events.on(Phaser.Core.Events.VISIBLE, resumeGame, game);