From 81ba0e8385c7a8a38b8277a5d386e4994f77eb39 Mon Sep 17 00:00:00 2001 From: Constantin Rusu Date: Mon, 23 Dec 2024 15:40:56 +0200 Subject: [PATCH] soundtrack trigger fix --- src/components/GameBackground.tsx | 29 +++++++++++++---------------- src/components/game/IntroDialog.tsx | 13 +++++++++++-- src/pages/Index.tsx | 15 ++++++++------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/components/GameBackground.tsx b/src/components/GameBackground.tsx index 422c0d2..714d309 100644 --- a/src/components/GameBackground.tsx +++ b/src/components/GameBackground.tsx @@ -1,24 +1,21 @@ import { useEffect, useState } from "react"; -export const GameBackground = () => { +interface GameBackgroundProps { + shouldStartAudio?: boolean; +} + +export const GameBackground = ({ shouldStartAudio = false }: GameBackgroundProps) => { const [audioStarted, setAudioStarted] = useState(false); useEffect(() => { - // Only start audio after user interaction - const handleFirstInteraction = () => { - if (!audioStarted) { - const audio = new Audio("/tension-background.mp3"); - audio.loop = true; - audio.volume = 0.3; - audio.play().catch(console.error); - setAudioStarted(true); - document.removeEventListener("click", handleFirstInteraction); - } - }; - - document.addEventListener("click", handleFirstInteraction); - return () => document.removeEventListener("click", handleFirstInteraction); - }, [audioStarted]); + if (shouldStartAudio && !audioStarted) { + const audio = new Audio("/tension-background.mp3"); + audio.loop = true; + audio.volume = 0.3; + audio.play().catch(console.error); + setAudioStarted(true); + } + }, [shouldStartAudio, audioStarted]); return (
diff --git a/src/components/game/IntroDialog.tsx b/src/components/game/IntroDialog.tsx index a961ab4..61de2ac 100644 --- a/src/components/game/IntroDialog.tsx +++ b/src/components/game/IntroDialog.tsx @@ -11,10 +11,19 @@ import { useState } from "react"; import { useTranslation } from "react-i18next"; import { LanguageSwitcher } from "@/components/LanguageSwitcher"; -export const IntroDialog = () => { +interface IntroDialogProps { + onStartAudio?: () => void; +} + +export const IntroDialog = ({ onStartAudio }: IntroDialogProps) => { const [open, setOpen] = useState(true); const { t } = useTranslation(); + const handleBeginSimulation = () => { + setOpen(false); + onStartAudio?.(); + }; + return ( @@ -57,7 +66,7 @@ export const IntroDialog = () => {