quantum-space-buddies/QSB/Tools/ProbeTool/QSBProbeSpotlight.cs

75 lines
1.6 KiB
C#
Raw Normal View History

2021-07-04 11:32:59 +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;
2022-08-27 11:44:52 -07:00
[UsedInUnityProject]
2023-07-28 19:30:57 +01:00
public class QSBProbeSpotlight : MonoBehaviour
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
public ProbeCamera.ID _id;
public float _fadeInLength = 1f;
public float _intensity;
2021-07-04 00:00:24 +01:00
2022-08-27 11:30:15 -07:00
private QSBSurveyorProbe _probe;
2022-03-02 19:46:33 -08:00
private OWLight2 _light;
private float _timer;
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void Awake()
{
2022-08-27 11:30:15 -07:00
_probe = QSBWorldSync.GetUnityObjects<QSBSurveyorProbe>().First(x => gameObject.transform.IsChildOf(x.transform));
2022-03-02 19:46:33 -08:00
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>();
//_intensity = _light.GetLight().intensity;
_light.GetLight().enabled = false;
enabled = false;
_probe.OnLaunchProbe += OnLaunch;
_probe.OnAnchorProbe += OnAnchorOrRetrieve;
_probe.OnRetrieveProbe += OnAnchorOrRetrieve;
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void OnDestroy()
{
_probe.OnLaunchProbe -= OnLaunch;
_probe.OnAnchorProbe -= OnAnchorOrRetrieve;
_probe.OnRetrieveProbe -= OnAnchorOrRetrieve;
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void Update()
{
_timer += Time.deltaTime;
var num = Mathf.Clamp01(_timer / _fadeInLength);
var intensityScale = (2f - num) * num * _intensity;
_light.SetIntensityScale(intensityScale);
}
2021-07-04 00:00:24 +01:00
2022-03-02 19:46:33 -08:00
private void StartFadeIn()
{
if (!enabled)
2021-07-04 00:00:24 +01:00
{
2022-03-02 19:46:33 -08:00
_light.GetLight().enabled = true;
_light.SetIntensityScale(0f);
_timer = 0f;
enabled = true;
2021-07-04 00:00:24 +01:00
}
2022-03-02 19:46:33 -08:00
}
2022-03-02 19:46:33 -08:00
private void OnLaunch()
{
if (_id == ProbeCamera.ID.Forward)
{
2022-03-02 19:46:33 -08:00
StartFadeIn();
}
}
2022-03-02 19:46:33 -08:00
private void OnAnchorOrRetrieve()
{
_light.GetLight().enabled = false;
enabled = false;
}
}