2021-04-12 00:17:49 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-05-17 18:29:18 +02:00
|
|
|
using FMODUnity;
|
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;
|
|
|
|
[SerializeField] private SerialFloat _objectTimeScale;
|
|
|
|
[SerializeField] private GameObject _graphics;
|
2021-05-17 18:29:18 +02:00
|
|
|
[SerializeField]
|
|
|
|
[EventRef]
|
|
|
|
private string _sfx;
|
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
|
|
|
|
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();
|
2021-05-17 18:29:18 +02:00
|
|
|
RuntimeManager.PlayOneShot( _sfx, transform.position );
|
2021-05-16 02:22:50 +02:00
|
|
|
_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 21:00:39 +02:00
|
|
|
elapsed += Time.deltaTime * _objectTimeScale;
|
2021-05-16 02:22:50 +02:00
|
|
|
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
|
|
|
}
|