Compare commits
2 commits
57333686a3
...
337e022356
Author | SHA1 | Date | |
---|---|---|---|
337e022356 | |||
e4b5522992 |
2 changed files with 73 additions and 91 deletions
|
@ -1,46 +1,43 @@
|
||||||
/*
|
/*
|
||||||
Author: Wai Lam & Reza
|
Author: Wai Lam & Reza
|
||||||
Date: 27/1/25
|
Date: 27/1/25
|
||||||
Description: Bathroom interaction
|
Description: Bathroom interaction
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
using UnityEngine.XR.Interaction.Toolkit;
|
using UnityEngine.XR.Interaction.Toolkit;
|
||||||
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
||||||
using UnityEngine.XR.Interaction.Toolkit.Interactors;
|
using UnityEngine.XR.Interaction.Toolkit.Interactors;
|
||||||
using TMPro;
|
|
||||||
|
|
||||||
public class BrushTeeth : MonoBehaviour
|
public class BrushTeeth : MonoBehaviour
|
||||||
{
|
{
|
||||||
private GameManager gameManager;
|
|
||||||
|
|
||||||
public Slider progressBar; // Reference to the Slider (progress bar)
|
public Slider progressBar; // Reference to the Slider (progress bar)
|
||||||
public float progressTime = 5f; // Time for the progress bar to complete
|
public float progressTime = 5f; // Time for the progress bar to complete
|
||||||
private XRGrabInteractable grabInteractable;
|
|
||||||
private float timer = 0f;
|
|
||||||
private bool isGrabbing = false;
|
|
||||||
|
|
||||||
private bool taskCompleted = false;
|
|
||||||
|
|
||||||
// Defines UI references
|
// Defines UI references
|
||||||
[Header("UI References")]
|
[Header("UI References")] public GameObject storyPanelUI;
|
||||||
|
|
||||||
public GameObject storyPanelUI;
|
|
||||||
public TMP_Text storyText;
|
public TMP_Text storyText;
|
||||||
|
|
||||||
// Defines Audio References
|
// Defines Audio References
|
||||||
[Header("Audio References")]
|
[Header("Audio References")] public AudioSource audioSource;
|
||||||
public AudioSource audioSource;
|
|
||||||
public AudioClip brushingSound;
|
public AudioClip brushingSound;
|
||||||
|
private GameManager _gameManager;
|
||||||
|
private XRGrabInteractable _grabInteractable;
|
||||||
|
private bool _isGrabbing;
|
||||||
|
|
||||||
void Start()
|
private bool _taskCompleted;
|
||||||
|
private float _timer;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
{
|
{
|
||||||
grabInteractable = GetComponent<XRGrabInteractable>();
|
_grabInteractable = GetComponent<XRGrabInteractable>();
|
||||||
|
|
||||||
if (grabInteractable == null)
|
if (_grabInteractable == null)
|
||||||
{
|
{
|
||||||
Debug.LogError("XRGrabInteractable component not found on the object!");
|
Debug.LogError("XRGrabInteractable component not found on the object!");
|
||||||
return;
|
return;
|
||||||
|
@ -50,80 +47,75 @@ public class BrushTeeth : MonoBehaviour
|
||||||
progressBar.gameObject.SetActive(false);
|
progressBar.gameObject.SetActive(false);
|
||||||
|
|
||||||
// Subscribe to grab and release events
|
// Subscribe to grab and release events
|
||||||
grabInteractable.selectEntered.AddListener(OnGrab);
|
_grabInteractable.selectEntered.AddListener(OnGrab);
|
||||||
grabInteractable.selectExited.AddListener(OnRelease);
|
_grabInteractable.selectExited.AddListener(OnRelease);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (isGrabbing && !taskCompleted)
|
if (!_isGrabbing || _taskCompleted) return;
|
||||||
{
|
|
||||||
timer += Time.deltaTime;
|
_timer += Time.deltaTime;
|
||||||
progressBar.value = timer / progressTime;
|
progressBar.value = _timer / progressTime;
|
||||||
|
|
||||||
if (timer >= progressTime)
|
if (_timer >= progressTime) CompleteProgress();
|
||||||
{
|
}
|
||||||
CompleteProgress();
|
|
||||||
}
|
private void OnDestroy()
|
||||||
}
|
{
|
||||||
|
if (_grabInteractable == null) return;
|
||||||
|
|
||||||
|
_grabInteractable.selectEntered.RemoveListener(OnGrab);
|
||||||
|
_grabInteractable.selectExited.RemoveListener(OnRelease);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnGrab(SelectEnterEventArgs args)
|
private void OnGrab(SelectEnterEventArgs args)
|
||||||
{
|
{
|
||||||
// Ignore if grabbed by a socket interactor
|
// Ignore if grabbed by a socket interactor
|
||||||
if (args.interactorObject.transform.GetComponent<XRSocketInteractor>() != null)
|
if (args.interactorObject.transform.GetComponent<XRSocketInteractor>() !=
|
||||||
{
|
null) return; // Do nothing if grabbed by a socket
|
||||||
return; // Do nothing if grabbed by a socket
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only show progress bar if NOT grabbed by a socket
|
// Only show progress bar if NOT grabbed by a socket
|
||||||
progressBar.gameObject.SetActive(true);
|
progressBar.gameObject.SetActive(true);
|
||||||
progressBar.value = 0f;
|
progressBar.value = 0f;
|
||||||
timer = 0f;
|
_timer = 0f;
|
||||||
isGrabbing = true;
|
_isGrabbing = true;
|
||||||
|
|
||||||
// Play brushing sound while toothbrush is grabbed, plays only if it isn't playing already
|
// Play brushing sound while toothbrush is grabbed, plays only if it isn't playing already
|
||||||
if (!audioSource.isPlaying)
|
if (audioSource.isPlaying) return;
|
||||||
{
|
|
||||||
audioSource.clip = brushingSound;
|
audioSource.clip = brushingSound;
|
||||||
|
|
||||||
// Loops the sound for as long as the toothbrush is held; allows editable progress time
|
// Loops the sound for as long as the toothbrush is held; allows editable progress time
|
||||||
audioSource.loop = true;
|
audioSource.loop = true;
|
||||||
|
|
||||||
audioSource.Play();
|
audioSource.Play();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnRelease(SelectExitEventArgs args)
|
private void OnRelease(SelectExitEventArgs args)
|
||||||
{
|
{
|
||||||
// Stop progress when released, regardless of interactor type
|
// Stop progress when released, regardless of interactor type
|
||||||
progressBar.gameObject.SetActive(false);
|
progressBar.gameObject.SetActive(false);
|
||||||
isGrabbing = false;
|
_isGrabbing = false;
|
||||||
timer = 0f;
|
_timer = 0f;
|
||||||
|
|
||||||
// Stop the brushing sound when the toothbrush is released
|
// Stop the brushing sound when the toothbrush is released
|
||||||
if (audioSource.isPlaying)
|
if (audioSource.isPlaying) audioSource.Stop();
|
||||||
{
|
|
||||||
audioSource.Stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CompleteProgress()
|
private void CompleteProgress()
|
||||||
{
|
{
|
||||||
if (taskCompleted) return;
|
if (_taskCompleted) return;
|
||||||
|
|
||||||
taskCompleted = true;
|
_taskCompleted = true;
|
||||||
progressBar.gameObject.SetActive(false);
|
progressBar.gameObject.SetActive(false);
|
||||||
isGrabbing = false;
|
_isGrabbing = false;
|
||||||
|
|
||||||
// Stop the brushing sound when the task is completed
|
// Stop the brushing sound when the task is completed
|
||||||
if (audioSource.isPlaying)
|
if (audioSource.isPlaying) audioSource.Stop();
|
||||||
{
|
|
||||||
audioSource.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
GameManager.Instance.BrushTeethTaskComplete();
|
GameManager.Instance.BrushTeethTaskComplete();
|
||||||
|
|
||||||
storyPanelUI.SetActive(true);
|
storyPanelUI.SetActive(true);
|
||||||
storyText.text = "I should be fresh enough to go to school now...";
|
storyText.text = "I should be fresh enough to go to school now...";
|
||||||
|
|
||||||
|
@ -133,19 +125,10 @@ public class BrushTeeth : MonoBehaviour
|
||||||
Debug.Log("Progress completed!");
|
Debug.Log("Progress completed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
|
||||||
{
|
|
||||||
if (grabInteractable != null)
|
|
||||||
{
|
|
||||||
grabInteractable.selectEntered.RemoveListener(OnGrab);
|
|
||||||
grabInteractable.selectExited.RemoveListener(OnRelease);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerator ClearMessageAfterSeconds(float delay)
|
private IEnumerator ClearMessageAfterSeconds(float delay)
|
||||||
{
|
{
|
||||||
// Waits for delay to end and hides the UI
|
// Waits for delay to end and hides the UI
|
||||||
yield return new WaitForSeconds(delay);
|
yield return new WaitForSeconds(delay);
|
||||||
storyText.text = "";
|
storyText.text = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,32 +1,32 @@
|
||||||
/*
|
/*
|
||||||
Author : Wai Lam
|
* Author: Wai Lam
|
||||||
Date : 13/2/2025
|
* Date: 13/2/2025
|
||||||
Description : Counting days
|
* Description: Counting days
|
||||||
*/
|
*/
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.SceneManagement;
|
|
||||||
using TMPro;
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using TMPro;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
public class CanvasFade : MonoBehaviour
|
public class CanvasFade : MonoBehaviour
|
||||||
{
|
{
|
||||||
public CanvasGroup dayPanel; // Assign the UI Panel
|
public CanvasGroup dayPanel; // Assign the UI Panel
|
||||||
public TextMeshProUGUI dayText; // Assign the TextMeshPro UI
|
public TextMeshProUGUI dayText; // Assign the TextMeshPro UI
|
||||||
|
|
||||||
public static event Action OnDayPanelHidden;
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
if (GameManager.Instance != null)
|
if (GameManager.Instance != null)
|
||||||
{
|
{
|
||||||
int dayNumber = GameManager.Instance.currentDay;
|
var dayNumber = GameManager.Instance.currentDay;
|
||||||
dayText.text = "Day " + dayNumber;
|
dayText.text = "Day " + dayNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
StartCoroutine(FadeOutPanel());
|
StartCoroutine(FadeOutPanel());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static event Action OnDayPanelHidden;
|
||||||
|
|
||||||
private IEnumerator FadeOutPanel()
|
private IEnumerator FadeOutPanel()
|
||||||
{
|
{
|
||||||
dayPanel.alpha = 1f;
|
dayPanel.alpha = 1f;
|
||||||
|
@ -34,8 +34,8 @@ public class CanvasFade : MonoBehaviour
|
||||||
|
|
||||||
yield return new WaitForSeconds(3f); // Display for 3 seconds
|
yield return new WaitForSeconds(3f); // Display for 3 seconds
|
||||||
|
|
||||||
float fadeDuration = 1.5f;
|
const float fadeDuration = 1.5f;
|
||||||
float elapsedTime = 0f;
|
var elapsedTime = 0f;
|
||||||
|
|
||||||
while (elapsedTime < fadeDuration)
|
while (elapsedTime < fadeDuration)
|
||||||
{
|
{
|
||||||
|
@ -45,8 +45,7 @@ public class CanvasFade : MonoBehaviour
|
||||||
}
|
}
|
||||||
|
|
||||||
dayPanel.gameObject.SetActive(false);
|
dayPanel.gameObject.SetActive(false);
|
||||||
|
|
||||||
OnDayPanelHidden?.Invoke();
|
|
||||||
|
|
||||||
|
OnDayPanelHidden?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue