Files
WhaleAdventure_VR/Assets/02_Scripts/Cave/RaftRestartManager.cs

97 lines
2.3 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RaftRestartManager : MonoBehaviour
{
[Header("References")]
[SerializeField] private RaftHealth raftHealth;
[Header("Restart")]
[SerializeField] private bool restartAutomatically = false;
[SerializeField] private float autoRestartDelay = 2.0f;
[Tooltip("키보드 테스트용입니다. VR 버튼 리스타트는 나중에 연결해도 됩니다.")]
[SerializeField] private KeyCode restartKey = KeyCode.R;
[Header("Debug")]
[SerializeField] private bool showDebugLog = true;
private bool waitingForRestart;
private Coroutine restartRoutine;
private void Awake()
{
if (raftHealth == null)
raftHealth = FindFirstObjectByType<RaftHealth>();
}
private void OnEnable()
{
if (raftHealth != null)
{
raftHealth.onDead.AddListener(OnRaftDead);
}
}
private void OnDisable()
{
if (raftHealth != null)
{
raftHealth.onDead.RemoveListener(OnRaftDead);
}
}
private void Update()
{
if (!waitingForRestart)
return;
#if ENABLE_LEGACY_INPUT_MANAGER
if (Input.GetKeyDown(restartKey))
{
RestartNow();
}
#endif
}
public void OnRaftDead()
{
if (waitingForRestart)
return;
waitingForRestart = true;
if (showDebugLog)
{
Debug.Log("[RaftRestartManager] 체력 0. 리스타트 대기 상태입니다. R 키 또는 RestartNow 호출로 재시작합니다.");
}
if (restartAutomatically)
{
if (restartRoutine != null)
StopCoroutine(restartRoutine);
restartRoutine = StartCoroutine(AutoRestartRoutine());
}
}
private IEnumerator AutoRestartRoutine()
{
yield return new WaitForSeconds(autoRestartDelay);
RestartNow();
}
public void RestartNow()
{
if (showDebugLog)
{
Debug.Log("[RaftRestartManager] 현재 씬을 다시 시작합니다.");
}
Time.timeScale = 1f;
Scene currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.buildIndex);
}
}