114 lines
2.6 KiB
C#
Raw Normal View History

2021-07-04 00:00:24 +01:00
using QSB.Utility;
using QSB.WorldSync;
2021-07-04 16:25:39 +01:00
using System.Linq;
2021-07-04 00:00:24 +01:00
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.Tools.ProbeTool;
internal class QSBProbeLantern : MonoBehaviour
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
public float _fadeInDuration;
public AnimationCurve _fadeInCurve;
public AnimationCurve _fadeOutCurve;
public OWEmissiveRenderer _emissiveRenderer;
public float _originalRange;
private QSBProbe _probe;
private OWLight2 _light;
private float _fadeFraction;
private float _targetFade;
private float _startFade;
private float _startFadeTime;
private float _fadeDuration;
private void Awake()
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
_probe = QSBWorldSync.GetUnityObjects<QSBProbe>().First(x => gameObject.transform.IsChildOf(x.transform));
if (_probe == null)
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Error - Couldn't find QSBProbe!", OWML.Common.MessageType.Error);
2021-07-04 00:00:24 +01:00
}
2022-03-02 19:46:33 -08:00
_light = GetComponent<OWLight2>();
_probe.OnAnchorProbe += OnProbeAnchorToSurface;
_probe.OnStartRetrieveProbe += OnStartRetrieveProbe;
_probe.OnRetrieveProbe += OnFinishRetrieveProbe;
}
2022-03-02 19:46:33 -08:00
private void Start()
{
if (_emissiveRenderer != null)
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
_emissiveRenderer.SetEmissiveScale(0f);
2021-07-04 00:00:24 +01:00
}
2022-03-02 19:46:33 -08:00
_light.GetLight().enabled = false;
//_originalRange = _light.range;
enabled = false;
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void OnDestroy()
{
_probe.OnAnchorProbe -= OnProbeAnchorToSurface;
_probe.OnStartRetrieveProbe -= OnStartRetrieveProbe;
_probe.OnRetrieveProbe -= OnFinishRetrieveProbe;
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void Update()
{
var animationCurve = (_targetFade <= 0f)
? _fadeOutCurve
: _fadeInCurve;
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
var fadeTime = Mathf.InverseLerp(_startFadeTime, _startFadeTime + _fadeDuration, Time.time);
_fadeFraction = Mathf.Lerp(_startFade, _targetFade, animationCurve.Evaluate(fadeTime));
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
var probeRuleSet = _probe.GetRulesetDetector().GetProbeRuleSet();
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
var lanternRange = (!(probeRuleSet != null) || !probeRuleSet.GetOverrideLanternRange())
? _originalRange
: probeRuleSet.GetLanternRange();
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
_light.range = lanternRange * _fadeFraction;
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
if (_emissiveRenderer != null)
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
_emissiveRenderer.SetEmissiveScale(_fadeFraction);
2021-07-04 00:00:24 +01:00
}
2022-03-02 19:46:33 -08:00
if (fadeTime >= 1f)
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
enabled = false;
2021-07-04 00:00:24 +01:00
}
2022-03-02 19:46:33 -08:00
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void FadeTo(float fade, float duration)
{
_startFadeTime = Time.time;
_fadeDuration = duration;
_startFade = _fadeFraction;
_targetFade = fade;
enabled = true;
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void OnProbeAnchorToSurface()
{
if (!_probe.IsRetrieving())
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
_light.GetLight().enabled = true;
2021-07-04 00:00:24 +01:00
_light.range = 0f;
2022-03-02 19:46:33 -08:00
FadeTo(1f, _fadeInDuration);
2021-07-04 00:00:24 +01:00
}
}
2022-03-02 19:46:33 -08:00
private void OnStartRetrieveProbe(float retrieveLength)
=> FadeTo(0f, retrieveLength);
private void OnFinishRetrieveProbe()
{
_light.GetLight().enabled = false;
_light.range = 0f;
_fadeFraction = 0f;
enabled = false;
}
}