96 lines
2.1 KiB
C#
96 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class PlayerDie : MonoBehaviour
|
|
{
|
|
public static PlayerDie Instance { get; private set; }
|
|
|
|
[System.Serializable]
|
|
public class References
|
|
{
|
|
public GameObject UI;
|
|
public Camera Camera;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class Settings
|
|
{
|
|
public float DelayBeforeRespawn = 3;
|
|
}
|
|
|
|
[SerializeField] private References _references;
|
|
[SerializeField] private Settings _settings;
|
|
|
|
private bool _respawned;
|
|
public bool Respawned => _respawned;
|
|
|
|
private bool _waitForPlayer;
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
|
|
DontDestroyOnLoad(this);
|
|
|
|
_references.UI.gameObject.SetActive(false);
|
|
_references.Camera.gameObject.SetActive(false);
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
|
|
Invoke("ReloadScene", _settings.DelayBeforeRespawn);
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
Instance = null;
|
|
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (_waitForPlayer)
|
|
{
|
|
if (Player.Instance)
|
|
{
|
|
_waitForPlayer = false;
|
|
ShowUI();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Continue()
|
|
{
|
|
Player.Instance.SetState(Player.PlayerState.Idle);
|
|
Player.Instance.Pause(false);
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public void GetCameraTransformation(out Vector3 position, out Quaternion rotation)
|
|
{
|
|
position = _references.Camera.transform.position;
|
|
rotation = _references.Camera.transform.rotation;
|
|
}
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
_waitForPlayer = true;
|
|
}
|
|
|
|
private void ReloadScene()
|
|
{
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
|
}
|
|
|
|
private void ShowUI()
|
|
{
|
|
Player.Instance.Pause();
|
|
Player.Instance.SetState(Player.PlayerState.Loser);
|
|
transform.position = Player.Instance.transform.position;
|
|
transform.rotation = Player.Instance.transform.rotation;
|
|
_references.UI.gameObject.SetActive(true);
|
|
_respawned = true;
|
|
}
|
|
}
|