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
Date: 7/2/25
Description: Collects information when a player looks at objects long enough
*/
* 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;
using UnityEngine;
public class InfoCollector : MonoBehaviour
{
@ -23,149 +22,129 @@ public class InfoCollector : MonoBehaviour
// Defines the UI text to display
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
public AudioSource audioSource;
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
vrCamera = Camera.main;
_vrCamera = Camera.main;
// Clear UI text initially
infoText.text = "";
}
void Update()
private void Update()
{
// Detects the direction the player is looking at
Ray ray = new Ray(vrCamera.transform.position, vrCamera.transform.forward);
RaycastHit hit;
var ray = new Ray(_vrCamera.transform.position, _vrCamera.transform.forward);
// 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
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 (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)
{
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;
}
// If gaze time reaches the 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
{
// Reset if no valid object is in view
currentObject = null;
gazeTimer = 0f;
_currentObject = null;
_gazeTimer = 0f;
}
}
// Function to display object information
void CollectInfo(GameObject obj)
private void CollectInfo(GameObject obj)
{
// Prevents spamming of display
isDisplaying = true;
_isDisplaying = true;
// **Fix: Mark object as collected**
collectedObjects.Add(obj);
_collectedObjects.Add(obj);
// Displays information
infoText.text = "<size=15>Info Collected:</size>\n\n" +
"<size=20>" + obj.name + "</size>\n" +
// Display information
infoText.text = "<size=15>Info Collected:</size>\n\n" +
"<size=20>" + obj.name + "</size>\n" +
"<size=16>" + GetObjectInfo(obj) + "</size>";
Debug.Log("Collected information from: " + obj.name);
// Play sound only if no other sound is currently playing
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(scribbleSound);
}
if (!audioSource.isPlaying) audioSource.PlayOneShot(scribbleSound);
// Clears text after displayed time
Invoke(nameof(ClearText), displayTime);
}
// Function to clear text after a delay
void ClearText()
private void ClearText()
{
// Removes text
infoText.text = "";
// 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
// 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
if (obj.name == "Needles")
return obj.name switch
{
// Check if the object's name is the same
// Returns predefined information
return "A used needle. If my parents finds out they would murder me.";
if (obj.name == "Bottles")
"Needles" => "A used needle. If my parents finds out they would murder me.",
// Returns predefined information
return "Saw dad drink this... I like how it numbs the pain";
if (obj.name == "Cigarettes")
"Bottles" => "Saw dad drink this... I like how it numbs the pain",
// Returns predefined information
return "Stole this from mom. I hope she doesn't find out.";
if (obj.name == "Penknife")
"Cigarettes" => "Stole this from mom. I hope she doesn't find out.",
// Returns predefined information
return "Sometimes I use this to feel something.";
if (obj.name == "Blood")
"Penknife" => "Sometimes I use this to feel something.",
// Returns predefined information
return "I don't remember if daddy or I did this.";
if (obj.name == "ParentsDoor")
"Blood" => "I don't remember if daddy or I did this.",
// Returns predefined information
return "My parents room. It's locked";
// Default information if there is no specific case
return "There's nothing to look at.";
"ParentsDoor" => "My parents room. It's locked",
// Default information if there is no specific case
_ => "There's nothing to look at."
};
}
}