revival/game/Assets/Scripts/Traps/TimeAndPlate.cs

70 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class TimeAndPlate : MonoBehaviour
{
[SerializeField] private PressurePlate[] _plates;
private bool[] _plateStates;
// trigger on successful activation of all plates
[SerializeField] private UnityEvent _onTrigger;
private bool _triggered;
private void Awake()
{
_plateStates = new bool[_plates.Length];
foreach (var plate in _plates)
{
plate.OnPress.AddListener(() =>
{
_plateStates[Array.IndexOf(_plates, plate)] = true;
});
plate.OnReset.AddListener(() =>
{
_plateStates[Array.IndexOf(_plates, plate)] = false;
});
}
}
private void Update()
{
if (_triggered) return;
// bail if any plates are inactive
for (int i = 0; i < _plateStates.Length; i++)
{
if (!_plateStates[i]) return;
}
// all plates are active
Trigger();
}
public void Trigger()
{
for (int i = 0; i < _plates.Length; i++)
{
_plates[i].gameObject.SetActive(false);
}
_onTrigger.Invoke();
}
public void Reset()
{
_triggered = false;
for (int i = 0; i < _plates.Length; i++)
{
_plates[i].gameObject.SetActive(true);
_plateStates[i] = false;
}
}
}