forked from ihsoft/KAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKASAddonPointer.cs
More file actions
401 lines (364 loc) · 16.6 KB
/
KASAddonPointer.cs
File metadata and controls
401 lines (364 loc) · 16.6 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
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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using UnityEngine;
namespace KAS
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class KASAddonPointer : MonoBehaviour
{
public static string bipWrongSndPath = "KAS/Sounds/bipwrong";
public GameObject audioGo = new GameObject();
public AudioSource audioBipWrong = new AudioSource();
public enum PointerMode
{
MoveAndAttach = 0,
CopyAndAttach = 1,
}
public static GameObject soundGo;
// Pointer parameters
private static bool allowPart = false;
private static bool allowEva = false;
private static bool allowStatic = false;
private static PointerMode pointerMode;
private static Part partToAttach;
private static float maxDist = 2f;
private static Transform sourceTransform;
private static bool msgOnly = false;
private static bool running = false;
private static GameObject pointer;
private static List<MeshRenderer> allModelMr;
private static Vector3 customRot = new Vector3(0f, 0f, 0f);
private static Transform pointerNodeTransform;
public static bool isRunning
{
get { return running; }
}
void Awake()
{
audioBipWrong = audioGo.AddComponent<AudioSource>();
audioBipWrong.volume = GameSettings.UI_VOLUME;
audioBipWrong.panLevel = 0; //set as 2D audiosource
if (GameDatabase.Instance.ExistsAudioClip(bipWrongSndPath))
{
audioBipWrong.clip = GameDatabase.Instance.GetAudioClip(bipWrongSndPath);
}
else
{
KAS_Shared.DebugError("Awake(AttachPointer) Bip wrong sound not found in the game database !");
ScreenMessages.PostScreenMessage("Sound file : " + bipWrongSndPath + " as not been found, please check your KAS installation !", 10, ScreenMessageStyle.UPPER_CENTER);
}
}
public static void StartPointer(Part partToMoveAndAttach, PointerMode mode, bool partIsValid, bool evaIsValid, bool staticIsValid, float maxDistance, Transform from, bool sendMsgOnly)
{
if (!running)
{
KAS_Shared.DebugLog("StartPointer(pointer)");
customRot = Vector3.zero;
allowPart = partIsValid;
allowEva = evaIsValid;
allowStatic = staticIsValid;
msgOnly = sendMsgOnly;
partToAttach = partToMoveAndAttach;
maxDist = maxDistance;
pointerMode = mode;
sourceTransform = from;
ShowAttachHelpMsg();
running = true;
}
}
public static void StopPointer()
{
KAS_Shared.DebugLog("StopPointer(pointer)");
running = false;
}
void Update()
{
UpdatePointer();
}
public void UpdatePointer()
{
if (!running)
{
if (pointer) UnityEngine.Object.Destroy(pointer);
return;
}
//Cast ray
Ray ray = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (!Physics.Raycast(ray, out hit, 500, 557059))
{
if (pointer) UnityEngine.Object.Destroy(pointer);
return;
}
//Create pointer if needed
if (!pointer)
{
GameObject modelGo = partToAttach.FindModelTransform("model").gameObject;
pointer = Mesh.Instantiate(modelGo) as GameObject;
foreach (Collider col in pointer.GetComponentsInChildren<Collider>())
{
UnityEngine.Object.Destroy(col);
}
allModelMr = new List<MeshRenderer>();
// Remove attached tube mesh renderer if any
List<MeshRenderer> tmpAllModelMr = new List<MeshRenderer>(pointer.GetComponentsInChildren<MeshRenderer>() as MeshRenderer[]);
foreach (MeshRenderer mr in tmpAllModelMr)
{
if (mr.name == "KAStube" || mr.name == "KASsrcSphere" || mr.name == "KASsrcTube" || mr.name == "KAStgtSphere" || mr.name == "KAStgtTube")
{
Destroy(mr);
continue;
}
allModelMr.Add(mr);
mr.material = new Material(Shader.Find("Transparent/Diffuse"));
}
pointerNodeTransform = new GameObject("KASPointerPartNode").transform;
pointerNodeTransform.parent = pointer.transform;
pointerNodeTransform.localPosition = partToAttach.srfAttachNode.position;
pointerNodeTransform.rotation = KAS_Shared.DirectionToQuaternion(pointer.transform, partToAttach.srfAttachNode.orientation);
}
//Set default color
Color color = Color.green;
// Check if object is valid
bool isValidObj = false;
Part hitPart = null;
KerbalEVA hitEva = null;
if (hit.rigidbody)
{
hitPart = hit.rigidbody.GetComponent<Part>();
hitEva = hit.rigidbody.GetComponent<KerbalEVA>();
if (hitPart && allowPart && !hitEva & hitPart != partToAttach) isValidObj = true;
if (hitEva && allowEva) isValidObj = true;
}
if (!hitPart && !hitEva && allowStatic) isValidObj = true;
//Check distance
bool isValidDist = false;
if (sourceTransform)
{
float distToPointer = Vector3.Distance(sourceTransform.position, hit.point);
if (distToPointer > maxDist)
{
isValidDist = false;
}
else
{
isValidDist = true;
}
}
else
{
isValidDist = true;
}
//Set color
if (!isValidObj) color = Color.red;
if (!isValidDist) color = Color.yellow;
color.a = 0.5f;
foreach (MeshRenderer mr in allModelMr) mr.material.color = color;
//Rotation keys
if (Input.GetKeyDown(KASAddonControlKey.rotateLeftKey.ToLower()))
{
RotatePointer(-15);
}
if (Input.GetKeyDown(KASAddonControlKey.rotateRightKey.ToLower()))
{
RotatePointer(+15);
}
KAS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit);
pointer.transform.rotation *= Quaternion.Euler(customRot);
//Attach on click
if (Input.GetKeyDown(KeyCode.Mouse0))
{
KAS_Shared.DebugLog("Attachment started...");
if (!isValidObj)
{
ScreenMessages.PostScreenMessage("Can't attach, target is not allowed !");
audioBipWrong.Play();
return;
}
if (!isValidDist)
{
ScreenMessages.PostScreenMessage("Can't attach, too far from target !");
audioBipWrong.Play();
return;
}
KASModuleGrab modulegrab = partToAttach.GetComponent<KASModuleGrab>();
//Move and attach mode
if (pointerMode == PointerMode.MoveAndAttach)
{
// Drop and detach part if needed
if (modulegrab)
{
if (modulegrab.grabbed) modulegrab.Drop();
modulegrab.Detach();
}
KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(partToAttach);
if (!connectedWinch)
{
KAS_Shared.DecoupleFromAll(partToAttach);
}
//Move part
partToAttach.transform.position = pointer.transform.position;
partToAttach.transform.rotation = pointer.transform.rotation;
if (connectedWinch)
{
//Set cable lenght to real lenght
connectedWinch.cableJointLength = connectedWinch.cableRealLenght;
}
if (msgOnly)
{
KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach using send message");
if (hitPart) partToAttach.SendMessage("OnAttachPart", hitPart, SendMessageOptions.DontRequireReceiver);
else partToAttach.SendMessage("OnAttachStatic", SendMessageOptions.DontRequireReceiver);
}
else
{
KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach with couple or static method");
if (!hitPart && !hitEva)
{
if (modulegrab)
{
modulegrab.AttachStatic();
modulegrab.fxSndAttachStatic.audio.Play();
}
else
{
KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static");
}
}
else
{
partToAttach.Couple(hitPart);
if (modulegrab)
{
modulegrab.fxSndAttachPart.audio.Play();
}
else
{
KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound");
}
}
partToAttach.SendMessage("OnAttach", SendMessageOptions.DontRequireReceiver);
}
}
if (pointerMode == PointerMode.CopyAndAttach)
{
// Not tested !
Part newPart = KAS_Shared.CreatePart(partToAttach.partInfo, pointer.transform.position, pointer.transform.rotation, partToAttach);
if (msgOnly)
{
if (hitPart) StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
else StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation));
}
else
{
if (!hitPart && !hitEva)
{
StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation, hitPart));
}
else
{
StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation));
}
}
}
running = false;
}
}
private IEnumerator WaitAndAttach(Part partToAttach, Vector3 position, Quaternion rotation, Part toPart = null)
{
while (!partToAttach.rigidbody)
{
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting rigidbody to initialize...");
yield return new WaitForFixedUpdate();
}
KASModuleGrab moduleGrab = partToAttach.GetComponent<KASModuleGrab>();
if (toPart)
{
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Rigidbody initialized, setting velocity...");
partToAttach.rigidbody.velocity = toPart.rigidbody.velocity;
partToAttach.rigidbody.angularVelocity = toPart.rigidbody.angularVelocity;
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting velocity to apply by waiting 0.1 seconds...");
yield return new WaitForSeconds(0.1f);
partToAttach.transform.position = position;
partToAttach.transform.rotation = rotation;
partToAttach.Couple(toPart);
if (moduleGrab)
{
moduleGrab.fxSndAttachPart.audio.Play();
}
else
{
KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound");
}
}
else
{
partToAttach.transform.position = position;
partToAttach.transform.rotation = rotation;
if (moduleGrab)
{
moduleGrab.AttachStatic();
moduleGrab.fxSndAttachStatic.audio.Play();
}
else
{
KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static");
}
}
}
private IEnumerator WaitAndSendMsg(Part partToAttach, Vector3 position, Quaternion rotation, Part toPart = null)
{
while (!partToAttach.rigidbody)
{
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting rigidbody to initialize...");
yield return new WaitForFixedUpdate();
}
if (toPart)
{
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Rigidbody initialized, setting velocity...");
partToAttach.rigidbody.velocity = toPart.rigidbody.velocity;
partToAttach.rigidbody.angularVelocity = toPart.rigidbody.angularVelocity;
KAS_Shared.DebugLog("WaitAndAttach(Pointer) - Waiting velocity to apply by waiting 0.1 seconds...");
yield return new WaitForSeconds(0.1f);
partToAttach.transform.position = position;
partToAttach.transform.rotation = rotation;
partToAttach.SendMessage("OnAttachPart", toPart, SendMessageOptions.DontRequireReceiver);
}
else
{
partToAttach.transform.position = position;
partToAttach.transform.rotation = rotation;
partToAttach.SendMessage("OnAttachStatic", SendMessageOptions.DontRequireReceiver);
}
}
private static void RotatePointer(float dist)
{
//left (-1.0, 0.0, 0.0) | right (1.0, 0.0, 0.0)
if (Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.left || Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.right)
{
customRot.Set(customRot.x + dist, customRot.y, customRot.z);
}
//down (0.0, -1.0, 0.0) | up (0.0, 1.0, 0.0)
if (Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.down || Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.up)
{
customRot.Set(customRot.x, customRot.y + dist, customRot.z);
}
//back (0.0, 0.0, -1.0) | forward (0.0, 0.0, 1.0)
if (Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.back || Vector3.Normalize(partToAttach.srfAttachNode.orientation) == Vector3.forward)
{
customRot.Set(customRot.x, customRot.y, customRot.z + dist);
}
//battery orientation, illuminator (0.0, 0.0, -1.0) orient nok rotate nok
//radial cport/pipe/strut/round rcs orientation (0.0, -1.3, 0.0) orient ok rotate ok
//telus bay / rover orientation (1.0, 0.0, 0.0) orient ok rotate nok
// rcs block orientation (0.1, 0.0, 0.0) orient ok rotate nok
}
public static void ShowAttachHelpMsg()
{
ScreenMessages.PostScreenMessage("Attach pointer enabled. Press " + KASAddonControlKey.rotateLeftKey + "/" + KASAddonControlKey.rotateRightKey + " to rotate and mouse click to attach. Press echap, space, mouse2 or " + KASAddonControlKey.attachKey + " to cancel.", 5, ScreenMessageStyle.UPPER_CENTER);
}
}
}