wirm/Game/Assets/Scripts/BrushTeeth.cs

134 lines
3.8 KiB
C#
Raw Normal View History

2025-02-07 14:14:11 +08:00
/*
2025-02-14 22:12:46 +08:00
* Author: Wai Lam & Reza
* Date: 27/1/25
* Description: Bathroom interaction
*/
using System.Collections;
2025-02-14 22:07:23 +08:00
using TMPro;
2025-01-31 17:56:22 +08:00
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit;
2025-02-06 20:38:00 +08:00
using UnityEngine.XR.Interaction.Toolkit.Interactables;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
2025-01-31 17:56:22 +08:00
2025-02-06 23:12:45 +08:00
public class BrushTeeth : MonoBehaviour
2025-01-31 17:56:22 +08:00
{
public Slider progressBar; // Reference to the Slider (progress bar)
2025-01-31 17:56:22 +08:00
public float progressTime = 5f; // Time for the progress bar to complete
2025-02-14 22:07:23 +08:00
2025-02-11 09:55:06 +08:00
// Defines UI references
2025-02-14 22:07:23 +08:00
[Header("UI References")] public GameObject storyPanelUI;
2025-02-11 09:55:06 +08:00
public TMP_Text storyText;
2025-02-14 22:07:23 +08:00
2025-02-11 09:55:06 +08:00
// Defines Audio References
2025-02-14 22:07:23 +08:00
[Header("Audio References")] public AudioSource audioSource;
public AudioClip brushingSound;
2025-02-14 22:07:23 +08:00
private GameManager _gameManager;
private XRGrabInteractable _grabInteractable;
private bool _isGrabbing;
private bool _taskCompleted;
private float _timer;
2025-01-31 17:56:22 +08:00
2025-02-14 22:07:23 +08:00
private void Start()
2025-01-31 17:56:22 +08:00
{
2025-02-14 22:07:23 +08:00
_grabInteractable = GetComponent<XRGrabInteractable>();
2025-01-31 17:56:22 +08:00
2025-02-14 22:07:23 +08:00
if (_grabInteractable == null)
2025-01-31 17:56:22 +08:00
{
Debug.LogError("XRGrabInteractable component not found on the object!");
return;
}
// Ensure the progress bar is hidden initially
progressBar.gameObject.SetActive(false);
// Subscribe to grab and release events
2025-02-14 22:07:23 +08:00
_grabInteractable.selectEntered.AddListener(OnGrab);
_grabInteractable.selectExited.AddListener(OnRelease);
2025-01-31 17:56:22 +08:00
}
2025-02-14 22:07:23 +08:00
private void Update()
2025-01-31 17:56:22 +08:00
{
2025-02-14 22:07:23 +08:00
if (!_isGrabbing || _taskCompleted) return;
2025-02-14 22:19:28 +08:00
2025-02-14 22:07:23 +08:00
_timer += Time.deltaTime;
progressBar.value = _timer / progressTime;
2025-01-31 17:56:22 +08:00
2025-02-14 22:07:23 +08:00
if (_timer >= progressTime) CompleteProgress();
}
private void OnDestroy()
{
if (_grabInteractable == null) return;
2025-02-14 22:19:28 +08:00
2025-02-14 22:07:23 +08:00
_grabInteractable.selectEntered.RemoveListener(OnGrab);
_grabInteractable.selectExited.RemoveListener(OnRelease);
2025-01-31 17:56:22 +08:00
}
private void OnGrab(SelectEnterEventArgs args)
{
2025-02-06 20:38:00 +08:00
// Ignore if grabbed by a socket interactor
2025-02-14 22:07:23 +08:00
if (args.interactorObject.transform.GetComponent<XRSocketInteractor>() !=
null) return; // Do nothing if grabbed by a socket
2025-02-06 20:38:00 +08:00
// Only show progress bar if NOT grabbed by a socket
2025-01-31 17:56:22 +08:00
progressBar.gameObject.SetActive(true);
progressBar.value = 0f;
2025-02-14 22:07:23 +08:00
_timer = 0f;
_isGrabbing = true;
// Play brushing sound while toothbrush is grabbed, plays only if it isn't playing already
2025-02-14 22:07:23 +08:00
if (audioSource.isPlaying) return;
2025-02-14 22:19:28 +08:00
2025-02-14 22:07:23 +08:00
audioSource.clip = brushingSound;
// Loops the sound for as long as the toothbrush is held; allows editable progress time
audioSource.loop = true;
audioSource.Play();
2025-01-31 17:56:22 +08:00
}
private void OnRelease(SelectExitEventArgs args)
{
2025-02-06 20:38:00 +08:00
// Stop progress when released, regardless of interactor type
2025-01-31 17:56:22 +08:00
progressBar.gameObject.SetActive(false);
2025-02-14 22:07:23 +08:00
_isGrabbing = false;
_timer = 0f;
// Stop the brushing sound when the toothbrush is released
2025-02-14 22:07:23 +08:00
if (audioSource.isPlaying) audioSource.Stop();
2025-01-31 17:56:22 +08:00
}
private void CompleteProgress()
{
2025-02-14 22:07:23 +08:00
if (_taskCompleted) return;
_taskCompleted = true;
2025-01-31 17:56:22 +08:00
progressBar.gameObject.SetActive(false);
2025-02-14 22:07:23 +08:00
_isGrabbing = false;
// Stop the brushing sound when the task is completed
2025-02-14 22:07:23 +08:00
if (audioSource.isPlaying) audioSource.Stop();
GameManager.Instance.BrushTeethTaskComplete();
2025-02-14 22:07:23 +08:00
storyPanelUI.SetActive(true);
storyText.text = "I should be fresh enough to go to school now...";
2025-02-11 15:01:17 +08:00
// Clear the text after a delay
StartCoroutine(ClearMessageAfterSeconds(7f));
2025-01-31 17:56:22 +08:00
Debug.Log("Progress completed!");
}
2025-02-11 15:01:17 +08:00
private IEnumerator ClearMessageAfterSeconds(float delay)
2025-02-11 09:55:06 +08:00
{
// Waits for delay to end and hides the UI
yield return new WaitForSeconds(delay);
2025-02-11 15:01:17 +08:00
storyText.text = "";
2025-02-11 09:55:06 +08:00
}
2025-02-14 22:07:23 +08:00
}