2021-03-03 12:53:52 +00:00
using System ;
2021-02-15 09:44:04 +00:00
using System.Collections ;
using System.Collections.Generic ;
2021-03-03 12:53:52 +00:00
using Ktyl.Util ;
2021-02-15 09:44:04 +00:00
using UnityEngine ;
2021-02-15 16:23:29 +00:00
using UnityEngine.InputSystem ;
2021-02-15 09:44:04 +00:00
public class DeathZone : MonoBehaviour
2021-02-15 13:14:42 +00:00
{
private GameObject player ;
2021-03-03 12:53:52 +00:00
[SerializeField] private SerialVector3 _respawnPosition ;
[SerializeField] private GameEvent _playerDeath ;
[SerializeField] private GameEvent _playerRespawn ;
2021-02-15 09:44:04 +00:00
private void OnTriggerEnter ( Collider other )
{
2021-02-15 16:23:29 +00:00
// Checks to make sure other collider is the Player. Sets player variable as Player game object and starts the coroutine.
2021-02-26 14:50:30 +00:00
if ( other . gameObject . CompareTag ( "Player" ) )
2021-02-15 09:44:04 +00:00
{
2021-02-15 13:14:42 +00:00
player = other . gameObject ;
2021-02-15 16:23:29 +00:00
StartCoroutine ( RespawnPlayer ( ) ) ;
2021-02-15 09:44:04 +00:00
}
}
2021-02-15 16:23:29 +00:00
// Corountine to trigger death animation, disable player movement, play fade, respawn player at last safe position and then play fade in animation and re-enable player movement.
2021-02-28 00:54:20 +00:00
public IEnumerator RespawnPlayer ( )
2021-02-15 09:44:04 +00:00
{
2021-03-03 12:53:52 +00:00
_playerDeath . Raise ( ) ;
2021-03-04 16:55:29 +00:00
// yield return new WaitForSeconds(0.5f);
2021-02-15 09:44:04 +00:00
2021-03-04 16:55:29 +00:00
// fadeScreen.GetComponent<Animator>().SetTrigger("fadeToBlack");
2021-02-26 17:08:41 +00:00
yield return new WaitForSeconds ( 1.5f ) ;
2021-03-03 12:53:52 +00:00
// move player to respawn position
player . transform . position = _respawnPosition ;
2021-03-04 16:55:29 +00:00
// fadeScreen.GetComponent<Animator>().SetTrigger("fadeToClear");
2021-02-15 16:23:29 +00:00
yield return new WaitForSeconds ( 0.5f ) ;
2021-02-15 09:44:04 +00:00
2021-03-03 12:53:52 +00:00
_playerRespawn . Raise ( ) ;
2021-02-15 09:44:04 +00:00
yield return null ;
}
}