-
Notifications
You must be signed in to change notification settings - Fork 32
Directly update tank type and volume for ModuleFuelTanks #183
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ public class ModuleDataHandlerBasic : PartModifierBase | |
| protected readonly ConfigNode originalNode; | ||
| protected readonly ConfigNode dataNode; | ||
|
|
||
| private readonly BaseEventDetails moduleDataChangedEventDetails; | ||
| protected readonly BaseEventDetails moduleDataChangedEventDetails; | ||
|
|
||
| public ModuleDataHandlerBasic(PartModule module, ConfigNode originalNode, ConfigNode dataNode, BaseEventDetails moduleDataChangedEventDetails) | ||
| { | ||
|
|
@@ -34,16 +34,54 @@ public ModuleDataHandlerBasic(PartModule module, ConfigNode originalNode, Config | |
| public override void OnWillBeCopiedActiveSubtype() => Deactivate(); | ||
| public override void OnWasCopiedActiveSubtype() => Activate(); | ||
|
|
||
| private void Activate() | ||
| protected virtual void Activate() | ||
| { | ||
| module.Load(dataNode); | ||
| module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails); | ||
| } | ||
|
|
||
| private void Deactivate() | ||
| protected virtual void Deactivate() | ||
| { | ||
| module.Load(originalNode); | ||
| module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails); | ||
| } | ||
| } | ||
|
|
||
| public class ModuleFuelTanksHandler : ModuleDataHandlerBasic | ||
| { | ||
| public ModuleFuelTanksHandler( | ||
| PartModule module, ConfigNode originalNode, ConfigNode dataNode, BaseEventDetails moduleDataChangedEventDetails | ||
| ) : base(module, originalNode, dataNode, moduleDataChangedEventDetails) | ||
| { } | ||
|
|
||
| protected override void Activate() => applyNode(dataNode); | ||
| protected override void Deactivate() => applyNode(originalNode); | ||
|
|
||
| private void applyNode(ConfigNode sourceNode) { | ||
| double volume = 0; | ||
| bool setsVolume = sourceNode.TryGetValue("volume", ref volume); | ||
| string type = null; | ||
| bool setsType = sourceNode.TryGetValue("type", ref type); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I see we're throwing away everything on the node other than |
||
|
|
||
| if (setsVolume) { | ||
| // Update the tank volume by sending a volume-change event in the format that Procedural Parts uses. | ||
| // Procedural Parts reports the outside volume of the tank in cubic meters (or equivalently, | ||
| // kiloliters). ModuleFuelTanks scales the reported volume by the `tankVolumeConversion` and | ||
| // `utilization` fields to compute the available internal volume in liters. | ||
| // Since the `volume` configuration field is meant to set the available volume in liters directly, | ||
| // we need to read the scaling values and apply the inverse scaling to the value that we send. | ||
| float scaleFactor = module.Fields.GetValue<float>("tankVolumeConversion"); | ||
| float utilization = module.Fields.GetValue<float>("utilization"); | ||
| var evtDetails = new BaseEventDetails(BaseEventDetails.Sender.USER); | ||
| evtDetails.Set<string>("volName", "Tankage"); | ||
| evtDetails.Set<double>("newTotalVolume", volume * 100 / utilization / scaleFactor); | ||
| module.part.SendEvent("OnPartVolumeChanged", evtDetails, 0); | ||
| } | ||
| if (setsType) { | ||
| module.Fields.SetValue("type", type); | ||
| } | ||
| module.Events.Send("ModuleDataChanged", moduleDataChangedEventDetails); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
This should be in its own file
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.
Also can we get rid of the inheritance? It doesn't really use any of the base behavior other than the list of events it responds to which is better just copied.
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.
Err to clarify, it should inherit from
PartModifierBaserather thanModuleDataHandlerBasic