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

73 lines
1.7 KiB
C#
Raw Permalink Normal View History

2021-05-16 21:00:39 +02:00
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()
{
if (_triggered) return;
2021-05-16 21:00:39 +02:00
for (int i = 0; i < _plates.Length; i++)
{
_plates[i].gameObject.SetActive(false);
}
_onTrigger.Invoke();
_triggered = true;
2021-05-16 21:00:39 +02:00
}
public void Reset()
{
_triggered = false;
for (int i = 0; i < _plates.Length; i++)
{
_plates[i].gameObject.SetActive(true);
_plateStates[i] = false;
}
}
}