quantum-space-buddies/QSB/Tools/QSBFlashlight.cs

105 lines
2.3 KiB
C#
Raw Normal View History

2020-12-20 10:56:15 +00:00
using OWML.Utils;
2020-07-30 20:17:51 +00:00
using UnityEngine;
namespace QSB.Tools
{
2020-12-02 21:23:01 +00:00
public class QSBFlashlight : MonoBehaviour
{
private OWLight2[] _lights;
private Transform _root;
private Transform _basePivot;
private Transform _wobblePivot;
private Vector3 _baseForward;
private Quaternion _baseRotation;
2021-01-18 12:33:07 +00:00
public bool FlashlightOn;
2020-12-14 20:41:56 +00:00
public void Start()
2020-12-02 21:23:01 +00:00
{
_baseForward = _basePivot.forward;
_baseRotation = _basePivot.rotation;
}
2020-12-02 21:23:01 +00:00
public void Init(Flashlight oldComponent)
{
_lights = oldComponent.GetValue<OWLight2[]>("_lights");
_root = oldComponent.GetValue<Transform>("_root");
_basePivot = oldComponent.GetValue<Transform>("_basePivot");
_wobblePivot = oldComponent.GetValue<Transform>("_wobblePivot");
2020-12-06 10:10:21 +00:00
Destroy(oldComponent.GetComponent<LightLOD>());
2020-12-05 20:58:05 +00:00
foreach (var light in _lights)
{
light.GetLight().enabled = false;
2020-12-06 10:10:21 +00:00
light.GetLight().shadows = LightShadows.Soft;
2020-12-05 20:58:05 +00:00
}
2021-01-18 12:33:07 +00:00
FlashlightOn = false;
2020-12-02 21:23:01 +00:00
}
2020-07-30 20:17:51 +00:00
2020-12-02 21:23:01 +00:00
public void UpdateState(bool value)
{
if (value)
{
TurnOn();
}
else
{
TurnOff();
}
}
2020-08-09 18:33:36 +00:00
2020-12-02 21:23:01 +00:00
private void TurnOn()
{
2021-01-18 12:33:07 +00:00
if (FlashlightOn)
2020-12-02 21:23:01 +00:00
{
return;
}
foreach (var light in _lights)
{
light.GetLight().enabled = true;
}
2021-01-18 12:33:07 +00:00
FlashlightOn = true;
2020-12-02 21:23:01 +00:00
var rotation = _root.rotation;
_basePivot.rotation = rotation;
_baseRotation = rotation;
_baseForward = _basePivot.forward;
}
2020-12-02 21:23:01 +00:00
private void TurnOff()
{
2021-01-18 12:33:07 +00:00
if (!FlashlightOn)
2020-12-02 21:23:01 +00:00
{
return;
}
foreach (var light in _lights)
{
light.GetLight().enabled = false;
}
2021-01-18 12:33:07 +00:00
FlashlightOn = false;
2020-12-02 21:23:01 +00:00
}
2020-12-14 20:41:56 +00:00
public void FixedUpdate()
2020-12-02 21:23:01 +00:00
{
2020-12-14 20:41:56 +00:00
// This really isn't needed... but it makes it look that extra bit nicer. ^_^
2020-12-02 21:23:01 +00:00
var lhs = Quaternion.FromToRotation(_basePivot.up, _root.up) * Quaternion.FromToRotation(_baseForward, _root.forward);
var b = lhs * _baseRotation;
_baseRotation = Quaternion.Slerp(_baseRotation, b, 6f * Time.deltaTime);
_basePivot.rotation = _baseRotation;
_baseForward = _basePivot.forward;
_wobblePivot.localRotation = OWUtilities.GetWobbleRotation(0.3f, 0.15f) * Quaternion.identity;
}
2021-01-26 14:07:17 +00:00
private void OnRenderObject()
{
if (!QSBCore.HasWokenUp)
{
return;
}
var light = _lights[0].GetLight();
if (light.enabled)
{
Popcron.Gizmos.Cone(light.transform.position, light.transform.rotation, light.range, light.spotAngle, Color.yellow);
}
}
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
}