From 7e9f6568facb1ba1225f4554d3e8334c9cce40ba Mon Sep 17 00:00:00 2001 From: Paolo Brasolin <paolo.brasolin@eurac.edu> Date: Mon, 4 Apr 2022 12:45:56 +0200 Subject: [PATCH] feat: #fe pause/resume gain depending on focus --- frontend/src/js/main.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/frontend/src/js/main.ts b/frontend/src/js/main.ts index 6520871..ea3e272 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); -- GitLab