Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
2.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;
using Variables;
public class DarknessController : MonoBehaviour
{
[SerializeField, Header("References")]
private Material m_darknessMat;
[SerializeField]
private Reference<float> m_lightLevel;
[SerializeField]
private Reference<bool> m_isInputDown;
[SerializeField, BoxGroup("Settings")]
private float m_darknessSpeed = 0.25f;
[SerializeField, BoxGroup("Settings")]
private float m_lightSpeed = 1.0f;
[SerializeField, BoxGroup("Settings")]
private AnimationCurve m_lightCurve = AnimationCurve.Linear(0,0,1,1);
[ShowNonSerializedField]
private bool m_isDarknessChanging;
private void OnEnable()
{
m_isInputDown.OnValueChanged += OnInputChange;
m_darknessMat.SetFloat("_Apply", 1);
m_lightLevel.Value = 1;
}
private void OnDisable()
{
m_isInputDown.OnValueChanged -= OnInputChange;
m_darknessMat.SetFloat("_Apply", 1);
m_lightLevel.Value = 1;
}
private void OnInputChange(bool value)
{
StopAllCoroutines();
Debug.Log($"Here {m_isDarknessChanging}");
if (value)
{ if (!m_isDarknessChanging)
{
StartCoroutine(ChangeDarkness(0, m_darknessSpeed));
}
else
{
StartCoroutine(ChangeDarkness(0, 0));
}
}
else
StartCoroutine(ChangeDarkness(1, m_lightSpeed, m_lightCurve));
}
private IEnumerator ChangeDarkness(float end, float time)
{
yield return StartCoroutine(ChangeDarkness(end, time, AnimationCurve.Linear(0, 0, 1, 1)));
}
private IEnumerator ChangeDarkness(float end, float time, AnimationCurve curve)
{
float start = m_lightLevel;
float elapsedTime = 0;
time = Mathf.InverseLerp(end, start, m_lightLevel) * time;
m_isDarknessChanging = true;
while(elapsedTime < time)
{
m_lightLevel.Value = Mathf.Lerp(start, end, curve.Evaluate(elapsedTime / time));
m_darknessMat.SetFloat("_Apply", m_lightLevel);
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
}
m_isDarknessChanging = false;
m_lightLevel.Value = end;
m_darknessMat.SetFloat("_Apply", m_lightLevel);
}
}