game(scripts): standardise PhoneInteraction

This commit is contained in:
Mark Joshwel 2025-02-15 00:36:07 +08:00
parent 05367fb7ae
commit f226e778f4

View file

@ -1,5 +1,10 @@
/*
* Author: Isaac
* Date: 11/2/25
* Description: Post-processing camera effects emulating various conditions
*/
using UnityEngine; using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
public class PhoneInteraction : MonoBehaviour public class PhoneInteraction : MonoBehaviour
@ -8,70 +13,53 @@ public class PhoneInteraction : MonoBehaviour
public GameObject choiceUI; // Assign your UI Panel in Inspector public GameObject choiceUI; // Assign your UI Panel in Inspector
public Transform attachTransform; // Drag XR Controller's Attach Transform here public Transform attachTransform; // Drag XR Controller's Attach Transform here
private AudioSource audioSource; private AudioSource _audioSource;
private bool phonePickedUp = false; private bool _choiceMade;
private bool choiceMade = false; private bool _phonePickedUp;
void Start() private void Start()
{ {
// Ensure AudioSource is available // Ensure AudioSource is available
if (!TryGetComponent(out audioSource)) if (!TryGetComponent(out _audioSource)) _audioSource = gameObject.AddComponent<AudioSource>();
{
audioSource = gameObject.AddComponent<AudioSource>();
}
if (phoneCallAudio != null) if (phoneCallAudio != null) _audioSource.clip = phoneCallAudio;
{
audioSource.clip = phoneCallAudio;
}
choiceUI.SetActive(false); // Hide UI initially choiceUI.SetActive(false); // Hide UI initially
} }
// Public method to be used in XR Grab Interactable's On Select Entered event
public void PickUpPhone()
{
if (!phonePickedUp)
{
phonePickedUp = true;
Debug.Log("Phone Picked Up! Showing UI.");
choiceUI.SetActive(true); // Show UI panel
// Ensure phone attaches properly
if (attachTransform != null)
{
transform.position = attachTransform.position;
transform.rotation = attachTransform.rotation;
}
}
}
private void Update() private void Update()
{ {
if (phonePickedUp && !choiceMade) if (!_phonePickedUp || _choiceMade) return;
{ if (Input.GetKeyDown(KeyCode.A))
if (Input.GetKeyDown(KeyCode.A)) AnswerCall();
{ else if (Input.GetKeyDown(KeyCode.B)) DeclineCall();
AnswerCall();
}
else if (Input.GetKeyDown(KeyCode.B))
{
DeclineCall();
}
}
} }
// mark : this is whether the player chooses between seeking help/not seeking help // Public method to be used in XR Grab interactable OnSelectEntered event
// maybe because they were scared or smtg? public void PickUpPhone()
{
// we can save this to ask them why they chose this and gather info on this bcos if (_phonePickedUp) return;
// that time i pitched this to a teacher they were happy _phonePickedUp = true;
Debug.Log("Phone Picked Up! Showing UI.");
// smtg along the lines of the MSF wanting to know how to improve and get people to reach out??? choiceUI.SetActive(true); // Show the UI panel
// Ensure the phone attaches properly
if (attachTransform == null) return;
transform.position = attachTransform.position;
transform.rotation = attachTransform.rotation;
}
// for mark (backend): this is whether the player chooses between seeking help/not seeking help
// maybe because they were scared or something?
// we can save this to ask them why they chose this and gather info on this because
// that time I pitched this to a teacher they were happy
// something like the MSF wanting to know how to improve and get people to reach out???
private void AnswerCall() private void AnswerCall()
{ {
choiceMade = true; _choiceMade = true;
Debug.Log("Phone Answered! Loading GoodEnding..."); Debug.Log("Phone Answered! Loading GoodEnding...");
choiceUI.SetActive(false); choiceUI.SetActive(false);
SceneManager.LoadScene("GoodEnding"); SceneManager.LoadScene("GoodEnding");
@ -79,10 +67,9 @@ public class PhoneInteraction : MonoBehaviour
private void DeclineCall() private void DeclineCall()
{ {
choiceMade = true; _choiceMade = true;
Debug.Log("Call Declined! Loading BadEnding..."); Debug.Log("Call Declined! Loading BadEnding...");
choiceUI.SetActive(false); choiceUI.SetActive(false);
SceneManager.LoadScene("BadEnding"); SceneManager.LoadScene("BadEnding");
} }
} }