-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModuleKerBalloon.cs
345 lines (303 loc) · 15.5 KB
/
ModuleKerBalloon.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using UnityEngine;
namespace KerBalloons
{
public class ModuleKerBalloon : PartModule
{
[KSPField(isPersistant = false)]
public string CFGballoonObject;
[KSPField(isPersistant = false)]
public string CFGropeObject;
[KSPField(isPersistant = false)]
public string CFGcapObject;
[KSPField(isPersistant = false)]
public string CFGliftPointObject;
[KSPField(isPersistant = false)]
public string CFGballoonPointObject;
public GameObject balloonObject;
public GameObject ropeObject;
public GameObject capObject;
public GameObject liftPointObject;
public GameObject balloonPointObject;
[KSPField(isPersistant = false)]
public float minAtmoPressure;
[KSPField(isPersistant = false)]
public float maxAtmoPressure ;
[KSPField(isPersistant = false)]
public float minScale;
[KSPField(isPersistant = false)]
public float maxScale;
[KSPField(isPersistant = false)]
public float minLift;
[KSPField(isPersistant = false)]
public float maxLift;
[KSPField(isPersistant = false)]
public string recommendedBody;
[KSPField(isPersistant = false)]
public float targetTWR;
[KSPField(isPersistant = false)]
public float liftLimit;
[KSPField(isPersistant = false)]
public bool speedLimiter;
[KSPField(isPersistant = false)]
public float maxSpeed;
[KSPField(isPersistant = false)]
public float maxSpeedTolerence;
[KSPField(isPersistant = false)]
public float speedAdjustStep;
[KSPField(isPersistant = false)]
public float speedAdjustMin;
[KSPField(isPersistant = false)]
public float speedAdjustMax;
public float speedAdjust;
[KSPField(isPersistant = true)]
public bool isInflating;
[KSPField(isPersistant = true)]
public bool hasInflated;
[KSPField(isPersistant = true)]
public bool isInflated;
[KSPField(isPersistant = true)]
public bool isDeflating;
[KSPField(isPersistant = true)]
public bool hasBurst;
[KSPField(isPersistant = true)]
public bool isRepacked;
[KSPField(isPersistant = false)]
public float scaleInc;
[KSPField(isPersistant = false)]
public string bodyName;
[KSPField(isPersistant = false)]
public float bodyG;
public override void OnStart(StartState state)
{
Debug.Log("ModuleKerBalloon Loaded");
if (HighLogic.LoadedSceneIsFlight)
{
balloonObject = getChildGameObject(this.part.gameObject, CFGballoonObject);
ropeObject = getChildGameObject(this.part.gameObject, CFGropeObject);
capObject = getChildGameObject(this.part.gameObject, CFGcapObject);
liftPointObject = getChildGameObject(this.part.gameObject, CFGliftPointObject);
balloonPointObject = getChildGameObject(this.part.gameObject, CFGballoonPointObject);
balloonObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
initialBalloonScale = balloonObject.transform.localScale;
initialBalloonPos = balloonObject.transform.transform.localPosition;
initialRopeScale = ropeObject.transform.localScale;
if(hasInflated && !isInflated)
{
balloonObject.SetActive(false);
ropeObject.SetActive(false);
capObject.SetActive(false);
}else if(isInflating)
{
repackBalloon();
}
else if (isDeflating)
{
balloonObject.SetActive(false);
ropeObject.SetActive(false);
isInflated = false;
isDeflating = false;
isRepacked = false;
}
}
}
public void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight)
{
//print("hasInflated: " + hasInflated + " | isInflated: " + isInflated + " | isInflating: " + isInflating + " | isDeflating: " + isDeflating + " | hasBurst: " + hasBurst + " | isRepacked: " + isRepacked);
float currentPressure = (float)FlightGlobals.getStaticPressure(this.part.transform.position);
if (hasInflated && !hasBurst)
{
if (isInflated)
{
float lift = BalloonProperties.getLift(this);
this.part.Rigidbody.AddForceAtPosition(vessel.upAxis * lift,liftPointObject.transform.position);
Vector3 scale = new Vector3(BalloonProperties.getScale(this), BalloonProperties.getScale(this), BalloonProperties.getScale(this));
balloonObject.transform.localScale = scale;
ropeObject.transform.rotation = Quaternion.Slerp(ropeObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), BalloonProperties.getLift(this) / 10);
balloonObject.transform.rotation = Quaternion.Slerp(balloonObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), BalloonProperties.getLift(this) / 8);
balloonObject.transform.position = balloonPointObject.transform.position;
if (currentPressure < minAtmoPressure || currentPressure > maxAtmoPressure) hasBurst = true;
}
else if (isDeflating)
{
if (scaleInc > 0)
{
scaleInc -= BalloonProperties.getScale(this) / 100;
balloonObject.transform.localScale = new Vector3(scaleInc, scaleInc, scaleInc);
float progress = scaleInc / BalloonProperties.getScale(this);
float lift = BalloonProperties.getLift(this) * progress;
this.part.Rigidbody.AddForceAtPosition(vessel.upAxis * lift, liftPointObject.transform.position);
ropeObject.transform.localScale = new Vector3(1, 1, progress);
balloonObject.transform.position = balloonPointObject.transform.position;
}
else
{
balloonObject.SetActive(false);
ropeObject.SetActive(false);
isInflated = false;
isDeflating = false;
isRepacked = false;
}
}else if(!isInflated && !isInflating && !isDeflating && !isRepacked)
{
Events["repackBalloon"].active = true;
}
}
else if (isInflating && !hasBurst)
{
if (scaleInc < BalloonProperties.getScale(this))
{
scaleInc += BalloonProperties.getScale(this)/200;
balloonObject.transform.localScale = new Vector3(scaleInc, scaleInc, scaleInc);
float progress = scaleInc / BalloonProperties.getScale(this);
float lift = BalloonProperties.getLift(this) * progress;
this.part.Rigidbody.AddForceAtPosition(vessel.upAxis * lift, liftPointObject.transform.position);
ropeObject.transform.rotation = Quaternion.Slerp(ropeObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), BalloonProperties.getLift(this) / 10);
balloonObject.transform.rotation = Quaternion.Slerp(balloonObject.transform.rotation, Quaternion.LookRotation(vessel.upAxis, vessel.upAxis), BalloonProperties.getLift(this) / 8);
ropeObject.transform.localScale = new Vector3(1, 1, progress);
balloonObject.transform.position = balloonPointObject.transform.position;
}
else
{
hasInflated = true;
isInflated = true;
isInflating = false;
}
}
else if (hasBurst && (isInflated || isInflating || isDeflating))
{
this.part.Effect("burst");
isInflated = false;
isInflating = false;
isDeflating = false;
balloonObject.SetActive(false);
ropeObject.SetActive(false);
Events["inflateBalloon"].active = false;
Events["deflateBalloon"].active = false;
Actions["inflateAction"].active = false;
Actions["deflateAction"].active = false;
}
}
}
public Vector3 initialBalloonScale;
public Vector3 initialBalloonPos;
public Vector3 initialRopeScale;
[KSPEvent(active = false, guiActive = false, guiActiveEditor = false, guiActiveUnfocused = true, unfocusedRange = 4, externalToEVAOnly = true, guiName = "Repack Balloon")]
public void repackBalloon()
{
isInflated = false;
isInflating = false;
isDeflating = false;
hasBurst = false;
hasInflated = false;
isRepacked = true;
balloonObject.transform.localScale = initialBalloonScale;
balloonObject.transform.localPosition = initialBalloonPos;
ropeObject.transform.localScale = initialBalloonScale;
capObject.SetActive(true);
balloonObject.SetActive(true);
ropeObject.SetActive(true);
Events["repackBalloon"].active = false;
Events["inflateBalloon"].active = true;
Events["deflateBalloon"].active = false;
Actions["inflateAction"].active = true;
Actions["deflateAction"].active = false;
}
[KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Inflate Balloon")]
public void inflateBalloon()
{
if (!isInflated)
{
float currentPressure = (float)FlightGlobals.getStaticPressure(this.part.transform.position);
if (currentPressure > minAtmoPressure && currentPressure < maxAtmoPressure)
{
Debug.Log("Inflating Balloon!");
this.part.Effect("inflate");
speedAdjust = 1;
isInflating = true;
capObject.SetActive(false);
Events["inflateBalloon"].active = false;
Events["deflateBalloon"].active = true;
}
else
{
if (currentPressure <= 0)
{
ScreenMessages.PostScreenMessage("Cannot inflate balloon in vacuum", 3, ScreenMessageStyle.UPPER_CENTER);
}
else if (currentPressure < minAtmoPressure)
{
ScreenMessages.PostScreenMessage("Cannot Inflate: Air pressure too low", 3, ScreenMessageStyle.UPPER_CENTER);
}
else if (currentPressure> maxAtmoPressure)
{
ScreenMessages.PostScreenMessage("Cannot Inflate: Air pressure too high", 3, ScreenMessageStyle.UPPER_CENTER);
}
}
}
}
[KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deflate Balloon")]
public void deflateBalloon()
{
if (isInflated)
{
Debug.Log("Deflating Balloon!");
if (!hasBurst) { this.part.Effect("deflate"); }
Events["deflateBalloon"].active = false;
isInflated = false;
isDeflating = true;
}
}
[KSPAction("Inflate Balloon")]
public void inflateAction(KSPActionParam param)
{
inflateBalloon();
Actions["inflateAction"].active = false;
}
[KSPAction("Deflate Balloon")]
public void deflateAction(KSPActionParam param)
{
deflateBalloon();
Actions["deflateAction"].active = false;
}
static public GameObject getChildGameObject(GameObject fromGameObject, string withName)
{
Transform[] ts = fromGameObject.transform.GetComponentsInChildren<Transform>();
foreach (Transform t in ts) if (t.gameObject.name == withName) return t.gameObject;
return null;
}
public override string GetInfo()
{
//I know about FlightGlobal.Bodies() but for some reason when I use it in this function the game freezes on load
//Also it can't be put in OnStart() because that isn't called until the part is created
bodyName = recommendedBody;
if (recommendedBody == "Sun") bodyG = 17.1f;
if (recommendedBody == "Kerbin") bodyG = 9.81f;
if (recommendedBody == "Mun") bodyG = 1.63f;
if (recommendedBody == "Minmus") bodyG = 0.491f;
if (recommendedBody == "Moho") bodyG = 2.70f;
if (recommendedBody == "Eve") bodyG = 16.7f;
if (recommendedBody == "Duna") bodyG = 2.94f;
if (recommendedBody == "Ike") bodyG = 1.10f;
if (recommendedBody == "Jool") bodyG = 7.85f;
if (recommendedBody == "Laythe") bodyG = 7.85f;
if (recommendedBody == "Vall") bodyG = 2.31f;
if (recommendedBody == "Bop") bodyG = 0.589f;
if (recommendedBody == "Tylo") bodyG = 7.85f;
if (recommendedBody == "Gilly") bodyG = 0.049f;
if (recommendedBody == "Pol") bodyG = 0.373f;
if (recommendedBody == "Dres") bodyG = 1.13f;
if (recommendedBody == "Eeloo") bodyG = 1.69f;
string moreInfoText;
moreInfoText = "Recommended Body: " + bodyName;
moreInfoText = moreInfoText + "\nMin pressure: " + minAtmoPressure.ToString() + "kPa";
moreInfoText = moreInfoText + "\nMax pressure: " + maxAtmoPressure.ToString() + "kPa";
moreInfoText = moreInfoText + "\nMax lift: " + maxLift.ToString() + "kN";
moreInfoText = moreInfoText + "\nMax payload " + "(" + bodyName + "):\n" + (Mathf.Floor((maxLift / bodyG) * 1000) / 1000).ToString() + "t" + " (at " + maxAtmoPressure + "kPa)";// + " (" + (Mathf.Floor((minLift / bodyG) * 1000) / 1000).ToString() + "t)";
//moreInfoText = moreInfoText + "\n At max pressure: " + (Mathf.Floor((maxLift/bodyG)*1000)/1000).ToString() + "t";
//moreInfoText = moreInfoText + "\n At min pressure: " + (Mathf.Floor((minLift / bodyG) * 1000) / 1000).ToString() + "t";
return moreInfoText;
}
}
}