398 lines
15 KiB
C#
398 lines
15 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
namespace InfallibleCode
|
||
{
|
||
[System.Serializable]
|
||
public class LightSwitchEvents
|
||
{
|
||
public UnityEvent OnLightSwitchTurnedOn;
|
||
public UnityEvent OnLightSwitchTurnedOff;
|
||
}
|
||
|
||
public class NewLightSwitch : MonoBehaviour, IInteractable
|
||
{
|
||
public bool lightIsOn; // You can change from the inspector if you want the light to be on or off as the default.
|
||
|
||
[SerializeField] private GameObject _lightΒulbs;
|
||
private Light[] _light;
|
||
public LightInteractableData[] lightInteractableData;
|
||
//public Renderer[] _lightRend;
|
||
//public List<Renderer> lightRendWithEmission;
|
||
private SanityActivation[] sanity;
|
||
|
||
Animator _lightSwitchAnimator;
|
||
AudioSource _lightSwitchAudioSource;
|
||
[SerializeField] private AudioClip lightSwitchAudioClip;
|
||
|
||
[HideInInspector] public bool canTurnOnLight = true;
|
||
|
||
public List<Color> emissiveLightColor = new List<Color>();
|
||
public List<float> emissiveLightIntensity = new List<float>();
|
||
|
||
private LightAnimationUpdate lightAnimationUpdateInLightSwitch;
|
||
|
||
public LightSwitchEvents lightSwitchEvents;
|
||
|
||
public delegate void OnActionHappened();
|
||
public event OnActionHappened onLightSwitchTurnedOn;
|
||
public event OnActionHappened onLightSwitchTurnedOff;
|
||
|
||
void Awake()
|
||
{
|
||
_light = _lightΒulbs.GetComponentsInChildren<Light>();
|
||
|
||
//_lightRend = _lightΒulbs.GetComponentsInChildren<Renderer>();
|
||
|
||
lightInteractableData = _lightΒulbs.GetComponentsInChildren<LightInteractableData>();
|
||
|
||
sanity = _lightΒulbs.GetComponentsInChildren<SanityActivation>();
|
||
|
||
_lightSwitchAnimator = GetComponent<Animator>();
|
||
|
||
_lightSwitchAudioSource = GetComponent<AudioSource>();
|
||
|
||
lightAnimationUpdateInLightSwitch = _lightΒulbs.GetComponentInChildren<LightAnimationUpdate>();
|
||
|
||
if (lightAnimationUpdateInLightSwitch != null)
|
||
{
|
||
// Component found, you can now use it
|
||
}
|
||
else
|
||
{
|
||
// Component not found
|
||
Debug.LogWarning("Component not found in children.");
|
||
}
|
||
|
||
//foreach (LightInteractableData _lightInteractableData in lightInteractableData)
|
||
//{
|
||
|
||
// foreach (Material material in _lightInteractableData.lightMaterials)
|
||
// {
|
||
// // Get the current emissive color of the material
|
||
// Color currentEmissiveColor = material.GetColor("_EmissiveColor");
|
||
|
||
// // Check if the emissive color is larger than (0, 0, 0, 255)
|
||
// if (currentEmissiveColor.r > 0 && currentEmissiveColor.g > 0 && currentEmissiveColor.b > 0 && currentEmissiveColor.a > 0)
|
||
// {
|
||
// // Do something only if the emissive color is larger than (0, 0, 0, 255)
|
||
// emissiveLightColor.Add(material.GetColor("_EmissiveColor"));
|
||
|
||
// Debug.Log("Emissive color is larger than (0, 0, 0, 255) for material: " + material.name);
|
||
// }
|
||
// }
|
||
|
||
//}
|
||
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
//Sets the default of the lights
|
||
if (lightIsOn)
|
||
{
|
||
_lightSwitchAnimator.Play("LightSwitchOn");
|
||
foreach (Light light in _light)
|
||
{
|
||
light.enabled = true;
|
||
}
|
||
//foreach (Renderer renderer in lightRendWithEmission)
|
||
//{
|
||
// foreach (Material material in renderer.materials)
|
||
// {
|
||
// // Set the emissive color
|
||
// foreach (var _emissiveLightColor in emissiveLightColor)
|
||
// {
|
||
// material.SetColor("_EmissiveColor", _emissiveLightColor);
|
||
// material.SetFloat("_EmissiveExposureWeight", 1f);
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
foreach (LightInteractableData _lightInteractableData in lightInteractableData)
|
||
{
|
||
foreach (Renderer lightRenderer in _lightInteractableData.lightRenderers)
|
||
{
|
||
print("Renderer");
|
||
//for (int i = 0; i < _lightInteractableData.isEmissiveMaterial.Count; i++)
|
||
//{
|
||
// if (_lightInteractableData.isEmissiveMaterial[i] == true)
|
||
// {
|
||
// for (int j = 0; j < lightRenderer.materials.Length; j++)
|
||
// {
|
||
// lightRenderer.materials[j].SetColor("_EmissiveColor", _lightInteractableData.emissionColors[i]);
|
||
// lightRenderer.materials[j].SetFloat("_EmissiveExposureWeight", 1f);
|
||
// }
|
||
|
||
// }
|
||
//}
|
||
|
||
//foreach (var material in lightRenderer.materials)
|
||
//{
|
||
print("material BITCH");
|
||
for (int i = 0; i < lightRenderer.materials.Length; i++)
|
||
{
|
||
lightRenderer.materials[i].SetColor("_EmissiveColor", _lightInteractableData.emissionColors[i]);
|
||
lightRenderer.materials[i].SetFloat("_EmissiveExposureWeight", _lightInteractableData.emissiveExposureWeight[i]);
|
||
print("emissiveExposureWeight: " + _lightInteractableData.emissiveExposureWeight[i]);
|
||
print("emissiveExposureWeight BITCH");
|
||
}
|
||
|
||
//}
|
||
|
||
}
|
||
|
||
}
|
||
foreach (SanityActivation sanity in sanity)
|
||
{
|
||
if (lightAnimationUpdateInLightSwitch == null)
|
||
{
|
||
sanity.EnableSanityTrigger();
|
||
}
|
||
else
|
||
{
|
||
if (!lightAnimationUpdateInLightSwitch.isFlickering)
|
||
{
|
||
sanity.EnableSanityTrigger();
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
else if (!lightIsOn)
|
||
{
|
||
////_light.enabled = false;
|
||
//_lightSwitchAnimator.Play("LightSwitchOff");
|
||
//foreach (Light light in _light)
|
||
//{
|
||
// light.enabled = false;
|
||
//}
|
||
//foreach (Renderer renderer in lightRendWithEmission)
|
||
//{
|
||
// //renderer.material.DisableKeyword("_EMISSION");
|
||
|
||
// //for (int i = 0; i < _lightRend.Length; i++)
|
||
// //{
|
||
// // _lightRend[i].material.SetFloat("_EmissiveExposureWeight", 0f);
|
||
// //}
|
||
|
||
// foreach (Material material in renderer.materials)
|
||
// {
|
||
// Color lightColor = new Color(0f, 0f, 0f, 255f);
|
||
// material.SetColor("_EmissiveColor", lightColor);
|
||
// }
|
||
//}
|
||
//foreach (SanityActivation sanity in sanity)
|
||
//{
|
||
// sanity.DisableSanityTrigger();
|
||
//}
|
||
}
|
||
}
|
||
|
||
public void LightOn()
|
||
{
|
||
lightIsOn = true;
|
||
_lightSwitchAnimator.Play("LightSwitchOn");
|
||
//_lightSwitchAudioSource.PlayOneShot(lightSwitchAudioClip, 0.5f);
|
||
RandomSoundsManager.GetInstance().PlayLightSwitchSound(_lightSwitchAudioSource); //random sound
|
||
foreach (Light light in _light)
|
||
{
|
||
light.enabled = true;
|
||
}
|
||
//foreach (Renderer renderer in lightRendWithEmission)
|
||
//{
|
||
// foreach (Material material in renderer.materials)
|
||
// {
|
||
// // Set the emissive color
|
||
// foreach (var _emissiveLightColor in emissiveLightColor)
|
||
// {
|
||
// material.SetColor("_EmissiveColor", _emissiveLightColor);
|
||
// material.SetFloat("_EmissiveExposureWeight", 1f);
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
foreach (LightInteractableData _lightInteractableData in lightInteractableData)
|
||
{
|
||
foreach (Renderer lightRenderer in _lightInteractableData.lightRenderers)
|
||
{
|
||
print("Renderer");
|
||
//for (int i = 0; i < _lightInteractableData.isEmissiveMaterial.Count; i++)
|
||
//{
|
||
// if (_lightInteractableData.isEmissiveMaterial[i] == true)
|
||
// {
|
||
// for (int j = 0; j < lightRenderer.materials.Length; j++)
|
||
// {
|
||
// lightRenderer.materials[j].SetColor("_EmissiveColor", _lightInteractableData.emissionColors[i]);
|
||
// lightRenderer.materials[j].SetFloat("_EmissiveExposureWeight", 1f);
|
||
// }
|
||
|
||
// }
|
||
//}
|
||
|
||
//foreach (var material in lightRenderer.materials)
|
||
//{
|
||
print("material BITCH");
|
||
for (int i = 0; i < lightRenderer.materials.Length; i++)
|
||
{
|
||
lightRenderer.materials[i].SetColor("_EmissiveColor", _lightInteractableData.emissionColors[i]);
|
||
lightRenderer.materials[i].SetFloat("_EmissiveExposureWeight", _lightInteractableData.emissiveExposureWeight[i]);
|
||
print("emissiveExposureWeight: " + _lightInteractableData.emissiveExposureWeight[i]);
|
||
print("emissiveExposureWeight BITCH");
|
||
}
|
||
|
||
//}
|
||
|
||
}
|
||
|
||
}
|
||
foreach (SanityActivation sanity in sanity)
|
||
{
|
||
if (lightAnimationUpdateInLightSwitch == null)
|
||
{
|
||
sanity.EnableSanityTrigger();
|
||
}
|
||
else
|
||
{
|
||
if (!lightAnimationUpdateInLightSwitch.isFlickering)
|
||
{
|
||
sanity.EnableSanityTrigger();
|
||
}
|
||
}
|
||
}
|
||
print("Light is on");
|
||
|
||
onLightSwitchTurnedOn?.Invoke();
|
||
|
||
if(lightSwitchEvents.OnLightSwitchTurnedOn != null)
|
||
{
|
||
lightSwitchEvents.OnLightSwitchTurnedOn?.Invoke();
|
||
}
|
||
}
|
||
|
||
public void LightOff()
|
||
{
|
||
lightIsOn = false;
|
||
_lightSwitchAnimator.Play("LightSwitchOff");
|
||
//_lightSwitchAudioSource.PlayOneShot(lightSwitchAudioClip, 0.5f);
|
||
RandomSoundsManager.GetInstance().PlayLightSwitchSound(_lightSwitchAudioSource);//random sound
|
||
foreach (Light light in _light)
|
||
{
|
||
light.enabled = false;
|
||
}
|
||
////_light.enabled = false;
|
||
//foreach (Renderer renderer in _lightRend)
|
||
//{
|
||
|
||
// foreach (Material material in renderer.materials)
|
||
// {
|
||
// Color lightColor = new Color(0f, 0f, 0f, 255f);
|
||
// material.SetColor("_EmissiveColor", lightColor);
|
||
// material.SetFloat("_EmissiveExposureWeight", -100f);
|
||
// }
|
||
//}
|
||
|
||
foreach (LightInteractableData _lightInteractableData in lightInteractableData)
|
||
{
|
||
foreach (Renderer lightRenderer in _lightInteractableData.lightRenderers)
|
||
{
|
||
print("material BITCH OFF apla hihi");
|
||
for (int i = 0; i < lightRenderer.materials.Length; i++)
|
||
{
|
||
lightRenderer.materials[i].SetColor("_EmissiveColor", Color.black);
|
||
lightRenderer.materials[i].SetFloat("_EmissiveExposureWeight", -100f);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
foreach (SanityActivation sanity in sanity)
|
||
{
|
||
sanity.DisableSanityTrigger();
|
||
}
|
||
print("Light is off");
|
||
|
||
onLightSwitchTurnedOff?.Invoke();
|
||
|
||
if (lightSwitchEvents.OnLightSwitchTurnedOff != null)
|
||
{
|
||
lightSwitchEvents.OnLightSwitchTurnedOff?.Invoke();
|
||
}
|
||
}
|
||
|
||
public void lightSwitch()
|
||
{
|
||
if (!lightIsOn)
|
||
{
|
||
LightOn();
|
||
}
|
||
else
|
||
{
|
||
LightOff();
|
||
}
|
||
|
||
#region Old logic
|
||
//if (!lightIsOn) //If light is not on & you press the following input:
|
||
//{
|
||
// if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)/*A button*/)
|
||
// {
|
||
// lightIsOn = true;
|
||
// foreach (Light light in _light)
|
||
// {
|
||
// light.enabled = true;
|
||
// }
|
||
// //_light.enabled = true;
|
||
// foreach (Renderer renderer in _lightRend)
|
||
// {
|
||
// renderer.material.EnableKeyword("_EMISSION");
|
||
// }
|
||
// foreach (SanityActivation sanity in sanity)
|
||
// {
|
||
// sanity.EnableSanityTrigger();
|
||
// }
|
||
// print("Light is on");
|
||
// }
|
||
//}
|
||
//else if (lightIsOn && Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)/*A button*/) //if light is on & you press the following input.
|
||
//{
|
||
// lightIsOn = false;
|
||
// foreach (Light light in _light)
|
||
// {
|
||
// light.enabled = false;
|
||
// }
|
||
// //_light.enabled = false;
|
||
// foreach (Renderer renderer in _lightRend)
|
||
// {
|
||
// renderer.material.DisableKeyword("_EMISSION");
|
||
// }
|
||
// foreach (SanityActivation sanity in sanity)
|
||
// {
|
||
// sanity.DisableSanityTrigger();
|
||
// }
|
||
// print("Light is off");
|
||
//}
|
||
#endregion
|
||
}
|
||
|
||
public void Interact()
|
||
{
|
||
if (canTurnOnLight)
|
||
{
|
||
lightSwitch();
|
||
}
|
||
}
|
||
|
||
public void CanTurnOnLight()
|
||
{
|
||
canTurnOnLight = true;
|
||
}
|
||
|
||
public void CannotTurnOnLight()
|
||
{
|
||
canTurnOnLight = false;
|
||
}
|
||
}
|
||
} |