254 lines
8.3 KiB
C#
254 lines
8.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Rendering.HighDefinition;
|
||
using UnityEngine.ParticleSystemJobs;
|
||
using System.Linq;
|
||
|
||
[RequireComponent(typeof(AudioSource))]
|
||
public class BreakLight : MonoBehaviour
|
||
{
|
||
[SerializeField] private Renderer[] _LampRend;
|
||
[SerializeField] private Renderer[] _lightBulbRend;
|
||
|
||
[SerializeField] private HDAdditionalLightData[] lightData;
|
||
private Light[] _light;
|
||
private Color _lightColorBroken;
|
||
[SerializeField] private float _LightIntensity;
|
||
[SerializeField] private float _LightIntensityBrokenMaxValue;
|
||
[SerializeField] private Color _emissionColorValue;
|
||
private Vector4 _emissionColor;
|
||
[SerializeField] private ParticleSystem[] _SparksParticles;
|
||
[SerializeField] private AudioClip _LightBreakAudioClip;
|
||
[SerializeField] private bool _shouldDisableAnimatorOnLightBreak = true;
|
||
|
||
private float _LerpTime;
|
||
|
||
private bool LightIsBreaking;
|
||
private bool LightBreakingSoundEffectPlayed;
|
||
|
||
[SerializeField] private LightAnimationUpdate _lightAnimationUpdate;
|
||
|
||
private Animator LightAnimator;
|
||
private AudioSource _lightBulbAudioSource;
|
||
private LightBulbEmissionUpdate lightBulbEmissionUpdate;
|
||
|
||
[SerializeField] private SanityActivation sanityActivation;
|
||
[HideInInspector] public bool lightIsBroken;
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
lightData = transform.GetComponentsInChildren<HDAdditionalLightData>();
|
||
_light = transform.GetComponentsInChildren<Light>();
|
||
_SparksParticles = transform.GetComponentsInChildren<ParticleSystem>();
|
||
_lightBulbAudioSource = GetComponent<AudioSource>();
|
||
|
||
_emissionColor = new Vector4(0f, 0f, 0f, 0f);
|
||
//_lightColorBroken = new Color(156f, 255f, 245f);
|
||
|
||
foreach (Renderer lampRend in _LampRend)
|
||
{
|
||
_emissionColorValue = lampRend.material.GetColor("_EmissionColor");
|
||
}
|
||
|
||
foreach (Renderer lampRend in _LampRend)
|
||
{
|
||
_emissionColorValue = lampRend.material.GetColor("_EmissionColor");
|
||
}
|
||
|
||
#region Extra Components Managment
|
||
if ((gameObject.GetComponent("Animator") as Animator) != null)
|
||
{
|
||
LightAnimator = GetComponent<Animator>();
|
||
}
|
||
else
|
||
{
|
||
LightAnimator = null;
|
||
}
|
||
|
||
if ((gameObject.GetComponent("LightBulbEmissionUpdate") as LightBulbEmissionUpdate) != null)
|
||
{
|
||
lightBulbEmissionUpdate = GetComponent<LightBulbEmissionUpdate>();
|
||
}
|
||
else
|
||
{
|
||
lightBulbEmissionUpdate = null;
|
||
}
|
||
//Sanity Trigger:
|
||
if ((gameObject.GetComponentInChildren<SanityActivation>()) != null)
|
||
{
|
||
sanityActivation = GetComponentInChildren<SanityActivation>();
|
||
}
|
||
else
|
||
{
|
||
sanityActivation = null;
|
||
}
|
||
|
||
if (_lightAnimationUpdate == null)
|
||
{
|
||
if (TryGetComponent<LightAnimationUpdate>(out LightAnimationUpdate lightAnimationUpdate))
|
||
{
|
||
// Το Rigidbody υπάρχει
|
||
_lightAnimationUpdate = lightAnimationUpdate;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|
||
[System.Obsolete]
|
||
public IEnumerator BreakThisLight()
|
||
{
|
||
print("Break Light");
|
||
if (_lightAnimationUpdate != null)
|
||
{
|
||
_lightAnimationUpdate.StopFlickering();
|
||
print("Stop Flickering because light is breaking");
|
||
}
|
||
|
||
if ((gameObject.GetComponent("Animator") as Animator) != null)
|
||
{
|
||
if(_shouldDisableAnimatorOnLightBreak)
|
||
LightAnimator.enabled = false;
|
||
}
|
||
if ((gameObject.GetComponent("LightBulbEmissionUpdate") as LightBulbEmissionUpdate) != null)
|
||
{
|
||
lightBulbEmissionUpdate.enabled = false;
|
||
}
|
||
foreach (HDAdditionalLightData light in lightData)
|
||
{
|
||
_LightIntensity = light.intensity;
|
||
}
|
||
yield return new WaitForSeconds(0.1f);
|
||
|
||
|
||
foreach (HDAdditionalLightData light in lightData)
|
||
{
|
||
_LerpTime = 0;
|
||
float LightIntensity = _LightIntensity;
|
||
float durationToBreakLight = 0.3f;
|
||
float elapsedTime = 0f; // Time elapsed since the start of the lerp
|
||
|
||
while (elapsedTime < durationToBreakLight)
|
||
{
|
||
LightIsBreaking = true;
|
||
// StartCoroutine(UpdateLightBreakIntensity()); // If necessary, start this outside the loop
|
||
|
||
// Smoothly update the light intensity
|
||
_LightIntensity = Mathf.Lerp(LightIntensity, _LightIntensityBrokenMaxValue, elapsedTime / durationToBreakLight);
|
||
light.intensity = _LightIntensity; // Update light intensity
|
||
|
||
// Increment elapsed time
|
||
elapsedTime += Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
|
||
//if (elapsedTime >= durationToBreakLight)
|
||
//{
|
||
//yield return new WaitForSeconds(0.01f);
|
||
foreach (var _light in _light)
|
||
{
|
||
_light.enabled = false;
|
||
}
|
||
light.enabled = false;
|
||
|
||
// Optional: Play sound and update materials, etc.
|
||
if (!LightBreakingSoundEffectPlayed)
|
||
{
|
||
RandomSoundsManager.GetInstance().PlayLightBreakSound(_lightBulbAudioSource);
|
||
LightBreakingSoundEffectPlayed = true;
|
||
}
|
||
foreach (Renderer lampRend in _LampRend)
|
||
{
|
||
lampRend.material.SetVector("_EmissionColor", _emissionColorValue * _emissionColor);
|
||
}
|
||
if (_SparksParticles.Length != 0)
|
||
{
|
||
foreach (ParticleSystem sparksParticles in _SparksParticles)
|
||
{
|
||
sparksParticles.Play();
|
||
}
|
||
}
|
||
EmissionIsDisabled();
|
||
LightIsBreaking = false;
|
||
|
||
if (sanityActivation != null)
|
||
{
|
||
sanityActivation.DisableSanityTrigger();
|
||
}
|
||
lightIsBroken = true;
|
||
//}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
IEnumerator UpdateLightBreakIntensity()
|
||
{
|
||
while (LightIsBreaking)
|
||
{
|
||
foreach (HDAdditionalLightData light in lightData)
|
||
{
|
||
//light.color = _lightColorBroken;
|
||
light.lightDimmer = _LightIntensity;
|
||
print("Updating broken light intensity");
|
||
}
|
||
yield return null;
|
||
}
|
||
}
|
||
|
||
public void EmissionIsEnabled()
|
||
{
|
||
foreach (Renderer lampRend in _LampRend)
|
||
{
|
||
//lampRend.materials[2].EnableKeyword("_EMISSION");
|
||
lampRend.materials[2].SetFloat("_EmissiveExposureWeight", 0);
|
||
}
|
||
foreach (Renderer lightBulbRend in _lightBulbRend)
|
||
{
|
||
lightBulbRend.materials[2].SetFloat("_EmissiveExposureWeight", 0);
|
||
}
|
||
}
|
||
|
||
public void EmissionIsDisabled()
|
||
{
|
||
//foreach (Renderer lampRend in _LampRend)
|
||
//{
|
||
// lampRend.material.SetFloat("_EmissiveExposureWeight", 1);
|
||
// Color color = new Color(0, 0, 0);
|
||
// lampRend.materials[2].SetColor("_EmissiveColor", color);
|
||
//}
|
||
|
||
foreach (Renderer lampRend in _LampRend)
|
||
{
|
||
Material[] materialsArray = lampRend.materials; // Get all materials of the current Renderer
|
||
|
||
foreach (var lampRendMaterial in materialsArray)
|
||
{
|
||
lampRendMaterial.SetFloat("_EmissiveExposureWeight", 1);
|
||
Color color = new Color(0, 0, 0);
|
||
lampRendMaterial.SetColor("_EmissiveColor", color);
|
||
}
|
||
|
||
// Assign the modified materials back to the current Renderer
|
||
lampRend.materials = materialsArray;
|
||
}
|
||
|
||
foreach (Renderer lightBulbRend in _lightBulbRend)
|
||
{
|
||
Material[] materialsArray = lightBulbRend.materials; // Get all materials of the current Renderer
|
||
|
||
foreach (var lightBulbRendMaterial in materialsArray)
|
||
{
|
||
lightBulbRendMaterial.SetFloat("_EmissiveExposureWeight", 1);
|
||
Color color = new Color(0, 0, 0);
|
||
lightBulbRendMaterial.SetColor("_EmissiveColor", color);
|
||
}
|
||
|
||
// Assign the modified materials back to the current Renderer
|
||
lightBulbRend.materials = materialsArray;
|
||
}
|
||
}
|
||
}
|