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

67 lines
1.5 KiB
C#
Raw Permalink 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
2021-05-17 00:03:42 +02:00
[RequireComponent(typeof(Collider))]
2021-03-22 17:45:42 +01:00
public class Killbox : MonoBehaviour
{
2021-03-23 16:03:18 +01:00
[SerializeField] private bool _instakill = false;
2021-05-17 00:03:42 +02:00
private Collider _box;
2021-03-22 17:45:42 +01:00
private PlayerDeath _player;
[SerializeField] private UnityEvent _playerEnter;
[SerializeField] private UnityEvent _playerExit;
2021-05-17 00:03:42 +02:00
private void Awake()
2021-03-22 17:45:42 +01:00
{
2021-05-17 00:03:42 +02:00
_box = GetComponent<Collider>();
2021-03-22 17:45:42 +01:00
_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-05-17 00:03:42 +02:00
if (!enabled) return;
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();
}
}