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

61 lines
1.4 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 UnityEvent _onPress;
2021-05-16 02:22:50 +02:00
[SerializeField] private UnityEvent _onReset;
[SerializeField] private SerialFloat _objectTimeScale;
[SerializeField] private GameObject _graphics;
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 02:22:50 +02:00
elapsed += Time.time * _objectTimeScale;
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();
}
}