revival/game/Assets/Scripts/PressurePlate/PressurePlate.cs

61 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Ktyl.Util;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(BoxCollider))]
public class PressurePlate : MonoBehaviour
{
[SerializeField] private float _triggerDelay = 2.0f;
[SerializeField] private float _resetDelay = -1f;
[SerializeField] private UnityEvent _onPress;
[SerializeField] private UnityEvent _onReset;
[SerializeField] private SerialFloat _objectTimeScale;
[SerializeField] private GameObject _graphics;
private bool opened;
private float? triggered = null;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
triggered = Time.time;
_onPress.Invoke();
_graphics.SetActive(false);
}
}
private void OnTriggerExit(Collider other)
{
// ignore
if (_resetDelay < 0) return;
if(other.CompareTag("Player") && triggered.HasValue)
{
StartCoroutine(Reset());
}
}
private IEnumerator Reset()
{
var elapsed = 0f;
while (elapsed < _resetDelay)
{
elapsed += Time.time * _objectTimeScale;
yield return null;
}
ResetImmediate();
}
public void ResetImmediate()
{
triggered = null;
_graphics.SetActive(true);
_onReset.Invoke();
}
}