From 1b18ab67b74023365125817e4718e38df63320bf Mon Sep 17 00:00:00 2001 From: rezazfn Date: Fri, 7 Feb 2025 11:15:46 +0800 Subject: [PATCH] game(assets): added info collecting script and fixed old scripts --- Game/Assets/Scripts/BedroomTask.cs | 87 +++++++++++++++++++---- Game/Assets/Scripts/InfoCollector.cs | 66 +++++++++++++++++ Game/Assets/Scripts/InfoCollector.cs.meta | 11 +++ Game/Assets/Scripts/Letter.cs | 1 + Game/Assets/Scripts/Trash.cs | 26 +++++-- 5 files changed, 172 insertions(+), 19 deletions(-) create mode 100644 Game/Assets/Scripts/InfoCollector.cs create mode 100644 Game/Assets/Scripts/InfoCollector.cs.meta diff --git a/Game/Assets/Scripts/BedroomTask.cs b/Game/Assets/Scripts/BedroomTask.cs index f054204..008b365 100644 --- a/Game/Assets/Scripts/BedroomTask.cs +++ b/Game/Assets/Scripts/BedroomTask.cs @@ -9,24 +9,37 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.ProBuilder.Shapes; using UnityEngine.XR.Interaction.Toolkit.Interactables; +using UnityEngine.XR.Interaction.Toolkit; public class BedroomTask : MonoBehaviour { public int trashCollected = 0; - public int trashToUnlock = 1; - public GameObject door; - public XRGrabInteractable doorInteractable; - - // Start is called before the first frame update + public int trashRequired = 10; + public GameObject door; // The VR swingable door object + private XRGrabInteractable grabInteractable; + private Collider doorCollider; + private Rigidbody doorRigidbody; + public GameObject lockedDoorUI; + public GameObject unlockedDoorUI; + void Start() { + lockedDoorUI.SetActive(false); + unlockedDoorUI.SetActive(false); if (door != null) { - doorInteractable = door.GetComponent(); - if (doorInteractable != null) + grabInteractable = door.GetComponent(); + doorCollider = door.GetComponent(); + doorRigidbody = door.GetComponent(); + + if (grabInteractable != null) { - doorInteractable.enabled = false; - Debug.Log("Room is not cleaned! Door is locked"); + grabInteractable.selectEntered.AddListener(OnDoorGrabAttempt); + } + + if (doorRigidbody != null) + { + LockDoor(); } } } @@ -34,18 +47,62 @@ public class BedroomTask : MonoBehaviour public void CollectTrash() { trashCollected++; - Debug.Log("Trash Collected: " + trashCollected + "/" + trashToUnlock); + Debug.Log($"🗑️ Trash collected: {trashCollected}/{trashRequired}"); - if (trashCollected >= trashToUnlock) + if (trashCollected >= trashRequired) { UnlockDoor(); - Debug.Log("Cleaning task completed! Unlocking door..."); + } + } + + private void LockDoor() + { + if (doorRigidbody != null) + { + doorRigidbody.isKinematic = true; // Stops movement + doorRigidbody.constraints = RigidbodyConstraints.FreezeAll; // Completely freezes door + } + + if (doorCollider != null) + { + doorCollider.enabled = false; // Prevents grabbing } } private void UnlockDoor() { - doorInteractable.enabled = true; - Debug.Log("Room is cleaned! Door unlocked"); + if (doorRigidbody != null) + { + doorRigidbody.isKinematic = false; // Allows movement + doorRigidbody.constraints = RigidbodyConstraints.None; // Removes restrictions + } + + if (doorCollider != null) + { + doorCollider.enabled = true; // Enables grabbing + } + + StartCoroutine(HideMessageAfterSeconds(unlockedDoorUI, 5f)); + Debug.Log("Room is clean! The door is now unlocked."); } -} + + private void OnDoorGrabAttempt(SelectEnterEventArgs args) + { + if (trashCollected < trashRequired) + { + StartCoroutine(HideMessageAfterSeconds(lockedDoorUI, 5f)); + Debug.Log("The door is locked! Clean the room first."); + } + } + + public bool IsTaskCompleted() + { + return trashCollected >= trashRequired; + } + + private IEnumerator HideMessageAfterSeconds(GameObject uiElement, float delay) + { + yield return new WaitForSeconds(delay); + uiElement.SetActive(false); + } +} \ No newline at end of file diff --git a/Game/Assets/Scripts/InfoCollector.cs b/Game/Assets/Scripts/InfoCollector.cs new file mode 100644 index 0000000..03f6c83 --- /dev/null +++ b/Game/Assets/Scripts/InfoCollector.cs @@ -0,0 +1,66 @@ +/* +Author: Reza +Date: 7/2/25 +Description: Collects information when a player looks at objects long enough +*/ + +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TMPro; + +public class InfoCollector : MonoBehaviour +{ + public float detectionRange = 5f; + public TMP_Text infoText; // Reference to a UI Text (TextMeshPro) + private Camera vrCamera; + private bool isDisplaying = false; + + void Start() + { + vrCamera = Camera.main; + infoText.text = ""; // Clear text initially + } + + void Update() + { + Ray ray = new Ray(vrCamera.transform.position, vrCamera.transform.forward); + RaycastHit hit; + + if (Physics.Raycast(ray, out hit, detectionRange)) + { + if (hit.collider.CompareTag("InfoObject") && !isDisplaying) + { + CollectInfo(hit.collider.gameObject); + } + } + } + + void CollectInfo(GameObject obj) + { + isDisplaying = true; + infoText.text = "Info Collected: " + obj.name + "\n" + GetObjectInfo(obj); + Debug.Log("Collected information from: " + obj.name); + + // Hide text after 3 seconds + Invoke("ClearText", 3f); + } + + void ClearText() + { + infoText.text = ""; + isDisplaying = false; + } + + string GetObjectInfo(GameObject obj) + { + // Define object descriptions here + if (obj.name == "AncientArtifact") + return "This artifact was used by an old civilization..."; + + if (obj.name == "HologramTerminal") + return "A futuristic data console storing historical records."; + + return "An unknown object with mysterious origins."; + } +} diff --git a/Game/Assets/Scripts/InfoCollector.cs.meta b/Game/Assets/Scripts/InfoCollector.cs.meta new file mode 100644 index 0000000..b6a768c --- /dev/null +++ b/Game/Assets/Scripts/InfoCollector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6cb4698acb16cc94e8caf48cad7156c4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Game/Assets/Scripts/Letter.cs b/Game/Assets/Scripts/Letter.cs index 9725f4c..ff59718 100644 --- a/Game/Assets/Scripts/Letter.cs +++ b/Game/Assets/Scripts/Letter.cs @@ -12,6 +12,7 @@ using UnityEngine.UIElements; public class Letter : MonoBehaviour { public GameObject letterUI; + public AudioClip scribble; private void Start() { diff --git a/Game/Assets/Scripts/Trash.cs b/Game/Assets/Scripts/Trash.cs index 85d5e9c..96183b3 100644 --- a/Game/Assets/Scripts/Trash.cs +++ b/Game/Assets/Scripts/Trash.cs @@ -12,13 +12,31 @@ public class Trash : MonoBehaviour { private BedroomTask bedroomTask; + private void Start() + { + // Find the BedroomTask script in the scene + bedroomTask = FindObjectOfType(); + + if (bedroomTask == null) + { + Debug.LogError("BedroomTask script not found in the scene!"); + } + } + private void OnTriggerEnter(Collider other) { if (other.CompareTag("TrashBin")) { - bedroomTask.CollectTrash(); - Debug.Log("Trash thrown in the bin! Count: " + bedroomTask.trashCollected); - Destroy(gameObject); + if (bedroomTask != null) + { + bedroomTask.CollectTrash(); + Debug.Log("🗑️ Trash thrown in the bin! Count: " + bedroomTask.trashCollected); + Destroy(gameObject); + } + else + { + Debug.LogError("BedroomTask reference is missing!"); + } } } -} +} \ No newline at end of file