forked from ihsoft/KAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKASModuleMagnet.cs
More file actions
292 lines (260 loc) · 9.33 KB
/
KASModuleMagnet.cs
File metadata and controls
292 lines (260 loc) · 9.33 KB
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 System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace KAS
{
public class KASModuleMagnet : KASModuleAttachCore
{
[KSPField] public float powerDrain = 1f;
[KSPField] public float breakForce = 10;
[KSPField] public float minFwdDot = 0.998f;
[KSPField] public float minRollDot = float.MinValue;
[KSPField] public bool attachToEva = false;
//Sounds
[KSPField] public string attachSndPath = "KAS/Sounds/magnetAttach";
[KSPField] public string detachSndPath = "KAS/Sounds/magnetDetach";
[KSPField] public string magnetSndPath = "KAS/Sounds/magnet";
[KSPField] public string magnetStartSndPath = "KAS/Sounds/magnetstart";
[KSPField] public string magnetStopSndPath = "KAS/Sounds/magnetstop";
public FXGroup fxSndAttach, fxSndDetach, fxSndMagnet, fxSndMagnetStart, fxSndMagnetStop;
[KSPField(guiActive = true, guiName = "State", guiFormat="S")] public string state = "Off";
private bool _magnetActive = false;
public bool MagnetActive
{
get
{
return _magnetActive;
}
set
{
if (value == true && _magnetActive == false)
{
KAS_Shared.DebugLog("MagnetActive(Magnet) Start magnet...");
state = "On";
if (!fxSndMagnetStart.audio.isPlaying)
{
fxSndMagnetStart.audio.Play();
}
if (!fxSndMagnet.audio.isPlaying)
{
fxSndMagnet.audio.Play();
}
_magnetActive = true;
}
else if (value == false && _magnetActive == true)
{
KAS_Shared.DebugLog("MagnetActive(Magnet) Stop magnet...");
state = "Off";
DetachMagnet();
_magnetActive = false;
}
}
}
public override string GetInfo()
{
string info = base.GetInfo();
info += "---- Magnet ----";
info += "\n";
info += "Break Force : " + breakForce;
info += "\n";
info += "Power consumption : " + powerDrain + "/s";
return info;
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (state == StartState.Editor || state == StartState.None) return;
KAS_Shared.createFXSound(this.part, fxSndAttach, attachSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndDetach, detachSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndMagnet, magnetSndPath, true);
KAS_Shared.createFXSound(this.part, fxSndMagnetStart, magnetStartSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndMagnetStop, magnetStopSndPath, false);
if (attachMode.FixedJoint)
{
MagnetActive = true;
}
}
public override void OnUpdate()
{
base.OnUpdate();
if (!HighLogic.LoadedSceneIsFlight) return;
UpdateMagnet();
}
void OnJointBreak(float breakForce)
{
KAS_Shared.DebugWarning("A joint broken on " + part.partInfo.title + " !, force: " + breakForce);
KAS_Shared.DebugWarning("Disable magnet...");
MagnetActive = false;
}
public void OnPartGrab(Vessel kerbalEvaVessel)
{
MagnetActive = false;
Events["ContextMenuMagnet"].guiActive = false;
}
public void OnPartDrop()
{
MagnetActive = false;
Events["ContextMenuMagnet"].guiActive = true;
}
public void OnAttachPart(Part targetPart)
{
KAS_Shared.DebugLog("OnAttachPart(magnet) - Attach magnet to : " + targetPart.partInfo.title);
if (FixedAttach.connectedPart)
{
MagnetActive = false;
}
AttachMagnet(targetPart);
}
void OnCollisionEnter(Collision collision)
{
if (MagnetActive)
{
AttachOnCollision(collision, "enter");
}
}
void OnCollisionStay(Collision collisionInfo)
{
if (MagnetActive)
{
AttachOnCollision(collisionInfo, "stay");
}
}
void UpdateMagnet()
{
if (!MagnetActive) return;
//Drain power and stop if no energy
if (!KAS_Shared.RequestPower(this.part, powerDrain))
{
state = "Insufficient Power";
if (this.part.vessel == FlightGlobals.ActiveVessel)
{
ScreenMessages.PostScreenMessage("Magnet stopped ! Insufficient Power", 5, ScreenMessageStyle.UPPER_CENTER);
}
MagnetActive = false;
}
}
private void AttachOnCollision(Collision collision, string type)
{
foreach (ContactPoint contact in collision.contacts)
{
// Check if if the collider have a rigidbody
if (!contact.otherCollider.attachedRigidbody)
{
continue;
}
// Check if it's a part
Part p = contact.otherCollider.attachedRigidbody.GetComponent<Part>();
if (!p)
{
continue;
}
// Check if not already attached
if (FixedAttach.connectedPart)
{
continue;
}
// Check if it's not itself
if (this.part == p)
{
continue;
}
//Attach eva if set in part.cfg
if (!attachToEva)
{
if (p.vessel.isEVA)
{
continue;
}
}
// Check forward dot
float fwdDot = Mathf.Abs(Vector3.Dot(contact.normal, this.transform.up));
if (fwdDot <= minFwdDot)
{
continue;
}
// Check roll dot
float rollDot = Vector3.Dot(contact.normal, this.transform.up);
if (rollDot <= minRollDot)
{
continue;
}
// Info
KAS_Shared.DebugLog("forward dot is : " + fwdDot + " and lockMinFwdDot is set to : " + minFwdDot);
KAS_Shared.DebugLog("roll dot is : " + rollDot + " and lockMinRollDot is set to : " + minRollDot);
KAS_Shared.DebugLog("AttachOnCollision - Collision with unattached part detected : " + p.name + " | at : " + contact.point);
KAS_Shared.DebugLog("AttachOnCollision - Attach to surface the object");
// attach
AttachMagnet(p);
// sound
if (FixedAttach.connectedPart)
{
fxSndAttach.audio.Play();
}
}
}
private void AttachMagnet(Part partToAttach)
{
if (KAS_Shared.RequestPower(this.part, powerDrain))
{
//Disable all collisions
this.part.collider.isTrigger = true;
// Create joint
AttachFixed(partToAttach, breakForce);
// Set reference
MagnetActive = true;
}
else
{
ScreenMessages.PostScreenMessage("Magnet not powered !", 5, ScreenMessageStyle.UPPER_CENTER);
}
}
private void DetachMagnet()
{
this.part.collider.isTrigger = false;
if (FixedAttach.connectedPart)
{
fxSndDetach.audio.Play();
fxSndMagnetStop.audio.Play();
}
Detach();
fxSndMagnet.audio.Stop();
}
[KSPEvent(name = "ContextMenuMagnet", active = true, guiActive = true, guiActiveUnfocused = false, guiName = "Magnet On/Off")]
public void ContextMenuMagnet()
{
MagnetActive = !MagnetActive;
}
[KSPAction("Toogle Magnet")]
public void ActionGroupMagnetToggle(KSPActionParam param)
{
if (!this.part.packed)
{
if (param.type == KSPActionType.Activate)
{
MagnetActive = true;
}
if (param.type == KSPActionType.Deactivate)
{
MagnetActive = false;
}
}
}
[KSPAction("Magnet On")]
public void ActionGroupMagnetOn(KSPActionParam param)
{
if (!this.part.packed)
{
MagnetActive = true;
}
}
[KSPAction("Magnet Off")]
public void ActionGroupMagnetOff(KSPActionParam param)
{
if (!this.part.packed)
{
MagnetActive = false;
}
}
}
}