quantum-space-buddies/QSB/Animation/Player/Thrusters/RemoteThrusterFlameController.cs

92 lines
2.2 KiB
C#
Raw Normal View History

2021-05-07 19:17:09 +01:00
using QSB.Player;
using QSB.WorldSync;
2021-05-07 19:17:09 +01:00
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.Animation.Player.Thrusters;
internal class RemoteThrusterFlameController : MonoBehaviour
2021-05-07 19:17:09 +01:00
{
2022-03-02 19:46:33 -08:00
[SerializeField]
private Thruster _thruster;
[SerializeField]
private Light _light;
2022-02-13 21:01:54 +00:00
2022-03-02 19:46:33 -08:00
[SerializeField]
private AnimationCurve _scaleByThrust = AnimationCurve.Linear(0f, 0f, 1f, 1f);
2022-02-13 21:01:54 +00:00
2022-03-02 19:46:33 -08:00
[SerializeField]
private DampedSpring _scaleSpring = new();
2022-02-13 21:01:54 +00:00
2022-03-02 19:46:33 -08:00
[SerializeField]
private float _belowMaxThrustScalar = 1f;
2022-02-13 21:01:54 +00:00
2022-03-02 19:46:33 -08:00
private MeshRenderer _thrusterRenderer;
private Vector3 _thrusterFilter;
private float _baseLightRadius;
private float _currentScale;
2022-02-13 21:01:54 +00:00
2022-03-02 19:46:33 -08:00
private bool _initialized;
private PlayerInfo _attachedPlayer;
2021-05-07 19:17:09 +01:00
2022-03-02 19:46:33 -08:00
// TODO : Make flames not appear underwater (Check original code!)
2021-05-07 19:17:09 +01:00
2022-03-02 19:46:33 -08:00
public void Init(PlayerInfo player)
{
_attachedPlayer = player;
_thrusterRenderer = GetComponent<MeshRenderer>();
_thrusterFilter = OWUtilities.GetShipThrusterFilter(_thruster);
_baseLightRadius = _light.range;
_currentScale = 0f;
_thrusterRenderer.enabled = false;
_light.enabled = false;
_light.shadows = LightShadows.Soft;
_initialized = true;
}
2021-05-07 19:17:09 +01:00
2022-03-02 19:46:33 -08:00
private void Update()
{
if (!_initialized)
2021-05-07 19:17:09 +01:00
{
2022-03-02 19:46:33 -08:00
return;
}
2021-05-07 19:17:09 +01:00
2022-03-02 19:46:33 -08:00
var num = _scaleByThrust.Evaluate(GetThrustFraction());
if (_belowMaxThrustScalar < 1f)
{
num *= _belowMaxThrustScalar;
}
_currentScale = _scaleSpring.Update(_currentScale, num, Time.deltaTime);
if (_currentScale < 0f)
{
2021-05-07 19:17:09 +01:00
_currentScale = 0f;
2022-03-02 19:46:33 -08:00
_scaleSpring.ResetVelocity();
2021-05-07 19:17:09 +01:00
}
2022-03-02 19:46:33 -08:00
if (_currentScale <= 0.001f)
2021-05-07 19:17:09 +01:00
{
2022-03-02 19:46:33 -08:00
_currentScale = 0f;
_scaleSpring.ResetVelocity();
2021-05-07 19:17:09 +01:00
}
2022-03-02 19:46:33 -08:00
transform.localScale = Vector3.one * _currentScale;
_light.range = _baseLightRadius * _currentScale;
_thrusterRenderer.enabled = _currentScale > 0f;
_light.enabled = _currentScale > 0f;
}
private float GetThrustFraction() => Vector3.Dot(_attachedPlayer.JetpackAcceleration.AccelerationVariableSyncer.Value, _thrusterFilter);
2021-10-27 10:07:08 +01:00
2022-03-02 19:46:33 -08:00
private void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines || !QSBWorldSync.AllObjectsReady)
2021-10-27 10:07:08 +01:00
{
2022-03-02 19:46:33 -08:00
return;
}
2022-03-02 19:46:33 -08:00
Popcron.Gizmos.Sphere(_light.transform.position, 0.05f, Color.yellow, 4);
Popcron.Gizmos.Line(_light.transform.position, _light.transform.parent.position, Color.yellow);
2021-05-07 19:17:09 +01:00
}
}