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

65 lines
1.5 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;
2021-03-23 16:03:18 +01:00
using UnityEngine.Serialization;
2021-03-22 17:45:42 +01:00
[RequireComponent(typeof(BoxCollider))]
public class Killbox : MonoBehaviour
{
2021-03-23 16:03:18 +01:00
[SerializeField] private bool _instakill = false;
2021-03-22 17:45:42 +01:00
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;
2021-03-23 16:03:18 +01:00
if (_instakill)
{
KillPlayer();
return;
}
2021-03-22 17:45:42 +01:00
_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()
{
2021-03-23 16:03:18 +01:00
// can't kill out of bounds player
if (!_player) return;
2021-03-22 17:45:42 +01:00
_player.Respawn();
}
}