diff --git a/QSB/Audio/Patches/ThrusterAudioPatches.cs b/QSB/Audio/Patches/ThrusterAudioPatches.cs index d8973d33..c50956ca 100644 --- a/QSB/Audio/Patches/ThrusterAudioPatches.cs +++ b/QSB/Audio/Patches/ThrusterAudioPatches.cs @@ -2,12 +2,6 @@ using QSB.Audio.Messages; using QSB.Messaging; using QSB.Patches; -using QSB.Player; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QSB.Audio.Patches; @@ -38,7 +32,9 @@ internal class ThrusterAudioPatches : QSBPatch if (tracker.LastPlayed != AudioType.None) { if (__instance is ShipThrusterAudio) + { new ShipThrusterAudioOneShotMessage(tracker.LastPlayed, tracker.Pitch, tracker.Volume).Send(); + } } } } diff --git a/QSB/ModelShip/ModelShipThrusterVariableSyncer.cs b/QSB/ModelShip/ModelShipThrusterVariableSyncer.cs index dacfe2d7..d1e9a404 100644 --- a/QSB/ModelShip/ModelShipThrusterVariableSyncer.cs +++ b/QSB/ModelShip/ModelShipThrusterVariableSyncer.cs @@ -1,5 +1,6 @@ using Mirror; using QSB.Player; +using QSB.Utility; using QSB.Utility.VariableSync; using System.Collections.Generic; using System.Linq; @@ -7,7 +8,7 @@ using UnityEngine; namespace QSB.ModelShip; -public class ModelShipThrusterVariableSyncer : NetworkBehaviour +public class ModelShipThrusterVariableSyncer : MonoBehaviour { public Vector3VariableSyncer AccelerationSyncer; @@ -15,21 +16,17 @@ public class ModelShipThrusterVariableSyncer : NetworkBehaviour private ThrusterAudio _thrusterAudio; public List ThrusterFlameControllers = new(); - public static ModelShipThrusterVariableSyncer LocalInstance; - - public void Start() + public void Init(GameObject modelShip) { - LocalInstance = this; - } + DebugLog.ToConsole("init model ship"); - public void Init() - { - _thrusterModel = gameObject.GetComponent(); - _thrusterAudio = gameObject.GetComponentInChildren(); + _thrusterModel = modelShip.GetComponent(); + _thrusterAudio = modelShip.GetComponentInChildren(); ThrusterFlameControllers.Clear(); - foreach (var item in gameObject.GetComponentsInChildren()) + foreach (var item in modelShip.GetComponentsInChildren()) { + DebugLog.ToConsole("adding thruster"); ThrusterFlameControllers.Add(item); } } @@ -38,14 +35,18 @@ public class ModelShipThrusterVariableSyncer : NetworkBehaviour { if (QSBPlayerManager.LocalPlayer.FlyingModelShip) { + DebugLog.ToConsole($"{QSBPlayerManager.LocalPlayerId} is flying the model ship"); GetFromShip(); return; } if (AccelerationSyncer.public_HasChanged()) { + DebugLog.ToConsole("value changed"); + if (AccelerationSyncer.Value == Vector3.zero) { + DebugLog.ToConsole("not flying"); foreach (var item in ThrusterFlameControllers) { item.OnStopTranslationalThrust(); @@ -57,6 +58,7 @@ public class ModelShipThrusterVariableSyncer : NetworkBehaviour } else { + DebugLog.ToConsole("flying"); foreach (var item in ThrusterFlameControllers) { item.OnStartTranslationalThrust(); @@ -71,8 +73,10 @@ public class ModelShipThrusterVariableSyncer : NetworkBehaviour private void GetFromShip() { + DebugLog.ToConsole("Getting from ship"); if (_thrusterModel) { + DebugLog.ToConsole("Update local acc"); AccelerationSyncer.Value = _thrusterModel.GetLocalAcceleration(); } } diff --git a/QSB/ModelShip/Patches/ModelShipThrusterPatches.cs b/QSB/ModelShip/Patches/ModelShipThrusterPatches.cs index fe5637e5..d396a925 100644 --- a/QSB/ModelShip/Patches/ModelShipThrusterPatches.cs +++ b/QSB/ModelShip/Patches/ModelShipThrusterPatches.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using QSB.ModelShip.TransformSync; using QSB.Patches; using QSB.Player; using System.Linq; @@ -14,26 +15,18 @@ internal class ModelShipThrusterPatches : QSBPatch [HarmonyPatch(typeof(ThrusterFlameController), nameof(ThrusterFlameController.GetThrustFraction))] public static bool GetThrustFraction(ThrusterFlameController __instance, ref float __result) { - if (!ModelShipThrusterVariableSyncer.LocalInstance.ThrusterFlameControllers.Contains(__instance)) - { - return true; - } + var modelShipThrusters = ModelShipTransformSync.LocalInstance?.ThrusterVariableSyncer; - if (!__instance._thrusterModel.IsThrusterBankEnabled(OWUtilities.GetShipThrusterBank(__instance._thruster))) - { - __result = 0f; - return false; - } + if (modelShipThrusters == null) return true; - if (QSBPlayerManager.LocalPlayer.FlyingModelShip) + if (modelShipThrusters.ThrusterFlameControllers.Contains(__instance) && !QSBPlayerManager.LocalPlayer.FlyingModelShip) { - __result = Vector3.Dot(__instance._thrusterModel.GetLocalAcceleration(), __instance._thrusterFilter); + if(__instance._thrusterModel.IsThrusterBankEnabled(OWUtilities.GetShipThrusterBank(__instance._thruster))) + { + __result = Vector3.Dot(modelShipThrusters.AccelerationSyncer.Value, __instance._thrusterFilter); + return false; + } } - else - { - __result = Vector3.Dot(ModelShipThrusterVariableSyncer.LocalInstance.AccelerationSyncer.Value, __instance._thrusterFilter); - } - - return false; + return true; } } diff --git a/QSB/ModelShip/TransformSync/ModelShipTransformSync.cs b/QSB/ModelShip/TransformSync/ModelShipTransformSync.cs index 23c72c26..29b08c09 100644 --- a/QSB/ModelShip/TransformSync/ModelShipTransformSync.cs +++ b/QSB/ModelShip/TransformSync/ModelShipTransformSync.cs @@ -1,4 +1,5 @@ -using QSB.Syncs.Sectored.Rigidbodies; +using QSB.ShipSync; +using QSB.Syncs.Sectored.Rigidbodies; using QSB.Utility; using QSB.WorldSync; @@ -8,6 +9,8 @@ internal class ModelShipTransformSync : SectoredRigidbodySync { public static ModelShipTransformSync LocalInstance { get; private set; } + public ModelShipThrusterVariableSyncer ThrusterVariableSyncer { get; private set; } + public override void OnStartClient() { base.OnStartClient(); @@ -31,6 +34,14 @@ internal class ModelShipTransformSync : SectoredRigidbodySync return modelShip; } + protected override void Init() + { + base.Init(); + + ThrusterVariableSyncer = this.GetRequiredComponent(); + ThrusterVariableSyncer.Init(AttachedRigidbody.gameObject); + } + /// /// replacement for base method /// using SetPos/Rot instead of Move