Skip to content

Commit 0dd34d9

Browse files
committed
First pass ModuleCenterFollowTransform
- Added ModuleCenterFollowTransform to replace ModuleCoPFollowTransform. - Deprecated ModuleCoPFollowTransform.
1 parent 0cc450e commit 0dd34d9

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

Source/KSPCommunityPartModules.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<ItemGroup>
3737
<Compile Include="HarmonyPatches\ModuleParachutePatch.cs" />
3838
<Compile Include="Modules\ModuleAutoCutDrogue.cs" />
39+
<Compile Include="Modules\ModuleCenterFollowTransform.cs" />
3940
<Compile Include="Modules\ModuleCoPFollowTransform.cs" />
4041
<Compile Include="Properties\AssemblyInfo.cs" />
4142
</ItemGroup>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Usecase: Making the Center of Pressure, Mass or Lift follow a transform on the same part.
3+
Example: BoringCrewServices Starliner main parachutes,
4+
StarshipExpansionProject Starship Flaps
5+
Originally By: Sofie Brink & JonnyOThan
6+
Originally For: KSPCommunityPartModules
7+
*/
8+
using UnityEngine;
9+
10+
namespace KSPCommunityPartModules.Modules
11+
{
12+
public class ModuleCenterFollowTransform : PartModule
13+
{
14+
public const string MODULENAME = nameof(ModuleCenterFollowTransform);
15+
16+
[KSPField]
17+
public bool enableCoP = false;
18+
19+
[KSPField]
20+
public bool enableCoM = false;
21+
22+
[KSPField]
23+
public bool enableCoL = false;
24+
25+
[KSPField]
26+
public string transformName;
27+
28+
[SerializeField]
29+
private Transform followTransform;
30+
31+
// TODO: Reset offset to prefab value if B9PS disables one of the fields.
32+
public override void OnLoad(ConfigNode node)
33+
{
34+
bool anyModeActive = enableCoP || enableCoM || enableCoL;
35+
36+
if (followTransform == null || followTransform.name != transformName)
37+
{
38+
if (transformName != null) followTransform = part.FindModelTransform(transformName);
39+
if (followTransform == null) Debug.LogError($"[{MODULENAME}] transformName '{transformName}' was empty or does not exist on part '{part.partInfo?.name}'");
40+
}
41+
if (!anyModeActive)
42+
{
43+
Debug.LogWarning($"[{MODULENAME}] no center is following transformName '{transformName}' on part '{part.partInfo?.name}'");
44+
}
45+
46+
if (followTransform == null && part.partInfo != null)
47+
{
48+
// this may be important if someone is swapping out versions of this module with B9PS
49+
// Note this probably isn't correct for parts that also have modules that mess with this field (e.g. ModuleProceduralFairing)
50+
if (enableCoP) part.CoPOffset = part.partInfo.partPrefab.CoPOffset;
51+
if (enableCoM) part.CoMOffset = part.partInfo.partPrefab.CoMOffset;
52+
if (enableCoL) part.CoLOffset = part.partInfo.partPrefab.CoLOffset;
53+
}
54+
55+
// NOTE: isEnabled will be persisted to the save file, but we want to treat it purely as runtime state
56+
isEnabled = followTransform != null && anyModeActive;
57+
enabled = followTransform != null && anyModeActive && HighLogic.LoadedSceneIsFlight;
58+
}
59+
60+
public void FixedUpdate()
61+
{
62+
// Note that we shouldn't ever get here if the transform just didn't exist
63+
// But it's certainly possible for *something* to delete it later
64+
if (followTransform == null)
65+
{
66+
isEnabled = false;
67+
enabled = false;
68+
}
69+
else
70+
{
71+
Vector3 offset = part.transform.InverseTransformPoint(followTransform.position);
72+
if (enableCoP) part.CoPOffset = offset;
73+
if (enableCoM) part.CoMOffset = offset;
74+
if (enableCoL) part.CoLOffset = offset;
75+
}
76+
}
77+
}
78+
}

Source/Modules/ModuleCoPFollowTransform.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ModuleCoPFollowTransform : PartModule
2020

2121
public override void OnLoad(ConfigNode node)
2222
{
23+
Debug.LogWarning($"[{MODULENAME}] This module is deprecated! please use ModuleCenterFollowTransform instead!");
2324
if (followTransform == null || followTransform.name != transformName)
2425
{
2526
if (transformName != null) followTransform = part.FindModelTransform(transformName);

0 commit comments

Comments
 (0)