GazeProject/Assets/EYE ADVANCED/Scripts/SequenceManagerLightTurnOn.cs
2025-05-15 15:19:18 +02:00

146 lines
4.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SequenceManagerLightTurnOn : MonoBehaviour
{
[SerializeField] private GameObject[] allInteractableObjects; // All 9 objects
[SerializeField] private Light globalLight;
[SerializeField] private Material greenMaterial;
[SerializeField] private Material redMaterial;
// Names of the objects that must be green for victory
[SerializeField] private string[] specialObjectNames = { "EyeInteractableUnlock (1)", "EyeInteractableUnlock (5)", "EyeInteractableUnlock (9)" };
// Tracks color state: true = green, false = red
private Dictionary<GameObject, bool> objectColorStates = new Dictionary<GameObject, bool>();
private void Start()
{
if (allInteractableObjects.Length != 9)
{
Debug.LogError("Please assign exactly 9 interactable objects!");
return;
}
InitializeObjects();
if (globalLight != null)
globalLight.enabled = false;
}
private void InitializeObjects()
{
// Set all to red first
foreach (GameObject obj in allInteractableObjects)
{
objectColorStates[obj] = false;
ApplyColorToObject(obj, false);
EyeUnlockSequence seq = obj.GetComponent<EyeUnlockSequence>();
if (seq != null)
seq.IsGreen = false;
}
// Pick 3 random ones to turn green
List<GameObject> shuffled = new List<GameObject>(allInteractableObjects);
ShuffleList(shuffled);
for (int i = 0; i < 3; i++)
{
GameObject obj = shuffled[i];
objectColorStates[obj] = true;
ApplyColorToObject(obj, true);
EyeUnlockSequence seq = obj.GetComponent<EyeUnlockSequence>();
if (seq != null)
seq.IsGreen = true;
Debug.Log($"Object {obj.name} set to GREEN");
}
}
private void ShuffleList<T>(List<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = Random.Range(0, n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
private void ApplyColorToObject(GameObject obj, bool isGreen)
{
MeshRenderer renderer = obj.GetComponent<MeshRenderer>();
if (renderer != null)
{
renderer.material = isGreen ? greenMaterial : redMaterial;
}
}
// Called from EyeUnlockSequence when a sequence is completed
public void NotifyCompleted(GameObject completedObject)
{
if (objectColorStates.ContainsKey(completedObject))
{
bool newColorState = !objectColorStates[completedObject];
objectColorStates[completedObject] = newColorState;
ApplyColorToObject(completedObject, newColorState);
Debug.Log($"Object {completedObject.name} toggled to {(newColorState ? "GREEN" : "RED")}");
CheckVictoryCondition();
}
}
private void CheckVictoryCondition()
{
bool conditionMet = true;
foreach (GameObject obj in allInteractableObjects)
{
bool isGreen = objectColorStates[obj];
bool shouldBeGreen = System.Array.Exists(specialObjectNames, name => name == obj.name);
if (isGreen != shouldBeGreen)
{
conditionMet = false;
break;
}
}
if (conditionMet)
{
ActivateGlobalLight();
}
else if (globalLight != null && globalLight.enabled)
{
globalLight.enabled = false;
Debug.Log("❌ Victory condition lost. Light deactivated.");
}
}
private void ActivateGlobalLight()
{
if (globalLight != null)
{
globalLight.enabled = true;
Debug.Log("🌟 Victory condition met! Light activated.");
}
}
public void ResetAllObjects()
{
if (globalLight != null)
globalLight.enabled = false;
InitializeObjects();
Debug.Log("🔁 All objects reset.");
}
}