2021-12-14 22:56:54 +00:00
|
|
|
|
using QSB.Player;
|
2022-02-13 12:07:26 +00:00
|
|
|
|
using QSB.Utility;
|
2021-12-14 22:56:54 +00:00
|
|
|
|
using UnityEngine;
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.Tools.FlashlightTool;
|
|
|
|
|
|
|
|
|
|
public class QSBFlashlight : MonoBehaviour, ILightSource
|
2020-07-28 00:13:43 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private OWLight2[] _lights;
|
2022-02-02 11:58:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private OWLight2 _illuminationCheckLight;
|
2022-02-02 11:58:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private Transform _root;
|
2022-02-02 11:58:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private Transform _basePivot;
|
2022-02-02 11:58:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private Transform _wobblePivot;
|
2022-02-02 11:58:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private Vector3 _baseForward;
|
|
|
|
|
private Quaternion _baseRotation;
|
|
|
|
|
private LightSourceVolume _lightSourceVolume;
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public bool FlashlightOn;
|
|
|
|
|
public PlayerInfo Player;
|
2021-01-18 12:33:07 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
_lightSourceVolume = this.GetRequiredComponentInChildren<LightSourceVolume>();
|
|
|
|
|
_lightSourceVolume.LinkLightSource(this);
|
|
|
|
|
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
|
|
|
|
|
if (_basePivot == null)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
DebugLog.DebugWrite($"Error - _basePivot is null!", OWML.Common.MessageType.Error);
|
|
|
|
|
return;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
_baseForward = _basePivot.forward;
|
|
|
|
|
_baseRotation = _basePivot.rotation;
|
|
|
|
|
}
|
2020-07-30 22:17:51 +02:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
foreach (var light in _lights)
|
|
|
|
|
{
|
|
|
|
|
light.GetLight().enabled = false;
|
|
|
|
|
light.GetLight().shadows = LightShadows.Soft;
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
FlashlightOn = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LightSourceType GetLightSourceType()
|
|
|
|
|
=> LightSourceType.FLASHLIGHT;
|
2021-11-08 20:55:53 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public OWLight2[] GetLights()
|
|
|
|
|
=> _lights;
|
2021-11-08 20:55:53 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void UpdateState(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
TurnOn();
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
TurnOff();
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2020-08-09 19:33:36 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private void TurnOn()
|
|
|
|
|
{
|
|
|
|
|
if (FlashlightOn)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
return;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
foreach (var light in _lights)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
light.SetActivation(true);
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
FlashlightOn = true;
|
|
|
|
|
Player.AudioController.PlayTurnOnFlashlight();
|
|
|
|
|
var rotation = _root.rotation;
|
|
|
|
|
_basePivot.rotation = rotation;
|
|
|
|
|
_baseRotation = rotation;
|
|
|
|
|
_baseForward = _basePivot.forward;
|
|
|
|
|
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TurnOff()
|
|
|
|
|
{
|
|
|
|
|
if (!FlashlightOn)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-31 14:21:54 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
foreach (var light in _lights)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
light.SetActivation(false);
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
|
|
FlashlightOn = false;
|
|
|
|
|
Player.AudioController.PlayTurnOffFlashlight();
|
|
|
|
|
_lightSourceVolume.SetVolumeActivation(FlashlightOn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckIlluminationAtPoint(Vector3 worldPoint, float buffer = 0f, float maxDistance = float.PositiveInfinity)
|
|
|
|
|
=> FlashlightOn
|
|
|
|
|
&& _illuminationCheckLight.CheckIlluminationAtPoint(worldPoint, buffer, maxDistance);
|
|
|
|
|
|
|
|
|
|
public void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
// This really isn't needed... but it makes it look that extra bit nicer. ^_^
|
|
|
|
|
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-02 21:23:01 +00:00
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|