Files
Stair_Horror/Assets/Scripts/LightSwitch.cs

116 lines
3.3 KiB
C#
Raw Normal View History

2025-12-01 16:30:54 +00:00
using UnityEngine;
using UnityEngine.Events; // Required for the Event System
2025-12-01 16:30:54 +00:00
public class LightSwitch : MonoBehaviour, IInteractable
{
[Header("Settings")]
public Light[] linkedLight;
2025-12-01 16:30:54 +00:00
public bool isLightOn = true;
[Header("Audio")]
2025-12-01 16:30:54 +00:00
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;
2025-12-01 16:30:54 +00:00
void Start()
{
if (audioSource == null) audioSource = GetComponent<AudioSource>();
2025-12-01 16:30:54 +00:00
UpdateLightState();
if(GameManager.Instance != null) GameManager.Instance.RegisterSwitch(this);
2025-12-01 16:30:54 +00:00
}
public void Interact()
{
// 1. Malfunction Check
2025-12-01 16:30:54 +00:00
if (Random.value < malfunctionChance)
{
PlaySound(malfunctionSound);
return;
2025-12-01 16:30:54 +00:00
}
// 2. Toggle Light
2025-12-01 16:30:54 +00:00
isLightOn = !isLightOn;
PlaySound(switchClickSound);
2025-12-01 16:30:54 +00:00
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();
}
}
2025-12-01 16:30:54 +00:00
}
public void ForceLightOn()
{
isLightOn = true;
PlaySound(switchClickSound);
2025-12-01 16:30:54 +00:00
UpdateLightState();
}
void UpdateLightState()
{
foreach(var linkedLight in linkedLight)
if(linkedLight != null) linkedLight.enabled = isLightOn;
2025-12-01 16:30:54 +00:00
}
void PlaySound(AudioClip clip)
2025-12-01 16:30:54 +00:00
{
if (audioSource != null && clip != null) audioSource.PlayOneShot(clip);
2025-12-01 16:30:54 +00:00
}
public string GetDescription()
{
return isLightOn ? "Turn <color=red>OFF</color>" : "Turn <color=green>ON</color>";
}
2025-12-01 16:30:54 +00:00
}