From d8d40f1aa5c1e5f20bd62653c406526ecfffaf4f Mon Sep 17 00:00:00 2001 From: rezazfn Date: Mon, 10 Feb 2025 00:56:32 +0800 Subject: [PATCH] game(scripts): polished bedroom task and brush teeth task to update gamemanager --- Game/Assets/Scenes/House.unity | 10 ++++++++- Game/Assets/Scripts/BedroomTask.cs | 13 +++++++++--- Game/Assets/Scripts/BrushTeeth.cs | 33 ++++++++++++++++++++++++++++-- Game/Assets/Scripts/GameManager.cs | 5 +++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Game/Assets/Scenes/House.unity b/Game/Assets/Scenes/House.unity index 1dde8a8..19d6164 100644 --- a/Game/Assets/Scenes/House.unity +++ b/Game/Assets/Scenes/House.unity @@ -86823,10 +86823,18 @@ PrefabInstance: serializedVersion: 3 m_TransformParent: {fileID: 745726886} m_Modifications: + - target: {fileID: 806528791475834742, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} + propertyPath: audioSource + value: + objectReference: {fileID: 1898819871} - target: {fileID: 806528791475834742, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} propertyPath: progressBar value: objectReference: {fileID: 1902891356} + - target: {fileID: 806528791475834742, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} + propertyPath: brushingSound + value: + objectReference: {fileID: 8300000, guid: 9cf02b0277ad0324389ae25a9fefe8ba, type: 3} - target: {fileID: 3017716152663212623, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} propertyPath: m_LocalPosition.x value: -0.184021 @@ -86873,7 +86881,7 @@ PrefabInstance: objectReference: {fileID: 430002287} - target: {fileID: 3408018161587624474, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} propertyPath: m_VersionIndex - value: 1679 + value: 1685 objectReference: {fileID: 0} - target: {fileID: 5470122699805207955, guid: 519c364a8a1b96b428f69317d2cfb87f, type: 3} propertyPath: m_Mesh diff --git a/Game/Assets/Scripts/BedroomTask.cs b/Game/Assets/Scripts/BedroomTask.cs index 7de5792..992cdf0 100644 --- a/Game/Assets/Scripts/BedroomTask.cs +++ b/Game/Assets/Scripts/BedroomTask.cs @@ -14,6 +14,8 @@ using UnityEngine.XR.Interaction.Toolkit; public class BedroomTask : MonoBehaviour { + private GameManager gameManager; + [Header("Task Requirement Values")] // To track how much trash has been collected so far public int trashCollected = 0; @@ -99,7 +101,7 @@ public class BedroomTask : MonoBehaviour // If player has collected/thrown required amount of trash if (trashCollected >= trashRequired) { - Debug.Log("Trash requirement met! Starting sound sequence..."); + GameManager.Instance.BedroomTaskComplete(); // Call unlocking door function/sequence StartCoroutine(PlaySoundSequence()); @@ -147,6 +149,12 @@ public class BedroomTask : MonoBehaviour doorCollider.enabled = true; } + // Play sound only if no other sound is currently playing + if (!audioSource.isPlaying) + { + audioSource.PlayOneShot(unlockedSound); + } + // Show the unlocked door UI unlockedDoorUI.SetActive(true); @@ -185,8 +193,6 @@ public class BedroomTask : MonoBehaviour private IEnumerator PlaySoundSequence() { - Debug.Log("Starting PlaySoundSequence..."); - // Play footsteps of parents walking away audioSource.PlayOneShot(footstepsSound); yield return new WaitForSeconds(footstepsSound.length); @@ -197,6 +203,7 @@ public class BedroomTask : MonoBehaviour // Unlocks the door after the clips and update the story UnlockDoor(); + storyText.text = "They finally left... just as soon as I finished cleaning. I can leave the room now."; storyPanelUI.SetActive(true); StartCoroutine(HideMessageAfterSeconds(storyPanelUI, 10f)); diff --git a/Game/Assets/Scripts/BrushTeeth.cs b/Game/Assets/Scripts/BrushTeeth.cs index 7ba082e..4ea22cd 100644 --- a/Game/Assets/Scripts/BrushTeeth.cs +++ b/Game/Assets/Scripts/BrushTeeth.cs @@ -1,5 +1,5 @@ /* -Author: Wai Lam +Author: Wai Lam & Reza Date: 27/1/25 Description: Bathroom interaction */ @@ -14,13 +14,17 @@ using UnityEngine.XR.Interaction.Toolkit.Interactors; public class BrushTeeth : MonoBehaviour { - public PostProcessingManager postProcessingManager; + private GameManager gameManager; public Slider progressBar; // Reference to the Slider (progress bar) public float progressTime = 5f; // Time for the progress bar to complete private XRGrabInteractable grabInteractable; private float timer = 0f; private bool isGrabbing = false; + + // Audio References + public AudioSource audioSource; + public AudioClip brushingSound; void Start() { @@ -67,6 +71,17 @@ public class BrushTeeth : MonoBehaviour progressBar.value = 0f; timer = 0f; isGrabbing = true; + + // Play brushing sound while toothbrush is grabbed, plays only if it isn't playing already + if (!audioSource.isPlaying) + { + audioSource.clip = brushingSound; + + // Loops the sound for as long as the toothbrush is held; allows editable progress time + audioSource.loop = true; + + audioSource.Play(); + } } private void OnRelease(SelectExitEventArgs args) @@ -75,12 +90,26 @@ public class BrushTeeth : MonoBehaviour progressBar.gameObject.SetActive(false); isGrabbing = false; timer = 0f; + + // Stop the brushing sound when the toothbrush is released + if (audioSource.isPlaying) + { + audioSource.Stop(); + } } private void CompleteProgress() { progressBar.gameObject.SetActive(false); isGrabbing = false; + + // Stop the brushing sound when the task is completed + if (audioSource.isPlaying) + { + audioSource.Stop(); + } + + GameManager.Instance.BrushTeethTaskComplete(); Debug.Log("Progress completed!"); } diff --git a/Game/Assets/Scripts/GameManager.cs b/Game/Assets/Scripts/GameManager.cs index 5232fbf..18625fe 100644 --- a/Game/Assets/Scripts/GameManager.cs +++ b/Game/Assets/Scripts/GameManager.cs @@ -65,4 +65,9 @@ public class GameManager : MonoBehaviour { bedroomCleaned = true; } + + public void BrushTeethTaskComplete() + { + teethBrushed = true; + } }