141 lines
3.2 KiB
C#
141 lines
3.2 KiB
C#
using UnityEngine;
|
|
|
|
public class TruthFountainOpenClose : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private GameObject truthFountainRoot;
|
|
[SerializeField] private TruthFountainGameManager gameManager;
|
|
[SerializeField] private Transform targetCamera;
|
|
|
|
[Header("Open Option")]
|
|
[SerializeField] private bool openOnStart = false;
|
|
[SerializeField] private bool startGameOnOpen = true;
|
|
|
|
[Header("VR Placement")]
|
|
[SerializeField] private bool placeInFrontOfCameraOnOpen = true;
|
|
[SerializeField] private bool faceCameraOnOpen = true;
|
|
[Min(0.1f)]
|
|
[SerializeField] private float distanceFromCamera = 2.2f;
|
|
[SerializeField] private float verticalOffset = -0.1f;
|
|
|
|
private void Reset()
|
|
{
|
|
truthFountainRoot = gameObject;
|
|
gameManager = GetComponentInParent<TruthFountainGameManager>();
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (truthFountainRoot == null)
|
|
{
|
|
truthFountainRoot = gameObject;
|
|
}
|
|
|
|
if (gameManager == null)
|
|
{
|
|
gameManager = FindFirstObjectByType<TruthFountainGameManager>();
|
|
}
|
|
|
|
if (targetCamera == null && Camera.main != null)
|
|
{
|
|
targetCamera = Camera.main.transform;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (openOnStart)
|
|
{
|
|
Open();
|
|
}
|
|
else
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
if (placeInFrontOfCameraOnOpen)
|
|
{
|
|
PlaceInFrontOfCamera();
|
|
}
|
|
|
|
if (truthFountainRoot != null)
|
|
{
|
|
truthFountainRoot.SetActive(true);
|
|
}
|
|
|
|
if (startGameOnOpen && gameManager != null)
|
|
{
|
|
gameManager.StartGame();
|
|
}
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
if (truthFountainRoot != null)
|
|
{
|
|
truthFountainRoot.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void Toggle()
|
|
{
|
|
if (truthFountainRoot == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (truthFountainRoot.activeSelf)
|
|
{
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
Open();
|
|
}
|
|
}
|
|
|
|
public void Restart()
|
|
{
|
|
if (truthFountainRoot != null && !truthFountainRoot.activeSelf)
|
|
{
|
|
truthFountainRoot.SetActive(true);
|
|
}
|
|
|
|
gameManager?.StartGame();
|
|
}
|
|
|
|
private void PlaceInFrontOfCamera()
|
|
{
|
|
if (targetCamera == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Transform rootTransform = truthFountainRoot != null ? truthFountainRoot.transform : transform;
|
|
Vector3 forward = targetCamera.forward;
|
|
forward.y = 0f;
|
|
|
|
if (forward.sqrMagnitude < 0.001f)
|
|
{
|
|
forward = targetCamera.forward;
|
|
}
|
|
|
|
forward.Normalize();
|
|
rootTransform.position = targetCamera.position + forward * distanceFromCamera + Vector3.up * verticalOffset;
|
|
|
|
if (faceCameraOnOpen)
|
|
{
|
|
Vector3 directionToCamera = rootTransform.position - targetCamera.position;
|
|
directionToCamera.y = 0f;
|
|
|
|
if (directionToCamera.sqrMagnitude > 0.001f)
|
|
{
|
|
rootTransform.rotation = Quaternion.LookRotation(directionToCamera.normalized, Vector3.up);
|
|
}
|
|
}
|
|
}
|
|
}
|