2021-04-12 00:17:49 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-05-16 02:22:50 +02:00
|
|
|
using Ktyl.Util;
|
2021-04-12 00:17:49 +02:00
|
|
|
using UnityEngine;
|
2021-04-15 16:10:58 +02:00
|
|
|
using UnityEngine.Events;
|
2021-04-12 00:17:49 +02:00
|
|
|
|
2021-05-16 02:22:50 +02:00
|
|
|
[RequireComponent(typeof(BoxCollider))]
|
2021-04-15 16:10:58 +02:00
|
|
|
public class PressurePlate : MonoBehaviour
|
2021-04-12 00:17:49 +02:00
|
|
|
{
|
2021-04-15 16:10:58 +02:00
|
|
|
[SerializeField] private float _triggerDelay = 2.0f;
|
2021-05-16 02:22:50 +02:00
|
|
|
[SerializeField] private float _resetDelay = -1f;
|
2021-04-15 16:10:58 +02:00
|
|
|
[SerializeField] private UnityEvent _onPress;
|
2021-05-16 02:22:50 +02:00
|
|
|
[SerializeField] private UnityEvent _onReset;
|
|
|
|
[SerializeField] private SerialFloat _objectTimeScale;
|
|
|
|
[SerializeField] private GameObject _graphics;
|
|
|
|
|
2021-04-21 17:42:11 +02:00
|
|
|
private bool opened;
|
2021-04-15 16:10:58 +02:00
|
|
|
|
|
|
|
private float? triggered = null;
|
2021-04-12 00:17:49 +02:00
|
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2021-05-16 02:22:50 +02:00
|
|
|
if (other.CompareTag("Player"))
|
2021-04-12 00:17:49 +02:00
|
|
|
{
|
|
|
|
triggered = Time.time;
|
2021-05-16 02:22:50 +02:00
|
|
|
_onPress.Invoke();
|
|
|
|
|
|
|
|
_graphics.SetActive(false);
|
2021-04-12 00:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-12 00:17:49 +02:00
|
|
|
{
|
2021-05-16 02:22:50 +02:00
|
|
|
StartCoroutine(Reset());
|
2021-04-12 00:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 02:22:50 +02:00
|
|
|
private IEnumerator Reset()
|
2021-04-12 00:17:49 +02:00
|
|
|
{
|
2021-05-16 02:22:50 +02:00
|
|
|
var elapsed = 0f;
|
|
|
|
while (elapsed < _resetDelay)
|
2021-04-12 00:17:49 +02:00
|
|
|
{
|
2021-05-16 02:22:50 +02:00
|
|
|
elapsed += Time.time * _objectTimeScale;
|
|
|
|
yield return null;
|
2021-04-12 00:17:49 +02:00
|
|
|
}
|
2021-05-16 02:22:50 +02:00
|
|
|
|
|
|
|
ResetImmediate();
|
2021-04-12 00:17:49 +02:00
|
|
|
}
|
2021-04-21 17:42:11 +02:00
|
|
|
|
2021-05-16 02:22:50 +02:00
|
|
|
public void ResetImmediate()
|
2021-04-21 17:42:11 +02:00
|
|
|
{
|
|
|
|
triggered = null;
|
2021-05-16 02:22:50 +02:00
|
|
|
_graphics.SetActive(true);
|
|
|
|
_onReset.Invoke();
|
2021-04-21 17:42:11 +02:00
|
|
|
}
|
2021-04-12 00:17:49 +02:00
|
|
|
}
|