2020-07-30 20:17:51 +00:00
|
|
|
|
using OWML.ModHelper.Events;
|
|
|
|
|
using UnityEngine;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-07-30 20:27:14 +00:00
|
|
|
|
namespace QSB.Tools
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
|
|
|
|
public class QSBFlashlight : MonoBehaviour
|
|
|
|
|
{
|
2020-07-30 20:17:51 +00:00
|
|
|
|
private OWLight2[] _lights;
|
|
|
|
|
private Transform _root;
|
|
|
|
|
private Transform _basePivot;
|
|
|
|
|
private Transform _wobblePivot;
|
2020-07-28 13:59:24 +00:00
|
|
|
|
|
2020-07-27 23:13:43 +00:00
|
|
|
|
private bool _flashlightOn;
|
|
|
|
|
private Vector3 _baseForward;
|
|
|
|
|
private Quaternion _baseRotation;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
_baseForward = _basePivot.forward;
|
|
|
|
|
_baseRotation = _basePivot.rotation;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 20:17:51 +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-08-09 18:33:36 +00:00
|
|
|
|
public void UpdateState(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
TurnOn();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TurnOff();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TurnOn()
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
if (_flashlightOn)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (var light in _lights)
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
light.GetLight().enabled = true;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
}
|
2020-07-28 13:59:24 +00:00
|
|
|
|
_flashlightOn = true;
|
|
|
|
|
var rotation = _root.rotation;
|
|
|
|
|
_basePivot.rotation = rotation;
|
|
|
|
|
_baseRotation = rotation;
|
|
|
|
|
_baseForward = _basePivot.forward;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 18:33:36 +00:00
|
|
|
|
private void TurnOff()
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
if (!_flashlightOn)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (var light in _lights)
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
light.GetLight().enabled = false;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
}
|
2020-07-28 13:59:24 +00:00
|
|
|
|
_flashlightOn = false;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
var lhs = Quaternion.FromToRotation(_basePivot.up, _root.up) * Quaternion.FromToRotation(_baseForward, _root.forward);
|
|
|
|
|
var b = lhs * _baseRotation;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
_baseRotation = Quaternion.Slerp(_baseRotation, b, 6f * Time.deltaTime);
|
|
|
|
|
_basePivot.rotation = _baseRotation;
|
|
|
|
|
_baseForward = _basePivot.forward;
|
|
|
|
|
_wobblePivot.localRotation = OWUtilities.GetWobbleRotation(0.3f, 0.15f) * Quaternion.identity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|