forked from sarbian/ModularFlightIntegrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModularVesselPrecalculate.cs
292 lines (244 loc) · 8.85 KB
/
ModularVesselPrecalculate.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
using System;
using UnityEngine;
namespace ModularFI
{
class ModularVesselPrecalculate : VesselPrecalculate
{
private float lastMainPhysics = 0;
public override void Awake()
{
TimingManager.UpdateAdd(TimingManager.TimingStage.Precalc, TimedUpdate);
TimingManager.FixedUpdateAdd(TimingManager.TimingStage.Precalc, TimedFixedUpdate);
base.Awake();
}
public override void OnDestroy()
{
TimingManager.UpdateRemove(TimingManager.TimingStage.Precalc, TimedUpdate);
TimingManager.FixedUpdateRemove(TimingManager.TimingStage.Precalc, TimedFixedUpdate);
base.OnDestroy();
}
public new void FixedUpdate()
{
// Empty on puprose. See ModularFlightIntegrator FixedUpdate comment
}
public void TimedFixedUpdate()
{
if (gameObject.activeInHierarchy && this.enabled)
base.FixedUpdate();
}
public override void Update()
{
// Empty on puprose. See ModularFlightIntegrator FixedUpdate comment
}
/// <summary>
/// Used for loaded/unpacked vessels so display matches physics. Runs most of what FixedUpdate does.
/// However, it does so with zero time offset and does not calc gravity (that happens in next fixed frame).
/// Finally, it signals to next fixed frame it doesn't have to rerun this stuff
/// </summary>
public void TimedUpdate()
{
if (gameObject.activeInHierarchy && this.enabled)
base.Update();
}
private static Action runFirstOverride;
public static bool RegisterMainPhysicsOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (runFirstOverride == null)
{
runFirstOverride = act;
return true;
}
print("RunFirst already has an override");
return false;
}
/// <summary>
/// Run when a vessel initializes in flight
/// </summary>
public override void RunFirst()
{
if (runFirstOverride != null)
runFirstOverride();
else
base.RunFirst();
}
private static Action<bool> mainPhysicsOverride;
public static bool RegisterMainPhysicsOverride(Action<bool> act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (mainPhysicsOverride == null)
{
mainPhysicsOverride = act;
return true;
}
print("MainPhysics already has an override");
return false;
}
/// <summary>
/// Does the main physics calls. Run from fixed update. Bits run in Update.
/// </summary>
/// <param name="doKillChecks">Do we check if the vessel should be killed?</param>
public override void MainPhysics(bool doKillChecks)
{
// Prevents ruuning twice in the the same frame. Needed by Principia
if (lastMainPhysics == Time.fixedTime)
return;
if (mainPhysicsOverride != null)
mainPhysicsOverride(doKillChecks);
else
base.MainPhysics(doKillChecks);
lastMainPhysics = Time.fixedTime;
}
private static Action applyVelocityCorrectionOverride;
public static bool RegisterApplyVelocityCorrectionOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (applyVelocityCorrectionOverride == null)
{
applyVelocityCorrectionOverride = act;
return true;
}
print("ApplyVelocityCorrection already has an override");
return false;
}
/// <summary>
/// Applies the velocity correction from drift compensation (if any).
/// </summary>
public override void ApplyVelocityCorrection()
{
if (applyVelocityCorrectionOverride != null)
applyVelocityCorrectionOverride();
else
base.ApplyVelocityCorrection();
}
private static Action goOnRailsOverride;
public static bool RegisterGoOnRailsOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (goOnRailsOverride == null)
{
goOnRailsOverride = act;
return true;
}
print("GoOnRails already has an override");
return false;
}
/// <summary>
/// Called by Vessel.GoOnRails. Clears any post-integration velocity offset.
/// </summary>
public override void GoOnRails()
{
if (goOnRailsOverride != null)
goOnRailsOverride();
else
base.GoOnRails();
}
private static Action goOffRailsOverride;
public static bool RegisterGoOffRailsOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (goOffRailsOverride == null)
{
goOffRailsOverride = act;
return true;
}
print("GoOffRails already has an override");
return false;
}
/// <summary>
/// Called by Vessel.GoOffRails
/// </summary>
public override void GoOffRails()
{
if (goOffRailsOverride != null)
goOffRailsOverride();
else
base.GoOffRails();
}
//protected override void StartEasing()
//{
// base.StartEasing();
//}
//
//protected override void StopEasing()
//{
// base.StopEasing();
//}
private static Action calculateGravityOverride;
public static bool RegisterCalculateGravityOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (calculateGravityOverride == null)
{
calculateGravityOverride = act;
return true;
}
print("CalculateGravity already has an override");
return false;
}
/// <summary>
/// Calculate the force of gravity, using drift compensation if in orbit and the compensation is better than not.
/// Ease gravity coming off rails while landed/splashed.
/// </summary>
public override void CalculateGravity()
{
if (calculateGravityOverride != null)
calculateGravityOverride();
else
base.CalculateGravity();
}
///// <summary>
///// This will update pos/rot if landed/splashed and on rails
///// </summary>
//public override void SetLandedPosRot()
//{
// base.SetLandedPosRot();
//}
private static Action calculatePhysicsStatsOverride;
public static bool RegisterCalculatePhysicsStatsOverride(Action act)
{
if (HighLogic.LoadedScene != GameScenes.SPACECENTER)
{
print("You can only register on the SPACECENTER scene");
}
if (calculatePhysicsStatsOverride == null)
{
calculatePhysicsStatsOverride = act;
return true;
}
print("CalculatePhysicsStats already has an override");
return false;
}
/// <summary>
/// Will calculate center of mass and total mass, mass-weighted average linear and angular velocity, moment of inertia, and angular momentum
/// Depends on FlightIntegrator setting part.physicsMass.
/// If vessel is unloaded, use traditional calculations based off stored values and root part.
/// </summary>
public override void CalculatePhysicsStats()
{
if (calculatePhysicsStatsOverride != null)
calculatePhysicsStatsOverride();
else
base.CalculatePhysicsStats();
}
//public override bool isEasingGravity { get; set; }
}
}