-
Notifications
You must be signed in to change notification settings - Fork 10
/
ModuleRODecoupler.cs
161 lines (132 loc) · 6.22 KB
/
ModuleRODecoupler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System.Text.RegularExpressions;
using UnityEngine;
namespace ROLib
{
public class ModuleRODecoupler : PartModule
{
private const string GroupName = "ModuleRODecoupler";
private const string GroupDisplayName = "RO-Decoupler";
public const float minImpulse = 0.1f;
#region KSPFields
/// <summary>
/// Maximum ejection impulse available.
/// </summary>
[KSPField] public float maxImpulse = 600.0f;
/// <summary>
/// Diameter exponent to multiply the force by.
/// </summary>
[KSPField] public float diamExponent = 0.525f;
/// <summary>
/// Diameter exponent to multiply the force by.
/// </summary>
[KSPField] public double triggerTime = 0;
/// <summary>
/// Displays the current Ejection Force
/// </summary>
[KSPField(guiActiveEditor = true, guiName = "Ejection Force:", guiUnits="N", guiFormat="F0", groupName = GroupName, groupDisplayName = GroupDisplayName)]
public float currentEjectionForce = 0.0f;
/// <summary>
/// Displays the current Ejection Force
/// </summary>
[KSPField(isPersistant = true, guiActive = true, guiName = "Remaining Time", guiFormat="F2", groupName = GroupName, groupDisplayName = GroupDisplayName)]
public double remainingTime = 0f;
/// <summary>
/// Does the part automatically decouple?
/// </summary>
[KSPField(isPersistant = true, guiActive =true, guiActiveEditor =true, guiName = "Auto Decouple", groupName = GroupName)]
public bool isAutoDecouple = false;
/// <summary>
/// Set the part to automatically decouple
/// </summary>
[KSPEvent(guiName = "Toggle Auto Decouple", guiActiveEditor = true)]
public void ToggleAutoDecouple()
{
isAutoDecouple = !isAutoDecouple;
this.ROLforEachSymmetryCounterpart(module => module.isAutoDecouple = this.isAutoDecouple);
ShowAutoDecoupleDelay();
}
/// <summary>
/// How many seconds until the other node automatically decouples
/// </summary>
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = false, guiName = "Decoupler Delay", guiUnits = "s", groupName = GroupName),
UI_FloatRange(suppressEditorShipModified = true)]
public float autoDecoupleDelay = 5f;
#endregion KSPFields
#region Private Variables
private ModuleDecouple[] decouplers;
private ModuleDecouple topDecoupler;
private ModuleDecouple bottomDecoupler;
private ModuleROTank modularPart;
private bool isCountingDown = false;
#endregion Private Variables
#region Standard KSP Overrides
public override void OnStartFinished(StartState state)
{
base.OnStartFinished(state);
part.ActivatesEvenIfDisconnected = true;
this.ROLupdateUIFloatRangeControl(nameof(autoDecoupleDelay), 0, 10, 1);
Fields[nameof(autoDecoupleDelay)].guiActiveEditor = isAutoDecouple;
decouplers = part.GetComponents<ModuleDecouple>();
topDecoupler = decouplers[0];
bottomDecoupler = decouplers[1];
topDecoupler.explosiveDir = Vector3.up;
ROLLog.log($"decouplers: {decouplers}");
ROLLog.log($"topDecoupler: {topDecoupler.explosiveNodeID}, {topDecoupler.explosiveDir}");
ROLLog.log($"bottomDecoupler: {bottomDecoupler.explosiveNodeID}, {bottomDecoupler.explosiveDir}");
modularPart = part.FindModuleImplementing<ModuleROTank>();
if (!(topDecoupler is ModuleDecouple && bottomDecoupler is ModuleDecouple && modularPart is ModuleROTank))
{
ROLLog.error($"{part} Unable to find ModuleDecouple or ModuleROTank modules");
isEnabled = enabled = false;
return;
}
else
{
topDecoupler.Events[nameof(ToggleStaging)].advancedTweakable = false;
bottomDecoupler.Events[nameof(ToggleStaging)].advancedTweakable = false;
if (modularPart is ModuleROTank)
modularPart.Fields[nameof(modularPart.currentDiameter)].uiControlEditor.onFieldChanged += OnDiameterChange;
UpdateImpulseValues();
}
}
public override void OnUpdate()
{
if (!isAutoDecouple) return;
if ((topDecoupler.isDecoupled || bottomDecoupler.isDecoupled) && triggerTime == 0)
{
isCountingDown = true;
triggerTime = Planetarium.GetUniversalTime();
ROLLog.log($"triggerTime: {triggerTime}");
}
if (isCountingDown && triggerTime > 0)
{
remainingTime = triggerTime + autoDecoupleDelay - Planetarium.GetUniversalTime();
ROLLog.log($"remainingTime: {remainingTime}");
if (remainingTime <= 0)
{
triggerTime = 0;
remainingTime = 0;
isCountingDown = false;
isAutoDecouple = false;
if (!topDecoupler.isDecoupled) topDecoupler.Decouple();
if (!bottomDecoupler.isDecoupled) bottomDecoupler.Decouple();
}
}
}
#endregion Standard KSP Overrides
#region Custom Methods
private void OnDiameterChange(BaseField bf, object obj) => UpdateImpulseValues();
public float EjectionForce => Mathf.Round(Mathf.Clamp(Mathf.Pow(modularPart.currentDiameter, diamExponent) * 100, minImpulse, maxImpulse));
public void UpdateImpulseValues()
{
currentEjectionForce = topDecoupler.ejectionForce = bottomDecoupler.ejectionForce = EjectionForce;
}
private void ShowAutoDecoupleDelay()
{
Fields[nameof(autoDecoupleDelay)].guiActive = isAutoDecouple;
Fields[nameof(autoDecoupleDelay)].guiActiveEditor = isAutoDecouple;
Fields[nameof(remainingTime)].guiActive = isAutoDecouple;
}
#endregion Custom Methods
}
}