using UnityEngine; using UnityEngine.Events; // Required for the Event System public class LightSwitch : MonoBehaviour, IInteractable { [Header("Settings")] public Light[] linkedLight; public bool isLightOn = true; [Header("Audio")] public AudioSource audioSource; public AudioClip switchClickSound; public AudioClip malfunctionSound; [Header("Horror Mechanics")] [Range(0f, 1f)] public float malfunctionChance = 0.1f; // --- NEW EVENT SYSTEM --- [Header("Event System")] [Tooltip("If true, the Special Events below will fire on the first interaction.")] public bool enableSpecialEvents = false; [Tooltip("What happens the VERY FIRST time you turn the light OFF? (e.g. Phone Rings)")] public UnityEvent OnFirstSwitchOff; [Tooltip("What happens the VERY FIRST time you turn the light ON? (e.g. Phone Stops)")] public UnityEvent OnFirstSwitchOn; [Space(10)] [Range(0f, 100f)] public float subsequentEventChance = 2f; // 2% chance [Tooltip("What happens randomly after the first time? (e.g. Scary noise)")] public UnityEvent OnRandomEvent; // Track how many times we've flipped the switch private int flipCount = 0; private bool firstOffTriggered = false; private bool firstOnTriggered = false; void Start() { if (audioSource == null) audioSource = GetComponent(); UpdateLightState(); if(GameManager.Instance != null) GameManager.Instance.RegisterSwitch(this); } public void Interact() { // 1. Malfunction Check if (Random.value < malfunctionChance) { PlaySound(malfunctionSound); return; } // 2. Toggle Light isLightOn = !isLightOn; PlaySound(switchClickSound); UpdateLightState(); if(GameManager.Instance != null) GameManager.Instance.CheckLights(); // 3. HANDLE EVENTS HandleEvents(); } void HandleEvents() { if (!enableSpecialEvents) return; // Logic: specific events for first time, random for later if (!isLightOn && !firstOffTriggered) { // First time turning OFF OnFirstSwitchOff.Invoke(); firstOffTriggered = true; } else if (isLightOn && firstOffTriggered && !firstOnTriggered) { // First time turning back ON (after having turned it off) OnFirstSwitchOn.Invoke(); firstOnTriggered = true; } else if (firstOffTriggered && firstOnTriggered) { // We have done the scripted part, now roll dice for random scares float roll = Random.Range(0f, 100f); if (roll < subsequentEventChance) { OnRandomEvent.Invoke(); } } } public void ForceLightOn() { isLightOn = true; PlaySound(switchClickSound); UpdateLightState(); } void UpdateLightState() { foreach(var linkedLight in linkedLight) if(linkedLight != null) linkedLight.enabled = isLightOn; } void PlaySound(AudioClip clip) { if (audioSource != null && clip != null) audioSource.PlayOneShot(clip); } public string GetDescription() { return isLightOn ? "Turn OFF" : "Turn ON"; } }