revival/game/Assets/Scripts/Traps/Killbox.cs

58 lines
1.3 KiB
C#
Raw Normal View History

2021-03-22 17:45:42 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(BoxCollider))]
public class Killbox : MonoBehaviour
{
private BoxCollider _box;
private PlayerDeath _player;
[SerializeField] private UnityEvent _playerEnter;
[SerializeField] private UnityEvent _playerExit;
private void OnEnable()
{
_box = GetComponent<BoxCollider>();
_box.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out PlayerDeath pd) && !_player)
{
_player = pd;
_playerEnter.Invoke();
}
}
private void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out PlayerDeath pd) && _player)
{
if (pd == _player)
{
_player = null;
_playerExit.Invoke();
}
else
{
Debug.LogError($":thonk:", this);
}
}
}
public void KillPlayer()
{
if (!_player)
{
Debug.LogError("killbox tried to contain out of bounds player", this);
return;
}
_player.Respawn();
}
}