-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHovercraft.cs
302 lines (268 loc) · 11.8 KB
/
Hovercraft.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using UnityEngine;
public class Hovercraft : PartModule
{
[KSPField]
public float hoverForce = 0F;
public AnimationState[] deployStates;
[KSPField(isPersistant = false)]
public bool hasDeployAnimation = true;
[KSPField(isPersistant = false)]
public string deployAnimName = "move";
[KSPField]
public float rearoffset = 0.0F;
[KSPField]
public float fuelefficiency = 0.002F;
[KSPField]
public float liftCap = 20f;
public float upwardSpeedrl = 0f;
public float upwardSpeedl = 0f;
public float upwardSpeedrr = 0f;
public float upwardSpeedr = 0f;
float rightrcrelative = 1;
float leftrcrelative = 1;
float rearrightrcrelative = 1;
float rearleftrcrelative = 1;
Vector3 rightrcrelativePosition = Vector3.forward;
Vector3 leftrcrelativePosition = Vector3.forward;
Vector3 rearrightrcrelativePosition = Vector3.forward;
Vector3 rearleftrcrelativePosition = Vector3.forward;
Transform CoMTransform = new GameObject().transform;
//[KSPField(guiName = "hoverdamp", guiActive = true)]
//private string damp = "None";
[KSPField(guiActive = false, guiActiveEditor = true, guiName = "Hover Height", isPersistant = true), UI_FloatRange(minValue = 0f, maxValue = 5f, stepIncrement = 0.1f)]
public float hoverHeight = 2.5f;
[KSPField(guiActive = true, guiActiveEditor = true, guiName = "Max Lift", isPersistant = true), UI_FloatRange(minValue = 0f, maxValue = 20f, stepIncrement = 0.5f)]
public float maxlift = 0f;
[KSPField(guiActive = true, guiActiveEditor = true, guiName = "DAMP", isPersistant = true), UI_FloatRange(minValue = 0f, maxValue = 2f, stepIncrement = 0.1f)]
public float hoverDamp = 1f;
protected Transform rightrcTransform = null;
protected Transform leftrcTransform = null;
protected Transform rearrightrcTransform = null;
protected Transform rearleftrcTransform = null;
List<KSPParticleEmitter> HoverEmitters;
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
{
if (state != StartState.Editor && state != StartState.None)
{
this.enabled = true;
this.part.force_activate();
}
else
{
this.enabled = false;
}
if (base.part.FindModelTransform("rightrc") != null)
{
this.rightrcTransform = base.part.FindModelTransform("rightrc");
}
if (base.part.FindModelTransform("leftrc") != null)
{
this.leftrcTransform = base.part.FindModelTransform("leftrc");
}
if (base.part.FindModelTransform("rearrightrc") != null)
{
this.rearrightrcTransform = base.part.FindModelTransform("rearrightrc");
}
if (base.part.FindModelTransform("rearleftrc") != null)
{
this.rearleftrcTransform = base.part.FindModelTransform("rearleftrc");
}
if (deployAnimName != "")
{
deployStates = SetUpAnimation(deployAnimName, this.part);
}
if (hasDeployAnimation)
{
deployStates = SetUpAnimation(deployAnimName, this.part);
foreach (AnimationState anim in deployStates)
{
anim.enabled = false;
}
}
foreach (var emitter in part.FindModelComponents<KSPParticleEmitter>())
{
emitter.emit = false;
EffectBehaviour.RemoveParticleEmitter(emitter);
}
HoverEmitters = new List<KSPParticleEmitter>();
foreach (Transform htf in part.FindModelTransforms("HoverEmi"))
{
KSPParticleEmitter kpe = htf.GetComponent<KSPParticleEmitter>();
HoverEmitters.Add(kpe);
kpe.emit = false;
}
}
//CoMTransform.position = vessel.CoM;
//CoMTransform.rotation = vessel.transform.rotation;
//rightrcrelativePosition = CoMTransform.InverseTransformPoint(rightrcTransform.position);
//leftrcrelativePosition = CoMTransform.InverseTransformPoint(leftrcTransform.position);
//rearrightrcrelativePosition = CoMTransform.InverseTransformPoint(rearrightrcTransform.position);
//rearleftrcrelativePosition = CoMTransform.InverseTransformPoint(rearleftrcTransform.position);
//rightrcrelative = 1 / System.Math.Abs(rightrcrelativePosition.y);
//leftrcrelative = 1 / System.Math.Abs(leftrcrelativePosition.y);
//rearrightrcrelative = 1 / System.Math.Abs(rearrightrcrelativePosition.y);
//rearleftrcrelative = 1 / System.Math.Abs(rearleftrcrelativePosition.y);
}
public override void OnFixedUpdate()
{
if (!HighLogic.LoadedSceneIsFlight) return;
Vector3 gee = FlightGlobals.getGeeForceAtPosition(transform.position);
hoverForce = vessel.GetTotalMass() * gee.magnitude;
if (base.part.FindModelTransform("rightrc") != null&&!rightrcTransform)
{
this.rightrcTransform = base.part.FindModelTransform("rightrc");
}
if (base.part.FindModelTransform("leftrc") != null&&!leftrcTransform)
{
this.leftrcTransform = base.part.FindModelTransform("leftrc");
}
if (base.part.FindModelTransform("rearrightrc") != null&&!rearrightrcTransform)
{
this.rearrightrcTransform = base.part.FindModelTransform("rearrightrc");
}
if (base.part.FindModelTransform("rearleftrc") != null&&!rearleftrcTransform)
{
this.rearleftrcTransform = base.part.FindModelTransform("rearleftrc");
}
if (hoverHeight != 0 && maxlift != 0)
{
if (hasDeployAnimation)
{
foreach (AnimationState anim in deployStates)
{
//animation clamping
if (anim.normalizedTime > 1)
{
anim.speed = 0;
anim.normalizedTime = 1;
}
if (anim.normalizedTime < 0)
{
anim.speed = 0;
anim.normalizedTime = 0;
}
//deploying
if (hoverHeight != 0 && maxlift != 0)
{
anim.enabled = true;
if (anim.normalizedTime < 1 && anim.speed < 1)
{
anim.speed = 1;
}
if (anim.normalizedTime == 1)
{
anim.enabled = false;
}
}
}
}
upwardSpeedr = DoHoverForce(rightrcTransform, hoverHeight, rearoffset, hoverDamp, hoverForce, rightrcrelative, maxlift, upwardSpeedr, this.part);
upwardSpeedl = DoHoverForce(leftrcTransform, hoverHeight, rearoffset, hoverDamp, hoverForce, leftrcrelative, maxlift, upwardSpeedl, this.part);
upwardSpeedrr = DoHoverForce(rearrightrcTransform, hoverHeight, rearoffset, hoverDamp, hoverForce, rearrightrcrelative, maxlift, upwardSpeedrr, this.part);
upwardSpeedrl = DoHoverForce(rearleftrcTransform, hoverHeight, rearoffset, hoverDamp, hoverForce, rearleftrcrelative, maxlift, upwardSpeedrl, this.part);
}
else
{
upwardSpeedr = 0;
upwardSpeedl = 0;
upwardSpeedrr = 0;
upwardSpeedrl = 0;
}
foreach (var pEmitter in HoverEmitters)
{
pEmitter.maxEmission = (int)(10f*(upwardSpeedr + upwardSpeedl + upwardSpeedrr + upwardSpeedrl));
pEmitter.minEmission = (int)(10f * (upwardSpeedr + upwardSpeedl + upwardSpeedrr + upwardSpeedrl));
pEmitter.Emit();
//if (!pEmitter.useWorldSpace)
//{
// if (pEmitter.maxEnergy < 0.5f)
// {
// float twoFrameTime = Mathf.Clamp(Time.deltaTime * 2f, 0.02f, 0.499f);
// pEmitter.maxEnergy = twoFrameTime;
// pEmitter.minEnergy = twoFrameTime / 3f;
// }
// pEmitter.Emit();
//}
}
if (hoverHeight == 0 || maxlift == 0)
{
if (hasDeployAnimation)
{
foreach (AnimationState anim in deployStates)
{
//animation clamping
if (anim.normalizedTime > 1)
{
anim.speed = 0;
anim.normalizedTime = 1;
}
if (anim.normalizedTime < 0)
{
anim.speed = 0;
anim.normalizedTime = 0;
}
anim.enabled = true;
if (anim.normalizedTime > 0 && anim.speed > -1)
{
anim.speed = -1;
}
if (anim.normalizedTime == 0)
{
anim.enabled = false;
}
}
}
}
}
static float DoHoverForce(Transform rcTransform, float hoverHeight, float offset, float hoverDamp, float hoverForce, float rcrelative, float maxlift, float upwardSpeed, Part part)
{
RaycastHit hit;
Ray rcray = new Ray(rcTransform.position, rcTransform.forward);
float height = 0.0f;
bool Ray = Physics.Raycast(rcray, out hit, hoverHeight, 557057);
if (FlightGlobals.ActiveVessel.mainBody.ocean) //if mainbody has ocean we land on water before the seabed
{
height = FlightGlobals.getAltitudeAtPos(rcTransform.position);
}
if (Ray || height < hoverHeight)
{
float hoverError = 0.0f;
if (Ray) hoverError = hoverHeight - hit.distance;
if (!Ray || height < hit.distance) hoverError = hoverHeight - height;
hoverError += offset;
upwardSpeed -= hoverError;
float lift = (hoverError - upwardSpeed * hoverDamp / Time.fixedDeltaTime) * hoverForce * rcrelative;
if (lift > maxlift * hoverForce) lift = maxlift * hoverForce;
upwardSpeed = hoverError;
if (hoverError >= 0.0f) part.AddForceAtPosition(-lift * rcTransform.forward, rcTransform.position);
//Debug.Log(hoverError);
//Debug.Log(lift);
}
else
{
upwardSpeed = 0f;
}
return upwardSpeed;
}
public static AnimationState[] SetUpAnimation(string animationName, Part part) //Thanks Majiir!(From BDA)
{
var states = new List<AnimationState>();
foreach (var animation in part.FindModelAnimators(animationName))
{
var animationState = animation[animationName];
animationState.speed = 0;
animationState.enabled = true;
animationState.wrapMode = WrapMode.ClampForever;
animation.Blend(animationName);
states.Add(animationState);
}
return states.ToArray();
}
}