-
Notifications
You must be signed in to change notification settings - Fork 0
V0.3.0 #19
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
Merged
Merged
V0.3.0 #19
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using HarmonyLib; | ||
using System; | ||
|
||
namespace KSPCommunityPartModules.HarmonyPatches | ||
{ | ||
[HarmonyPatch(typeof(ModuleParachute))] | ||
public class ModuleParachuteEvents | ||
{ | ||
public static void ModuleManagerPostLoad() | ||
{ | ||
Harmony harmony = new Harmony("KSPCommunityPartModules.ModuleParachutePatch"); | ||
harmony.PatchAll(); | ||
} | ||
|
||
public static event Action<ModuleParachute> OnDeployed; | ||
public static event Action<ModuleParachute> OnRepacked; | ||
|
||
[HarmonyPostfix] | ||
[HarmonyPatch("OnParachuteSemiDeployed")] | ||
static void RaiseEventOnSemiDeployed(ModuleParachute __instance) | ||
{ | ||
OnDeployed?.Invoke(__instance); | ||
} | ||
|
||
[HarmonyPostfix] | ||
[HarmonyPatch("OnParachuteFullyDeployed")] | ||
static void RaiseEventOnFullyDeployed(ModuleParachute __instance) | ||
{ | ||
OnDeployed?.Invoke(__instance); | ||
} | ||
|
||
[HarmonyPostfix] | ||
[HarmonyPatch("Repack")] | ||
static void RaiseEventOnRepack(ModuleParachute __instance) | ||
{ | ||
OnRepacked?.Invoke(__instance); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Usecase: Making the Center of Pressure, Mass or Lift follow a transform on the same part. | ||
Example: BoringCrewServices Starliner main parachutes, | ||
StarshipExpansionProject Starship Flaps | ||
Originally By: Sofie Brink & JonnyOThan | ||
Originally For: KSPCommunityPartModules | ||
*/ | ||
using UnityEngine; | ||
|
||
namespace KSPCommunityPartModules.Modules | ||
{ | ||
public class ModuleCenterFollowTransform : PartModule | ||
{ | ||
public const string MODULENAME = nameof(ModuleCenterFollowTransform); | ||
|
||
[KSPField] | ||
public bool enableCoP = false; | ||
private bool wasEnabledCoP; | ||
|
||
[KSPField] | ||
public bool enableCoM = false; | ||
private bool wasEnabledCoM; | ||
|
||
[KSPField] | ||
public bool enableCoL = false; | ||
private bool wasEnabledCoL; | ||
|
||
[KSPField] | ||
public string transformName; | ||
|
||
[SerializeField] | ||
private Transform followTransform; | ||
|
||
public override void OnLoad(ConfigNode node) | ||
{ | ||
bool anyModeActive = enableCoP || enableCoM || enableCoL; | ||
|
||
if (followTransform == null || followTransform.name != transformName) | ||
{ | ||
if (transformName != null) followTransform = part.FindModelTransform(transformName); | ||
if (followTransform == null) Debug.LogError($"[{MODULENAME}] transformName '{transformName}' was empty or does not exist on part '{part.partInfo?.name}'"); | ||
} | ||
if (!anyModeActive) | ||
{ | ||
Debug.LogWarning($"[{MODULENAME}] no center is following transformName '{transformName}' on part '{part.partInfo?.name}'"); | ||
} | ||
|
||
if (followTransform == null && part.partInfo != null) | ||
{ | ||
// this may be important if someone is swapping out versions of this module with B9PS | ||
// Note this probably isn't correct for parts that also have modules that mess with this field (e.g. ModuleProceduralFairing) | ||
if (enableCoP) part.CoPOffset = part.partInfo.partPrefab.CoPOffset; | ||
if (enableCoM) part.CoMOffset = part.partInfo.partPrefab.CoMOffset; | ||
if (enableCoL) part.CoLOffset = part.partInfo.partPrefab.CoLOffset; | ||
} | ||
// Set offets back to the values in the prefab if the mode is disabled after initial initialisation; e.g. B9PS | ||
if (!enableCoP && wasEnabledCoP) part.CoPOffset = part.partInfo.partPrefab.CoPOffset; | ||
if (!enableCoM && wasEnabledCoM) part.CoMOffset = part.partInfo.partPrefab.CoMOffset; | ||
if (!enableCoL && wasEnabledCoL) part.CoLOffset = part.partInfo.partPrefab.CoLOffset; | ||
wasEnabledCoP = enableCoP; | ||
wasEnabledCoM = enableCoM; | ||
wasEnabledCoL = enableCoL; | ||
|
||
// NOTE: isEnabled will be persisted to the save file, but we want to treat it purely as runtime state | ||
isEnabled = followTransform != null && anyModeActive; | ||
enabled = followTransform != null && anyModeActive && HighLogic.LoadedSceneIsFlight; | ||
} | ||
|
||
public void FixedUpdate() | ||
{ | ||
// Note that we shouldn't ever get here if the transform just didn't exist | ||
// But it's certainly possible for *something* to delete it later | ||
if (followTransform == null) | ||
{ | ||
isEnabled = false; | ||
enabled = false; | ||
} | ||
else | ||
{ | ||
Vector3 offset = part.transform.InverseTransformPoint(followTransform.position); | ||
if (enableCoP) part.CoPOffset = offset; | ||
if (enableCoM) part.CoMOffset = offset; | ||
if (enableCoL) part.CoLOffset = offset; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm debating if this should only be done here or in fixed-update too.
Doing it here is probably better for performance, while doing it in fixed-update would mean if something other than B9PS manipulates the KSPFields without calling OnLoad the module would reset the now no longer updating center too.