114 lines
2.8 KiB
C#
Raw Normal View History

2021-11-09 19:39:56 +00:00
using UnityEngine;
namespace QSB.Tools.FlashlightTool
{
2021-11-08 20:55:53 +00:00
public class QSBFlashlight : MonoBehaviour, ILightSource
2020-12-02 21:23:01 +00:00
{
private OWLight2[] _lights;
2021-11-08 20:55:53 +00:00
internal OWLight2 _illuminationCheckLight;
2020-12-02 21:23:01 +00:00
private Transform _root;
private Transform _basePivot;
private Transform _wobblePivot;
private Vector3 _baseForward;
private Quaternion _baseRotation;
2021-11-08 20:55:53 +00:00
private LightSourceVolume _lightSourceVolume;
2021-01-18 12:33:07 +00:00
public bool FlashlightOn;
2020-12-14 21:41:56 +01:00
public void Start()
2020-12-02 21:23:01 +00:00
{
2021-11-08 20:55:53 +00:00
_lightSourceVolume = this.GetRequiredComponentInChildren<LightSourceVolume>();
_lightSourceVolume.LinkLightSource(this);
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
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)
{
2021-11-08 20:55:53 +00:00
_lights = oldComponent._lights;
_illuminationCheckLight = oldComponent._illuminationCheckLight;
_root = oldComponent._root;
_basePivot = oldComponent._basePivot;
_wobblePivot = oldComponent._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-06-18 22:38:32 +01:00
2021-01-18 12:33:07 +00:00
FlashlightOn = false;
2020-12-02 21:23:01 +00:00
}
2020-07-30 22:17:51 +02:00
2021-11-08 20:55:53 +00:00
public LightSourceType GetLightSourceType()
=> LightSourceType.FLASHLIGHT;
public OWLight2[] GetLights()
=> _lights;
2020-12-02 21:23:01 +00:00
public void UpdateState(bool value)
{
if (value)
{
TurnOn();
}
else
{
TurnOff();
}
}
2020-08-09 19:33:36 +01: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;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:23:01 +00:00
foreach (var light in _lights)
{
2021-11-08 20:55:53 +00:00
light.SetActivation(true);
2020-12-02 21:23:01 +00:00
}
2021-06-18 22:38:32 +01:00
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;
2021-11-08 20:55:53 +00:00
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
2020-12-02 21:23:01 +00:00
}
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;
}
2021-06-18 22:38:32 +01:00
2020-12-02 21:23:01 +00:00
foreach (var light in _lights)
{
2021-11-08 20:55:53 +00:00
light.SetActivation(false);
2020-12-02 21:23:01 +00:00
}
2021-06-18 22:38:32 +01:00
2021-01-18 12:33:07 +00:00
FlashlightOn = false;
2021-11-08 20:55:53 +00:00
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
2020-12-02 21:23:01 +00:00
}
2021-11-08 20:55:53 +00:00
public bool CheckIlluminationAtPoint(Vector3 worldPoint, float buffer = 0f, float maxDistance = float.PositiveInfinity)
=> FlashlightOn
2021-11-08 20:55:53 +00:00
&& _illuminationCheckLight.CheckIlluminationAtPoint(worldPoint, buffer, maxDistance);
2020-12-14 21:41:56 +01:00
public void FixedUpdate()
2020-12-02 21:23:01 +00:00
{
2020-12-14 21:41:56 +01: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;
}
}
2020-12-03 08:28:05 +00:00
}