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

65 lines
1.5 KiB
C#
Raw Normal View History

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