using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// GAME 1303 Tier 4 — "All three levers at once." Watches several PuzzleSwitches and
/// fires when enough of them are on.
///
/// SETUP
/// - Empty GameObject + this script. Drag the switches into the list.
/// - Required: how many must be on (0 = all of them).
/// - Wire OnGroupComplete -> Door.Open.
/// - Reset If Broken: for simultaneous-press puzzles, the group un-completes when a
/// switch turns back off (only meaningful with non-one-shot switches).
/// </summary>
public class SwitchGroup : MonoBehaviour
{
public List<PuzzleSwitch> switches = new List<PuzzleSwitch>();
[Tooltip("How many must be ON. 0 = all of them.")]
public int required = 0;
public bool resetIfBroken = false;
public UnityEvent OnGroupComplete;
public UnityEvent OnGroupBroken;
public UnityEvent<int, int> OnProgress; // (on, needed)
public bool IsComplete { get; private set; }
void Start()
{
foreach (var s in switches)
{
if (s == null) continue;
s.OnActivated.AddListener(Check);
s.OnDeactivated.AddListener(Check);
}
Check();
}
public void Check()
{
int need = required > 0 ? required : switches.Count;
int on = 0;
foreach (var s in switches) if (s != null && s.IsOn) on++;
OnProgress?.Invoke(on, need);
if (!IsComplete && on >= need)
{
IsComplete = true;
OnGroupComplete?.Invoke();
}
else if (IsComplete && resetIfBroken && on < need)
{
IsComplete = false;
OnGroupBroken?.Invoke();
}
}
}