From 9a2c0bf036622dcf88fd06f49e42ee280a8b80e2 Mon Sep 17 00:00:00 2001 From: Chiquita Ahsanunnisa <16521248@mahasiswa.itb.ac.id> Date: Sat, 11 May 2024 14:32:01 +0700 Subject: [PATCH] fix: checking & stop on enter --- .../Resources/Data/Cutscene/Tutorial1.asset | 8 ++--- .../Resources/Data/Cutscene/Tutorial2.asset | 8 ++--- Assets/UI/InGame/Dialog/DialogController.cs | 35 +++++++++++++------ .../_Scripts/Core/Game/GameAudioController.cs | 9 +++-- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Assets/Resources/Data/Cutscene/Tutorial1.asset b/Assets/Resources/Data/Cutscene/Tutorial1.asset index 63fab5b6..02f12855 100644 --- a/Assets/Resources/Data/Cutscene/Tutorial1.asset +++ b/Assets/Resources/Data/Cutscene/Tutorial1.asset @@ -259,10 +259,10 @@ MonoBehaviour: animation: 2 audio: source: {fileID: 0} - name: - clip: {fileID: 0} - volume: 0 - pitch: 0 + name: swipe + clip: {fileID: 8300000, guid: fb9422424cdecad4196e684e4852c918, type: 3} + volume: 1 + pitch: 1 loop: 0 spatialize: 0 - Text: Gih sono cobain ke dummy di tengah map diff --git a/Assets/Resources/Data/Cutscene/Tutorial2.asset b/Assets/Resources/Data/Cutscene/Tutorial2.asset index e9dfbdbf..319fbc62 100644 --- a/Assets/Resources/Data/Cutscene/Tutorial2.asset +++ b/Assets/Resources/Data/Cutscene/Tutorial2.asset @@ -151,10 +151,10 @@ MonoBehaviour: animation: 2 audio: source: {fileID: 0} - name: - clip: {fileID: 0} - volume: 0 - pitch: 0 + name: swipe + clip: {fileID: 8300000, guid: fb9422424cdecad4196e684e4852c918, type: 3} + volume: 1 + pitch: 1 loop: 0 spatialize: 0 - Text: Gih sono cobain ke dummy di tengah map diff --git a/Assets/UI/InGame/Dialog/DialogController.cs b/Assets/UI/InGame/Dialog/DialogController.cs index adbfa5eb..73f482a7 100644 --- a/Assets/UI/InGame/Dialog/DialogController.cs +++ b/Assets/UI/InGame/Dialog/DialogController.cs @@ -19,6 +19,7 @@ public class DialogController : ScreenController private float endY = -50f; private Coroutine currentAnimationLeft; private Coroutine currentAnimationRight; + private static Audio currentAudio; // Events private event Action OnCutsceneFinished; @@ -60,11 +61,11 @@ public class DialogController : ScreenController public void ProgressCutscene() { StopAnimation(); - if(cutsceneProgress < currentCutscene.dialogs.Count) + if (cutsceneProgress < currentCutscene.dialogs.Count) { cutsceneProgress++; - if(cutsceneProgress == currentCutscene.dialogs.Count) + if (cutsceneProgress == currentCutscene.dialogs.Count) { GameController.Instance.stateController.PopState(); OnCutsceneFinished?.Invoke(); @@ -72,9 +73,16 @@ public class DialogController : ScreenController else { LoadData(currentCutscene.dialogs[cutsceneProgress]); - if(currentCutscene.dialogs[cutsceneProgress].audio.source != null) + if (GameAudioController.IsValidAudio(currentCutscene.dialogs[cutsceneProgress].audio)) { - GameAudioController.Instance.PlayOnce(currentCutscene.dialogs[cutsceneProgress].audio); + Debug.Log("Cutscene: Playing audio"); + currentAudio = currentCutscene.dialogs[cutsceneProgress].audio; + GameAudioController.Instance.PlayOnce(currentAudio); + } + else + { + Debug.Log("Cutscene: No audio"); + currentAudio = null; } } } @@ -108,7 +116,7 @@ public class DialogController : ScreenController dialogOverlay.m_PersonRImage.visible = data.PersonRActive; dialogOverlay.m_PersonRImage.style.backgroundImage = new StyleBackground(data.PersonRImage); dialogOverlay.m_PersonRLabelText.text = data.PersonRName; - + dialogOverlay.m_MainText.text = data.Text; Animate(data.animation); @@ -164,7 +172,7 @@ public class DialogController : ScreenController yield return new WaitForSecondsRealtime(0.001f); } dialogOverlay.m_PersonLImage.style.top = targetY; - + targetY = initialY; newY = endY; @@ -187,7 +195,7 @@ public class DialogController : ScreenController yield return new WaitForSecondsRealtime(0.001f); } dialogOverlay.m_PersonRImage.style.top = targetY; - + targetY = initialY; newY = endY; @@ -200,17 +208,24 @@ public class DialogController : ScreenController } dialogOverlay.m_PersonRImage.style.top = targetY; break; - + default: yield return null; break; - } + } } private void KeyPress(InputAction.CallbackContext context) { - if(InCutscene) + if (InCutscene) { + Debug.Log("Cutscene: In cutscene"); + if (currentAudio != null) + { + Debug.Log("Cutscene: Stopping current audio"); + Destroy(currentAudio.source); + GameAudioController.Instance.StopAllCoroutines(); + } ProgressCutscene(); } } diff --git a/Assets/_Scripts/Core/Game/GameAudioController.cs b/Assets/_Scripts/Core/Game/GameAudioController.cs index fee2206b..f4c475df 100644 --- a/Assets/_Scripts/Core/Game/GameAudioController.cs +++ b/Assets/_Scripts/Core/Game/GameAudioController.cs @@ -8,7 +8,7 @@ public class GameAudioController : MonoBehaviour protected void Awake() { - if(Instance != null) + if (Instance != null) { Destroy(gameObject); return; @@ -19,6 +19,11 @@ public class GameAudioController : MonoBehaviour audioController.Init(this); } + public static bool IsValidAudio(Audio audio) + { + return audio != null && audio.clip != null; + } + public void PlayOnce(Audio audio, float volume = 1) { audio.source = gameObject.AddComponent<AudioSource>(); @@ -44,7 +49,7 @@ public class GameAudioController : MonoBehaviour audio.spatialize = false; audio.Play(); - + StartCoroutine(DeleteClipWhenComplete(audio)); } -- GitLab