From aa731553f7df0930911bb6aa99388ff97f7a371a Mon Sep 17 00:00:00 2001 From: Matojeje Date: Tue, 20 Aug 2024 19:25:08 +0200 Subject: [PATCH] Music cutscene volume crossfade --- src/components/Intermission.ts | 4 ++++ src/scenes/GameScene.ts | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/components/Intermission.ts b/src/components/Intermission.ts index 773d421..628dcdf 100644 --- a/src/components/Intermission.ts +++ b/src/components/Intermission.ts @@ -16,6 +16,7 @@ export const enum Mode { export class Intermission extends Phaser.GameObjects.Container { public scene: GameScene; public mode: Mode; + public transitionProgress: number; private graphics: Phaser.GameObjects.Graphics; private cutscene: Phaser.GameObjects.Image; @@ -75,6 +76,7 @@ export class Intermission extends Phaser.GameObjects.Container { this.add(this.subtitles); this.queuedLines = []; + this.transitionProgress = 0; /* Button */ @@ -218,11 +220,13 @@ export class Intermission extends Phaser.GameObjects.Container { to: 1, ease: Phaser.Math.Easing.Quintic.InOut, onUpdate: (tween, target, key, current: number) => { + this.transitionProgress = current; let radius = current * 0.6 * this.scene.W; this.redrawMask(this.scene.CX, this.scene.CY, radius); }, onComplete: () => { this.setVisible(false); + this.transitionProgress = 0; }, }); } diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index 65cae5c..cca247e 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -1396,7 +1396,10 @@ export class GameScene extends BaseScene { } updateMusicState() { + const clamp = Phaser.Math.Clamp; + const tween = this.intermission.transitionProgress; const volumeModifier = 0.4; + let intendedVolume = { base: 1, cutscene: 0, @@ -1409,14 +1412,16 @@ export class GameScene extends BaseScene { downtime: 1, }; - if (this.intermission.visible) intendedVolume = { - base: 0, - cutscene: 1, - downtime: 0, - }; + if (this.intermission.visible) { + intendedVolume.cutscene = 1 - tween; + intendedVolume.downtime *= tween; + intendedVolume.base *= tween; + } + + console.debug(tween, intendedVolume) - this.musicBase.setVolume(intendedVolume.base * volumeModifier); - this.musicDowntime.setVolume(intendedVolume.downtime * volumeModifier); - this.musicCutscene.setVolume(intendedVolume.cutscene * volumeModifier); + this.musicBase.setVolume( clamp(intendedVolume.base, 0, 1) * volumeModifier); + this.musicDowntime.setVolume(clamp(intendedVolume.downtime, 0, 1) * volumeModifier); + this.musicCutscene.setVolume(clamp(intendedVolume.cutscene, 0, 1) * volumeModifier); } }