Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework fairing side auto-strut logic #55

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading