2021-03-03 13:53:52 +01:00
using System ;
2021-02-15 10:44:04 +01:00
using System.Collections ;
using System.Collections.Generic ;
2021-03-03 13:53:52 +01:00
using Ktyl.Util ;
2021-02-15 10:44:04 +01:00
using UnityEngine ;
2021-02-15 17:23:29 +01:00
using UnityEngine.InputSystem ;
2021-02-15 10:44:04 +01:00
public class DeathZone : MonoBehaviour
2021-02-15 14:14:42 +01:00
{
public Animator animator ;
2021-02-15 10:44:04 +01:00
public GameObject fadeScreen ;
2021-02-15 14:14:42 +01:00
private GameObject player ;
2021-03-03 13:53:52 +01:00
[SerializeField] private SerialVector3 _respawnPosition ;
[SerializeField] private GameEvent _playerDeath ;
[SerializeField] private GameEvent _playerRespawn ;
2021-02-15 10:44:04 +01:00
private void OnTriggerEnter ( Collider other )
{
2021-02-15 17:23:29 +01:00
// Checks to make sure other collider is the Player. Sets player variable as Player game object and starts the coroutine.
2021-02-15 14:14:42 +01:00
if ( other . gameObject . name = = "Player" )
2021-02-15 10:44:04 +01:00
{
2021-02-15 14:14:42 +01:00
player = other . gameObject ;
2021-02-15 17:23:29 +01:00
StartCoroutine ( RespawnPlayer ( ) ) ;
2021-02-15 10:44:04 +01:00
}
}
2021-02-15 17:23:29 +01: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.
IEnumerator RespawnPlayer ( )
2021-02-15 10:44:04 +01:00
{
animator . SetTrigger ( "IsDead" ) ;
2021-03-03 13:53:52 +01:00
_playerDeath . Raise ( ) ;
2021-02-15 17:23:29 +01:00
yield return new WaitForSeconds ( 0.5f ) ;
2021-02-15 10:44:04 +01:00
2021-02-15 17:23:29 +01:00
fadeScreen . GetComponent < Animator > ( ) . SetTrigger ( "fadeToBlack" ) ;
yield return new WaitForSeconds ( 1.5f ) ;
2021-03-03 13:53:52 +01:00
// move player to respawn position
player . transform . position = _respawnPosition ;
2021-02-15 17:23:29 +01:00
fadeScreen . GetComponent < Animator > ( ) . SetTrigger ( "fadeToClear" ) ;
yield return new WaitForSeconds ( 0.5f ) ;
2021-02-15 10:44:04 +01:00
2021-03-03 13:53:52 +01:00
_playerRespawn . Raise ( ) ;
2021-02-15 10:44:04 +01:00
yield return null ;
}
}