Skip to content

Commit

Permalink
Rework fairing side auto-strut logic. (#55)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
siimav authored Jun 6, 2024
1 parent 6545fe1 commit 9427f82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Source/ProceduralFairings/FairingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Part> shieldedParts) => RemoveJoints();

public void OnShieldingEnabled(List<Part> shieldedParts)
Expand All @@ -305,6 +311,8 @@ public void OnShieldingEnabled(List<Part> shieldedParts)
StartCoroutine(CreateAutoStruts());
}

public void OnFairingDecouple(Part fairingPart) => RemoveJoints();

void OnPartAttach(GameEvents.HostTargetAction<Part, Part> action)
{
// On loading any craft, the sideFairing knows its shape already.
Expand Down Expand Up @@ -335,8 +343,6 @@ void OnVesselModified(Vessel v)
{
if (vessel == v && !part.packed && Mode == BaseMode.Adapter)
{
if (GetTopPart() == null)
RemoveJoints();
StartCoroutine(HandleAutomaticDecoupling());
}
}
Expand Down Expand Up @@ -760,7 +766,6 @@ public bool FairingPresent
return false;
}
}
private Part FindTopBasePart() => Mode == BaseMode.Adapter ? GetTopPart() : null;
private bool HasTopOrSideNode() => HasNodeComponent<ProceduralFairingSide>(part.FindAttachNodes("connect")) is AttachNode;

void SetNodeVisibility(AttachNode node, bool show) => node.position.x = show ? 0 : 10000;
Expand Down Expand Up @@ -797,21 +802,20 @@ private IEnumerator<YieldInstruction> CreateAutoStruts()
if (part.GetComponent<KzNodeNumberTweaker>() 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<ProceduralFairingSide>() 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<ConfigurableJoint>() is ConfigurableJoint joint)
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Source/ProceduralFairings/FairingDecoupler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ private IEnumerator HandleFairingDecouple()
yield return new WaitForFixedUpdate();
if (part.parent)
{
if (part.parent.FindModuleImplementing<ProceduralFairingBase>() is ProceduralFairingBase fbase)
{
fbase.OnFairingDecouple(part);
}
part.decouple();
ejectFx.audio.Play();
decoupled = true;
Expand Down

0 comments on commit 9427f82

Please sign in to comment.