game(scripts): standardise InfoCollector

This commit is contained in:
Mark Joshwel 2025-02-15 00:33:49 +08:00
parent 489798de72
commit 0a75383050

View file

@ -1,13 +1,12 @@
/* /*
Author: Reza * Author: Reza
Date: 7/2/25 * Date: 7/2/25
Description: Collects information when a player looks at objects long enough * Description: Collects information when a player looks at objects long enough
*/ */
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
using TMPro; using TMPro;
using UnityEngine;
public class InfoCollector : MonoBehaviour public class InfoCollector : MonoBehaviour
{ {
@ -23,149 +22,129 @@ public class InfoCollector : MonoBehaviour
// Defines the UI text to display // Defines the UI text to display
public TMP_Text infoText; public TMP_Text infoText;
// Defines the camera
private Camera vrCamera;
// Defines whether UI is displaying to prevent spamming
private bool isDisplaying = false;
// Defines the object the player is currently looking at
private GameObject currentObject = null;
// Tracks how long the player has been looking at the object
private float gazeTimer = 0f;
// Defines Audio References // Defines Audio References
public AudioSource audioSource; public AudioSource audioSource;
public AudioClip scribbleSound; public AudioClip scribbleSound;
// Store objects that have already been collected
private HashSet<GameObject> collectedObjects = new HashSet<GameObject>();
void Start() // Store objects that have already been collected
private readonly HashSet<GameObject> _collectedObjects = new();
// Defines the object the player is currently looking at
private GameObject _currentObject;
// Tracks how long the player has been looking at the object
private float _gazeTimer;
// Defines whether UI is displaying to prevent spamming
private bool _isDisplaying;
// Defines the camera
private Camera _vrCamera;
private void Start()
{ {
// Assigns to player's camera // Assigns to player's camera
vrCamera = Camera.main; _vrCamera = Camera.main;
// Clear UI text initially // Clear UI text initially
infoText.text = ""; infoText.text = "";
} }
void Update() private void Update()
{ {
// Detects the direction the player is looking at // Detects the direction the player is looking at
Ray ray = new Ray(vrCamera.transform.position, vrCamera.transform.forward); var ray = new Ray(_vrCamera.transform.position, _vrCamera.transform.forward);
RaycastHit hit;
// Stores data of object hit in the detection range // Stores data of object hit in the detection range
if (Physics.Raycast(ray, out hit, detectionRange)) if (Physics.Raycast(ray, out var hit, detectionRange))
{ {
// Ensures that relevant info objects are detected // Ensures that relevant info objects are detected
if (hit.collider.CompareTag("InfoObject")) if (!hit.collider.CompareTag("InfoObject")) return;
var targetObject = hit.collider.gameObject;
// **Fix: Stop gaze timer if an object has been collected**
if (_collectedObjects.Contains(targetObject)) return; // Exit without increasing gaze time
// If the player is still looking at the same object, increase gaze time
if (_currentObject == targetObject)
{ {
GameObject targetObject = hit.collider.gameObject; _gazeTimer += Time.deltaTime;
// **Fix: Stop gaze timer if object has been collected** // If gaze time reaches the required time and info is not displayed yet
if (collectedObjects.Contains(targetObject)) if (_gazeTimer >= gazeTimeRequired && !_isDisplaying) CollectInfo(targetObject);
{ }
return; // Exit without increasing gaze time else
} {
// Reset timer when looking at a new object
// If the player is still looking at the same object, increase gaze time _currentObject = targetObject;
if (currentObject == targetObject) _gazeTimer = 0f;
{
gazeTimer += Time.deltaTime;
// If gaze time reaches required time and info is not displayed yet
if (gazeTimer >= gazeTimeRequired && !isDisplaying)
{
CollectInfo(targetObject);
}
}
else
{
// Reset timer when looking at a new object
currentObject = targetObject;
gazeTimer = 0f;
}
} }
} }
else else
{ {
// Reset if no valid object is in view // Reset if no valid object is in view
currentObject = null; _currentObject = null;
gazeTimer = 0f; _gazeTimer = 0f;
} }
} }
// Function to display object information // Function to display object information
void CollectInfo(GameObject obj) private void CollectInfo(GameObject obj)
{ {
// Prevents spamming of display // Prevents spamming of display
isDisplaying = true; _isDisplaying = true;
// **Fix: Mark object as collected** // **Fix: Mark object as collected**
collectedObjects.Add(obj); _collectedObjects.Add(obj);
// Displays information // Display information
infoText.text = "<size=15>Info Collected:</size>\n\n" + infoText.text = "<size=15>Info Collected:</size>\n\n" +
"<size=20>" + obj.name + "</size>\n" + "<size=20>" + obj.name + "</size>\n" +
"<size=16>" + GetObjectInfo(obj) + "</size>"; "<size=16>" + GetObjectInfo(obj) + "</size>";
Debug.Log("Collected information from: " + obj.name); Debug.Log("Collected information from: " + obj.name);
// Play sound only if no other sound is currently playing // Play sound only if no other sound is currently playing
if (!audioSource.isPlaying) if (!audioSource.isPlaying) audioSource.PlayOneShot(scribbleSound);
{
audioSource.PlayOneShot(scribbleSound);
}
// Clears text after displayed time // Clears text after displayed time
Invoke(nameof(ClearText), displayTime); Invoke(nameof(ClearText), displayTime);
} }
// Function to clear text after a delay // Function to clear text after a delay
void ClearText() private void ClearText()
{ {
// Removes text // Removes text
infoText.text = ""; infoText.text = "";
// Allows new information to be displayed // Allows new information to be displayed
isDisplaying = false; _isDisplaying = false;
} }
// mark : this info collecting mechanism is more of easter eggs and if the players are // for mark (backend): this info collecting mechanism is more of easter eggs and if the players are
// observant enough to look around the room / find out what the character is going through // observant enough to look around the room / find out what the character is going through
// can use this as statistics of what players tend to focus on // can use this as statistics of what players tend to focus on
string GetObjectInfo(GameObject obj) private static string GetObjectInfo(GameObject obj)
{ {
// Check if the object's name is the same return obj.name switch
if (obj.name == "Needles") {
// Check if the object's name is the same
// Returns predefined information // Returns predefined information
return "A used needle. If my parents finds out they would murder me."; "Needles" => "A used needle. If my parents finds out they would murder me.",
if (obj.name == "Bottles")
// Returns predefined information // Returns predefined information
return "Saw dad drink this... I like how it numbs the pain"; "Bottles" => "Saw dad drink this... I like how it numbs the pain",
if (obj.name == "Cigarettes")
// Returns predefined information // Returns predefined information
return "Stole this from mom. I hope she doesn't find out."; "Cigarettes" => "Stole this from mom. I hope she doesn't find out.",
if (obj.name == "Penknife")
// Returns predefined information // Returns predefined information
return "Sometimes I use this to feel something."; "Penknife" => "Sometimes I use this to feel something.",
if (obj.name == "Blood")
// Returns predefined information // Returns predefined information
return "I don't remember if daddy or I did this."; "Blood" => "I don't remember if daddy or I did this.",
if (obj.name == "ParentsDoor")
// Returns predefined information // Returns predefined information
return "My parents room. It's locked"; "ParentsDoor" => "My parents room. It's locked",
// Default information if there is no specific case
// Default information if there is no specific case _ => "There's nothing to look at."
return "There's nothing to look at."; };
} }
} }