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

89 lines
2.4 KiB
C#
Raw Normal View History

2021-05-07 13:58:37 +00:00
using QSB.Player;
using QSB.Utility;
using UnityEngine;
namespace QSB.Animation.Player.Thrusters
2021-05-07 13:58:37 +00:00
{
internal class RemoteThrusterWashController : MonoBehaviour
{
[SerializeField]
private float _raycastDistance = 10f;
2022-02-13 21:01:54 +00:00
[SerializeField]
private AnimationCurve _emissionDistanceScale;
2022-02-13 21:01:54 +00:00
[SerializeField]
private AnimationCurve _emissionThrusterScale;
2022-02-13 21:01:54 +00:00
[SerializeField]
private ParticleSystem _defaultParticleSystem;
2021-05-07 13:58:37 +00:00
private ParticleSystem.MainModule _defaultMainModule;
private ParticleSystem.EmissionModule _defaultEmissionModule;
private float _baseDefaultEmissionRate;
2021-05-07 13:58:37 +00:00
private PlayerInfo _attachedPlayer;
2021-05-07 13:58:37 +00:00
private bool _initialised;
2021-05-07 13:58:37 +00:00
public void Init(PlayerInfo player)
2021-05-07 13:58:37 +00:00
{
_attachedPlayer = player;
2021-05-07 13:58:37 +00:00
if (_defaultParticleSystem == null)
{
DebugLog.ToConsole($"Error - DefaultParticleSystem is null!", OWML.Common.MessageType.Error);
return;
}
2021-06-18 21:38:32 +00:00
_defaultMainModule = _defaultParticleSystem.main;
_defaultEmissionModule = _defaultParticleSystem.emission;
_baseDefaultEmissionRate = _defaultEmissionModule.rateOverTime.constant;
2021-05-07 18:17:09 +00:00
_initialised = true;
2021-05-07 13:58:37 +00:00
}
private void Update()
2021-05-07 13:58:37 +00:00
{
if (!_initialised)
{
return;
}
2021-05-07 13:58:37 +00:00
RaycastHit hitInfo = default;
var aboveSurface = false;
var emissionThrusterScale = _emissionThrusterScale.Evaluate(_attachedPlayer.JetpackAcceleration.AccelerationVariableSyncer.Value.y);
if (emissionThrusterScale > 0f)
2021-05-07 13:58:37 +00:00
{
aboveSurface = Physics.Raycast(transform.position, transform.forward, out hitInfo, _raycastDistance, OWLayerMask.physicalMask);
2021-05-07 13:58:37 +00:00
}
emissionThrusterScale = (!aboveSurface) ? 0f : (emissionThrusterScale * _emissionDistanceScale.Evaluate(hitInfo.distance));
if (emissionThrusterScale > 0f)
2021-05-07 13:58:37 +00:00
{
var position = hitInfo.point + (hitInfo.normal * 0.25f);
var rotation = Quaternion.LookRotation(hitInfo.normal);
if (!_defaultParticleSystem.isPlaying)
{
_defaultParticleSystem.Play();
}
_defaultEmissionModule.rateOverTimeMultiplier = _baseDefaultEmissionRate * emissionThrusterScale;
_defaultParticleSystem.transform.SetPositionAndRotation(position, rotation);
if (_defaultMainModule.customSimulationSpace != hitInfo.transform)
{
_defaultMainModule.customSimulationSpace = hitInfo.transform;
_defaultParticleSystem.Clear();
}
2021-05-07 13:58:37 +00:00
}
else
2021-05-07 13:58:37 +00:00
{
if (_defaultParticleSystem.isPlaying)
{
_defaultParticleSystem.Stop(false, ParticleSystemStopBehavior.StopEmitting);
}
2021-05-07 13:58:37 +00:00
}
}
}
}