-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathKM_LaunchEffect_SmkS.cs
498 lines (401 loc) · 17.7 KB
/
KM_LaunchEffect_SmkS.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (c) 2019, Sébastien GAGGINI AKA Sarbian, France
* All rights reserved.
*
* Original work by dtobi, used with his permission
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;
namespace SmokeScreen
{
public class KM_LaunchEffect_SmkS : PartModule
{
[KSPField(isPersistant = false)]
public string model = string.Empty;
[KSPField(isPersistant = false)]
public string parent = string.Empty;
[KSPField(isPersistant = false)]
public Vector3 position = Vector3d.zero;
[KSPField(isPersistant = false)]
public Vector3 rotation = Vector3d.zero;
[KSPField(isPersistant = false)]
public Vector3 scale = Vector3d.one;
[KSPField(isPersistant = false)]
public string effectName = "PreLaunchEffect";
[KSPField(isPersistant = false)]
public string effectLightName = "PreLaunchEffectLight";
[KSPField(isPersistant = false)]
public string transformName = "";
[KSPField(isPersistant = false)]
public bool checkBottomNode = true;
[KSPField(isPersistant = false)]
public bool debug = false;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Show"),
UI_Toggle(disabledText = "Off", enabledText = "Editor")]
public bool editorPlacementOptionsActive = false;
[KSPField(isPersistant = true)]
public bool isRunning = false;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Timer"),
UI_FloatRange(minValue = 0f, maxValue = 20f, stepIncrement = 1f)]
public float runningTime = 0.0f;
private float startTime = 0f;
private bool timerRunning = false;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "With"),
UI_FloatRange(minValue = 0f, maxValue = 2f, stepIncrement = 0.1f)]
public float width = 0f;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Height"),
UI_FloatRange(minValue = 0f, maxValue = 20f, stepIncrement = 1f)]
public float height = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = false, guiName = "offset")]
public Vector3 offset = Vector3d.zero;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "X"),
UI_FloatRange(minValue = -4f, maxValue = 4f, stepIncrement = 0.1f)]
public float xOffset = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Y"),
UI_FloatRange(minValue = -4f, maxValue = 4f, stepIncrement = 0.1f)]
public float yOffset = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Z"),
UI_FloatRange(minValue = -4f, maxValue = 4f, stepIncrement = 0.1f)]
public float zOffset = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Num P"),
UI_FloatRange(minValue = 10, maxValue = 200f, stepIncrement = 20f)]
public float numP = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = false, guiName = "speed")]
public Vector3 speed = Vector3d.zero;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "speedX"),
UI_FloatRange(minValue = -5f, maxValue = 5f, stepIncrement = 0.25f)]
public float speedX = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "speedY"),
UI_FloatRange(minValue = -5f, maxValue = 5f, stepIncrement = 0.25f)]
public float speedY = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "speedZ"),
UI_FloatRange(minValue = -5f, maxValue = 5f, stepIncrement = 0.25f)]
public float speedZ = 0;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Size"),
UI_FloatRange(minValue = -0f, maxValue = 1.5f, stepIncrement = 0.05f)]
public float size = 0f;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Random"),
UI_FloatRange(minValue = -0f, maxValue = 2f, stepIncrement = 0.1f)]
public float rndVelocity = 0f;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "SoftOff"),
UI_Toggle(disabledText = "Disabled", enabledText = "Enabled")]
public bool softDecrease = false;
[KSPField(isPersistant = true, guiActive = false, guiActiveEditor = true, guiName = "Launch Effects"),
UI_Toggle(disabledText = "Disabled", enabledText = "Enabled")]
public bool isActive = true;
protected static Random rnd = new Random();
private int performanceLimiter = rnd.Next();
private int performaceThreshold = 10;
private Dictionary<int, KSPParticleEmitter> effectsList = new Dictionary<int, KSPParticleEmitter>();
private Dictionary<int, Vector3> locationList = new Dictionary<int, Vector3>();
private Dictionary<int, Light> lightList = new Dictionary<int, Light>();
[KSPEvent(guiName = "Toggle", guiActive = true)]
public void toggle()
{
setEffect(!isActive);
}
Vessel.Situations lastSituation;
private GameObject effectModel;
[KSPAction("Toggle")]
public void toggleAG(KSPActionParam param)
{
setEffect(!isActive);
}
public override void OnStart(StartState state)
{
if (checkBottomNode)
{
foreach (var node in this.part.attachNodes)
{
print("Att:" + node.id);
if (node.id == "bottom" && node.attachedPart != null)
{
print("Attached!");
isActive = false;
}
}
}
// Loads the effectModel manually. Avoids the need for a MODEL node
// But may add a effectModel more than once if more than 1 KM_LaunchEffect_SmkS
// module is added with that uses the same effectModel.
if (model != string.Empty)
{
effectModel = GameDatabase.Instance.GetModel(model);
if (effectModel == null)
{
print("Cannot find effectModel " + model);
return;
}
effectModel.SetActive(true);
Transform parentTransform = part.transform.Find(parent);
//print("ParentTransform Null = " + (parentTransform==null));
effectModel.transform.SetParent(parentTransform ?? part.transform);
effectModel.transform.localPosition = position;
//print("position = " + position.ToString("F2"));
effectModel.transform.localScale = scale;
//print("scale = " + scale.ToString("F2"));
effectModel.transform.localRotation = Quaternion.Euler(rotation);
//print("rotation = " + rotation.ToString("F2"));
}
if (xOffset != 0 || yOffset != 0 || zOffset != 0)
offset = new Vector3(xOffset, yOffset, zOffset);
if (speedX != 0 || speedY != 0 || speedZ != 0)
speed = new Vector3(speedX, speedY, speedZ);
registerEffects();
if (state != StartState.Editor && Vessel.Situations.PRELAUNCH == vessel.situation)
{
OnPreLaunch();
}
if (!debug)
{
this.Fields["runningTime"].guiActiveEditor = false;
this.Fields["width"].guiActiveEditor = false;
this.Fields["height"].guiActiveEditor = false;
this.Fields["xOffset"].guiActiveEditor = false;
this.Fields["yOffset"].guiActiveEditor = false;
this.Fields["zOffset"].guiActiveEditor = false;
this.Fields["numP"].guiActiveEditor = false;
this.Fields["speedX"].guiActiveEditor = false;
this.Fields["speedY"].guiActiveEditor = false;
this.Fields["speedZ"].guiActiveEditor = false;
this.Fields["size"].guiActiveEditor = false;
this.Fields["rndVelocity"].guiActiveEditor = false;
this.Fields["softDecrease"].guiActiveEditor = false;
if (Events["toggle"] != null) Events["toggle"].guiActiveEditor = false;
}
}
void registerEffects()
{
var launchEffects = effectModel == null ? this.part.GetComponentsInChildren<KSPParticleEmitter>() : effectModel.GetComponentsInChildren<KSPParticleEmitter>() ;
foreach (KSPParticleEmitter launchEffect in launchEffects)
{
if (launchEffect != null)
{
//print ("DBG Found Effect: " + launchEffect.name);
if (launchEffect.name == effectName)
{
effectsList.Add(launchEffect.GetInstanceID(), launchEffect);
if (transformName == "" || transformName == effectName)
{
//print ("DBG: LP");
locationList.Add(launchEffect.GetInstanceID(), launchEffect.transform.localPosition);
}
else
{
//print ("DBG: MB");
var partTransforms = this.part.GetComponentsInChildren<MonoBehaviour>();
foreach (MonoBehaviour partTransform in partTransforms)
{
if (transformName == partTransform.name)
{
launchEffect.transform.localPosition = partTransform.transform.localPosition;
locationList.Add(launchEffect.GetInstanceID(), partTransform.transform.localPosition);
}
}
}
launchEffect.shape2D = new Vector2(width, width);
if (rndVelocity != 0)
launchEffect.rndVelocity = new Vector3(rndVelocity, rndVelocity, rndVelocity);
if (height != 0)
launchEffect.maxEnergy = height;
if (height != 0)
launchEffect.minEnergy = height / 2;
if (size != 0)
launchEffect.minSize = size;
if (size != 0)
launchEffect.minSize = size / 2;
if (!offset.IsZero())
launchEffect.transform.Translate(offset, Space.Self);
if (!speed.IsZero())
launchEffect.localVelocity = speed;
}
}
}
//print ("DBG: BeforeLights");
var launchEffectsLights = effectModel == null ? this.part.GetComponentsInChildren<Light>() : effectModel.GetComponentsInChildren<Light>();
foreach (Light launchEffectLight in launchEffectsLights)
{
if (launchEffectLight != null)
{
// print ("Found Light: " + launchEffectLight.name);
if (launchEffectLight.name == effectLightName)
{
// print ("Added light");
lightList.Add(launchEffectLight.GetInstanceID(), launchEffectLight);
}
}
}
//print ("DBG: AfterLights");
}
public virtual void OnPreLaunch() { }
public virtual void OnLaunch() { }
public virtual void OnTimer() { }
protected void startTimer()
{
timerRunning = true;
startTime = Time.fixedTime;
}
protected float remainingTime()
{
return !timerRunning ? 0 : startTime + runningTime - Time.fixedTime;
}
protected void checkTimer()
{
if (remainingTime() < 0)
{
startTime = 0;
timerRunning = false;
OnTimer();
}
}
public override void OnUpdate()
{
if (HighLogic.LoadedSceneIsEditor)
{
updateEditor();
return;
}
// we launched
if (lastSituation == Vessel.Situations.PRELAUNCH && vessel.situation != Vessel.Situations.PRELAUNCH)
{
OnLaunch();
}
checkTimer();
lastSituation = vessel.situation;
}
public override string GetInfo()
{
return "KM Launch Effect by dtobi";
}
private void updateEditor()
{
if (performanceLimiter++ % performaceThreshold == 0)
setEffect(editorPlacementOptionsActive);
}
public void setEffect(bool state)
{
foreach (KeyValuePair<int, KSPParticleEmitter> pair in effectsList)
{
KSPParticleEmitter launchEffect = pair.Value;
//print ("Found Effect: " + launchEffect.name+" state"+state);
launchEffect.emit = state;
if (state)
{
if (numP != 0)
launchEffect.maxEmission = (int)(numP);
if (numP != 0)
launchEffect.minEmission = (int)(numP / 2);
}
}
foreach (Light launchEffectLight in lightList.Values)
{
launchEffectLight.intensity = (state ? 1 : 0);
}
}
}
public class KM_PreLaunchEffect_SmkS : KM_LaunchEffect_SmkS
{
public override void OnLaunch()
{
if (runningTime > 0)
{
startTimer();
}
else
{
setEffect(false);
Events["toggle"].guiActive = false;
}
}
public override void OnPreLaunch()
{
if (isActive)
{
setEffect(true);
}
else
{
print("Effect " + effectName + " not active. Not starting.");
}
}
public override void OnTimer()
{
setEffect(false);
//print("Shutting off effects:" + effectName);
}
}
public class KM_PostLaunchEffect_SmkS : KM_LaunchEffect_SmkS
{
private int performanceLimiterUpdate = 0;
private int performanceThreshold = 15;
private int originalNumP = 0;
public override void OnPreLaunch()
{
if (isActive) setEffect(false);
}
public override void OnUpdate()
{
if (HighLogic.LoadedSceneIsEditor)
return;
if (softDecrease && performanceLimiterUpdate++ % performanceThreshold == 0)
{
// we launched
if (vessel.situation != Vessel.Situations.PRELAUNCH)
{
var remT = remainingTime();
if (remT <= 0)
{
numP = 0;
setEffect(false);
softDecrease = false;
}
else
{
numP = originalNumP * remT / runningTime;
setEffect(true);
}
}
}
checkTimer();
base.OnUpdate();
}
public override void OnLaunch()
{
performanceLimiterUpdate = rnd.Next();
originalNumP = (int)numP;
if (isActive) setEffect(true);
startTimer();
Events["toggle"].guiActive = false;
}
public override void OnTimer()
{
setEffect(false);
softDecrease = false;
print("Shutting off effects:" + effectName);
}
}
}