|
| 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 | +} |
0 commit comments