|
|
- 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;
-
- [SerializeField, BoxGroup("Settings")]
- private AnimationCurve m_lightCurve = AnimationCurve.Linear(0,0,1,1);
-
-
- [ShowNonSerializedField]
- private float m_currentDarknessLevel = 1;
- [ShowNonSerializedField]
- private bool m_isDarknessChanging;
-
-
- private void OnEnable()
- {
- m_isInputDown.OnValueChanged += OnInputChange;
- m_darknessMat.SetFloat("_Apply",1);
- }
-
- private void OnDisable()
- {
- m_isInputDown.OnValueChanged -= OnInputChange;
- m_darknessMat.SetFloat("_Apply", 1);
- }
-
-
- private void OnInputChange(bool value)
- {
- StopAllCoroutines();
- 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_currentDarknessLevel;
- float elapsedTime = 0;
-
- time = Mathf.InverseLerp(end, start, m_currentDarknessLevel) * time;
-
- m_isDarknessChanging = true;
-
- while(elapsedTime < time || time != 0)
- {
- m_currentDarknessLevel = Mathf.Lerp(start, end, curve.Evaluate(elapsedTime / time));
- m_darknessMat.SetFloat("_Apply", m_currentDarknessLevel);
- yield return new WaitForEndOfFrame();
- elapsedTime += Time.deltaTime;
- }
-
- m_currentDarknessLevel = end;
- m_darknessMat.SetFloat("_Apply", m_currentDarknessLevel);
- m_isDarknessChanging = false;
-
- }
-
-
-
- }
|