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

99 lines
2.6 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;
namespace QSB.Animation.Player.Thrusters
{
internal class RemoteThrusterFlameController : MonoBehaviour
{
private Thruster _thruster;
private Light _light;
private AnimationCurve _scaleByThrust = AnimationCurve.Linear(0f, 0f, 1f, 1f);
2021-11-20 19:49:50 +00:00
private DampedSpring _scaleSpring = new();
2021-05-07 19:17:09 +01:00
private float _belowMaxThrustScalar = 1f;
private MeshRenderer _thrusterRenderer;
private Vector3 _thrusterFilter;
private float _baseLightRadius;
private float _currentScale;
private bool _isReady;
private bool _initialized;
private PlayerInfo _attachedPlayer;
// TODO : Make flames not appear underwater (Check original code!)
2021-05-08 12:53:09 +01:00
public void InitFromOld(Thruster thruster, Light light, AnimationCurve scaleByThrust, DampedSpring scaleSpring, float belowMaxThrustScalar, float baseLightRadius, PlayerInfo player)
2021-05-07 19:17:09 +01:00
{
_thruster = thruster;
_light = light;
_scaleByThrust = scaleByThrust;
_scaleSpring = scaleSpring;
_belowMaxThrustScalar = belowMaxThrustScalar;
_attachedPlayer = player;
2021-05-08 12:53:09 +01:00
_baseLightRadius = baseLightRadius;
2021-05-07 19:17:09 +01:00
_isReady = true;
}
private void Init()
{
_thrusterRenderer = GetComponent<MeshRenderer>();
_thrusterFilter = OWUtilities.GetShipThrusterFilter(_thruster);
_currentScale = 0f;
_thrusterRenderer.enabled = false;
_light.enabled = false;
2021-05-08 09:32:33 +01:00
_light.shadows = LightShadows.Soft;
2021-05-07 19:17:09 +01:00
_initialized = true;
}
private void Update()
{
if (_isReady && !_initialized)
{
Init();
}
if (!_initialized)
{
return;
}
var num = _scaleByThrust.Evaluate(GetThrustFraction());
if (_belowMaxThrustScalar < 1f)
{
num *= _belowMaxThrustScalar;
}
2021-06-18 22:38:32 +01:00
2021-05-07 19:17:09 +01:00
_currentScale = _scaleSpring.Update(_currentScale, num, Time.deltaTime);
if (_currentScale < 0f)
{
_currentScale = 0f;
_scaleSpring.ResetVelocity();
}
2021-06-18 22:38:32 +01:00
2021-05-07 19:23:24 +01:00
if (_currentScale <= 0.001f)
2021-05-07 19:17:09 +01:00
{
_currentScale = 0f;
_scaleSpring.ResetVelocity();
}
2021-06-18 22:38:32 +01:00
2021-05-07 19:17:09 +01:00
transform.localScale = Vector3.one * _currentScale;
_light.range = _baseLightRadius * _currentScale;
2021-05-07 19:23:24 +01:00
_thrusterRenderer.enabled = _currentScale > 0f;
_light.enabled = _currentScale > 0f;
2021-05-07 19:17:09 +01:00
}
2021-12-14 21:42:40 -08:00
private float GetThrustFraction() => Vector3.Dot(_attachedPlayer.JetpackAcceleration.AccelerationVariableSyncer.Value, _thrusterFilter);
2021-10-27 10:07:08 +01:00
private void OnRenderObject()
{
if (!WorldObjectManager.AllObjectsReady || !QSBCore.ShowLinesInDebug)
2021-10-27 10:07:08 +01:00
{
return;
}
2021-11-07 09:52:03 +00:00
Popcron.Gizmos.Sphere(_light.transform.position, 0.05f, Color.yellow, 4);
2021-10-27 10:07:08 +01:00
Popcron.Gizmos.Line(_light.transform.position, _light.transform.parent.position, Color.yellow);
}
2021-05-07 19:17:09 +01:00
}
}