Started building house and refined the interact system

This commit is contained in:
2025-12-02 17:13:58 +00:00
parent b71944ef87
commit 732a2577c2
7783 changed files with 68228 additions and 13482 deletions

View File

@@ -3,66 +3,80 @@ using UnityEngine;
public class LightSwitch : MonoBehaviour, IInteractable
{
[Header("Settings")]
public Light linkedLight; // Drag the actual light source here
public Light linkedLight;
public bool isLightOn = true;
[Header("Horror Mechanics")]
[Range(0f, 1f)] public float malfunctionChance = 0.1f; // 10% chance to fail
[Range(0f, 1f)] public float malfunctionChance = 0.1f;
[Header("Audio")]
public AudioSource audioSource;
public AudioClip switchClickSound;
public AudioClip malfunctionSound;
void Start()
{
// Safety Check: If you forgot to assign the AudioSource, try to find one.
if (audioSource == null) audioSource = GetComponent<AudioSource>();
UpdateLightState();
// Register this switch with the Game Manager
if (GameManager.Instance != null) GameManager.Instance.RegisterSwitch(this);
// Safety Check: Only register if the Manager actually exists
if(GameManager.Instance != null)
{
GameManager.Instance.RegisterSwitch(this);
}
}
public void Interact()
{
// Random chance for the switch to get "stuck" or fail
// 1. Calculate Malfunction
if (Random.value < malfunctionChance)
{
audioSource.PlayOneShot(malfunctionSound);
return; // Exit without turning off light
PlaySound(malfunctionSound);
return;
}
// 2. Toggle Light
isLightOn = !isLightOn;
audioSource.PlayOneShot(switchClickSound);
PlaySound(switchClickSound);
UpdateLightState();
// Notify Manager to check if all lights are off
GameManager.Instance.CheckLights();
// 3. Notify Manager (With Safety Check)
if(GameManager.Instance != null)
{
GameManager.Instance.CheckLights();
}
else
{
Debug.LogWarning("Light switched, but no GameManager found to track it!");
}
}
// Called by the Staircase Trigger to spook the player
public void ForceLightOn()
{
isLightOn = true;
audioSource.PlayOneShot(switchClickSound); // Hearing the click from downstairs is scary
PlaySound(switchClickSound);
UpdateLightState();
}
void UpdateLightState()
{
if (linkedLight != null) linkedLight.enabled = isLightOn;
if(linkedLight != null) linkedLight.enabled = isLightOn;
}
// Optional: Change material of switch to look glowing/dark
// Helper function to safely play sounds
void PlaySound(AudioClip clip)
{
if (audioSource != null && clip != null)
{
audioSource.PlayOneShot(clip);
}
}
public string GetDescription()
{
{
if (isLightOn)
{
return "Press [E] to turn light <color=red>OFF</color>";
}
else
{
return "Press [E] to turn light <color=green>ON</color>";
}
}
if (isLightOn) return "Press [E] to turn light <color=red>OFF</color>";
else return "Press [E] to turn light <color=green>ON</color>";
}
}