From 9427f823a2686c004afc1045bc3ea9e1120e1c96 Mon Sep 17 00:00:00 2001 From: siimav <1120038+siimav@users.noreply.github.com> Date: Fri, 7 Jun 2024 02:17:27 +0300 Subject: [PATCH] Rework fairing side auto-strut logic. (#55) Removes the old code that added a joint between fairing sides and the fairing base payload. Now fairing sides are jointed to each other - from the tip of one fairing side to the transform (extreme bottom) of another. --- Source/ProceduralFairings/FairingBase.cs | 23 +++++++++++-------- Source/ProceduralFairings/FairingDecoupler.cs | 4 ++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Source/ProceduralFairings/FairingBase.cs b/Source/ProceduralFairings/FairingBase.cs index 2d2e11e..49e1bdd 100644 --- a/Source/ProceduralFairings/FairingBase.cs +++ b/Source/ProceduralFairings/FairingBase.cs @@ -297,6 +297,12 @@ private void OnEditorShipModified(ShipConstruct ship) public void OnPartPack() => RemoveJoints(); + public void OnPartUnpack() + { + if (HighLogic.LoadedSceneIsFlight && autoStrutSides) + StartCoroutine(CreateAutoStruts()); + } + public void OnShieldingDisabled(List shieldedParts) => RemoveJoints(); public void OnShieldingEnabled(List shieldedParts) @@ -305,6 +311,8 @@ public void OnShieldingEnabled(List shieldedParts) StartCoroutine(CreateAutoStruts()); } + public void OnFairingDecouple(Part fairingPart) => RemoveJoints(); + void OnPartAttach(GameEvents.HostTargetAction action) { // On loading any craft, the sideFairing knows its shape already. @@ -335,8 +343,6 @@ void OnVesselModified(Vessel v) { if (vessel == v && !part.packed && Mode == BaseMode.Adapter) { - if (GetTopPart() == null) - RemoveJoints(); StartCoroutine(HandleAutomaticDecoupling()); } } @@ -760,7 +766,6 @@ public bool FairingPresent return false; } } - private Part FindTopBasePart() => Mode == BaseMode.Adapter ? GetTopPart() : null; private bool HasTopOrSideNode() => HasNodeComponent(part.FindAttachNodes("connect")) is AttachNode; void SetNodeVisibility(AttachNode node, bool show) => node.position.x = show ? 0 : 10000; @@ -797,21 +802,20 @@ private IEnumerator CreateAutoStruts() if (part.GetComponent() is KzNodeNumberTweaker nnt && part.FindAttachNodes("connect") is AttachNode[] attached) { - Part topBasePart = FindTopBasePart(); for (int i = 0; i < nnt.numNodes; ++i) { if (attached[i].attachedPart is Part p && p.Rigidbody && - attached[i > 0 ? i - 1 : nnt.numNodes - 1].attachedPart is Part pp) + attached[i > 0 ? i - 1 : nnt.numNodes - 1].attachedPart is Part pp && + p.FindModuleImplementing() is ProceduralFairingSide pfs) { - AddStrut(p, pp); - if (topBasePart != null) - AddStrut(p, topBasePart); + Vector3 offsetToFairingTop = pfs.height * Vector3.up + pfs.meshPos; + AddStrut(p, pp, offsetToFairingTop); } } } } - private ConfigurableJoint AddStrut(Part p, Part pp) + private ConfigurableJoint AddStrut(Part p, Part pp, Vector3 anchorOffset) { if (p && p != pp && p.Rigidbody != pp.Rigidbody && pp.Rigidbody is Rigidbody rb && p.gameObject.AddComponent() is ConfigurableJoint joint) @@ -827,6 +831,7 @@ private ConfigurableJoint AddStrut(Part p, Part pp) joint.breakForce = p.breakingForce; joint.breakTorque = p.breakingTorque; joint.connectedBody = rb; + joint.anchor = anchorOffset; joints.Add(joint); return joint; diff --git a/Source/ProceduralFairings/FairingDecoupler.cs b/Source/ProceduralFairings/FairingDecoupler.cs index c04405c..33d1545 100644 --- a/Source/ProceduralFairings/FairingDecoupler.cs +++ b/Source/ProceduralFairings/FairingDecoupler.cs @@ -94,6 +94,10 @@ private IEnumerator HandleFairingDecouple() yield return new WaitForFixedUpdate(); if (part.parent) { + if (part.parent.FindModuleImplementing() is ProceduralFairingBase fbase) + { + fbase.OnFairingDecouple(part); + } part.decouple(); ejectFx.audio.Play(); decoupled = true;