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.
 
 
 
 

77 lines
1.8 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<bool> m_isInputDown;
[SerializeField, BoxGroup("Settings")]
private float m_darknessSpeed = 0.25f;
[SerializeField, BoxGroup("Settings")]
private float m_lightSpeed = 1.0f;
[ShowNonSerializedField]
private float m_currentDarknessLevel;
private bool m_isDarknessChanging;
private void OnEnable()
{
m_isInputDown.OnValueChanged += OnInputChange;
}
private void OnDisable()
{
m_isInputDown.OnValueChanged -= OnInputChange;
}
private void OnInputChange(bool value)
{
StopAllCoroutines();
if (value)
StartCoroutine(ChangeDarkness(0, m_darknessSpeed));
else
StartCoroutine(ChangeDarkness(1, m_lightSpeed));
}
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_currentDarknessLevel;
float elapsedTime = 0;
time = Mathf.InverseLerp(start, end, m_currentDarknessLevel);
m_isDarknessChanging = true;
while(elapsedTime < time)
{
m_currentDarknessLevel = Mathf.Lerp(start, end, curve.Evaluate(elapsedTime / time));
m_darknessMat.SetFloat("_Apply", m_currentDarknessLevel);
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
}
m_isDarknessChanging = false;
m_currentDarknessLevel = end;
}
}