forked from ihsoft/KAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKASModuleGrapplingHook.cs
More file actions
238 lines (210 loc) · 8.22 KB
/
KASModuleGrapplingHook.cs
File metadata and controls
238 lines (210 loc) · 8.22 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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using UnityEngine;
using KSP.IO;
namespace KAS
{
public class KASModuleGrapplingHook : KASModuleAttachCore
{
[KSPField] public float forceNeeded = 5;
[KSPField] public bool attachToPart = true;
[KSPField] public Vector3 rayDir = Vector3.down;
[KSPField] public float rayLenght = 1;
[KSPField] public float partBreakForce = 10;
[KSPField] public float staticBreakForce = 15;
//[KSPField] public float minFwdDot = 0.990f;
//[KSPField] public float minRollDot = float.MinValue;
[KSPField] public float aboveDist = 0f;
//Sounds
[KSPField] public string attachStaticSndPath = "KAS/Sounds/grappleAttachStatic";
[KSPField] public string attachPartSndPath = "KAS/Sounds/grappleAttachPart";
[KSPField] public string attachEvaSndPath = "KAS/Sounds/grappleAttachEva";
[KSPField] public string detachSndPath = "KAS/Sounds/grappleDetach";
public FXGroup fxSndAttachStatic, fxSndAttachPart, fxSndAttachEva, fxSndDetach;
//Info
[KSPField(guiActive = true, guiName = "State", guiFormat="S")] public string state = "Idle";
public override string GetInfo()
{
string info = base.GetInfo();
info += "---- Grappling Hook ----";
info += "\n";
info += "Attach strength (part) : " + partBreakForce;
info += "\n";
info += "Attach strength (ground) : " + staticBreakForce;
info += "\n";
info += "Inpact force : " + forceNeeded;
return info;
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (attachMode.StaticJoint || attachMode.FixedJoint)
{
Events["ContextMenuDetach"].guiActive = true;
Events["ContextMenuDetach"].guiActiveUnfocused = true;
}
else
{
Events["ContextMenuDetach"].guiActive = false;
Events["ContextMenuDetach"].guiActiveUnfocused = false;
}
KAS_Shared.createFXSound(this.part, fxSndAttachStatic, attachStaticSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndAttachPart, attachPartSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndAttachEva, attachEvaSndPath, false);
KAS_Shared.createFXSound(this.part, fxSndDetach, detachSndPath, false);
}
public void OnPartUnpack()
{
this.part.rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
public void OnPartPack()
{
this.part.rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
}
public void OnPartGrab(Vessel kerbalEvaVessel)
{
DetachGrapple();
}
public void OnAttachStatic()
{
DetachGrapple();
AttachStaticGrapple(staticBreakForce);
}
public void OnAttachPart(Part targetPart)
{
KAS_Shared.DebugLog("OnAttachPart(GrapplingHook)");
if (FixedAttach.connectedPart)
{
DetachGrapple();
}
AttachPartGrapple(targetPart, partBreakForce);
}
void OnCollisionEnter(Collision collision)
{
if (!attachMode.StaticJoint && !attachMode.FixedJoint)
{
AttachOnCollision(collision);
}
}
private void AttachOnCollision(Collision collision)
{
//Don't attach if inpact force is too low
if (collision.relativeVelocity.magnitude < forceNeeded) return;
float shorterDist = Mathf.Infinity;
bool nearestHitFound = false;
Part nearestHitPart = null;
RaycastHit nearestHit = new RaycastHit();
Vector3 rayDirection = this.part.transform.TransformDirection(rayDir);
//Get all raycast hits in front of the grapple
List<RaycastHit> nearestHits = new List<RaycastHit>(Physics.RaycastAll(this.part.transform.position, rayDirection, rayLenght, 557059));
foreach (RaycastHit hit in nearestHits)
{
//Exclude grapple collider
if (hit.collider == this.part.collider) continue;
//Exclude parts if needed
if (!attachToPart)
{
if (hit.rigidbody)
{
if (hit.rigidbody.GetComponent<Part>())
{
continue;
}
}
}
/*
// Check forward dot
float fwdDot = Mathf.Abs(Vector3.Dot(hit.normal, this.transform.up));
if (fwdDot <= minFwdDot)
{
continue;
}
// Check roll dot
float rollDot = Vector3.Dot(hit.normal, this.transform.up);
if (rollDot <= minRollDot)
{
continue;
}*/
// Get closest hit
float tmpShorterDist = Vector3.Distance(this.part.transform.position, hit.point);
if (tmpShorterDist <= shorterDist)
{
shorterDist = tmpShorterDist;
nearestHit = hit;
if (nearestHit.rigidbody) nearestHitPart = nearestHit.rigidbody.GetComponent<Part>();
nearestHitFound = true;
}
}
if (!nearestHitFound)
{
KAS_Shared.DebugLog("AttachOnCollision - Nothing to attach in front of grapple");
return;
}
KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(this.part);
if (connectedWinch)
{
MoveAbove(nearestHit.point, nearestHit.normal, aboveDist);
connectedWinch.cableJointLength = connectedWinch.cableRealLenght;
}
if (nearestHitPart)
{
KAS_Shared.DebugLog("AttachOnCollision - grappleAttachOnPart=true");
KAS_Shared.DebugLog("AttachOnCollision - Attaching to part : " + nearestHitPart.partInfo.title);
AttachPartGrapple(nearestHitPart, partBreakForce);
}
else
{
KAS_Shared.DebugLog("AttachOnCollision - Attaching to static : " + nearestHit.collider.name);
AttachStaticGrapple(staticBreakForce);
}
}
public void AttachPartGrapple(Part attachToPart, float breakForce)
{
AttachFixed(attachToPart, partBreakForce);
state = "Attached to : " + attachToPart.partInfo.title;
//Sound
if (attachToPart.vessel.isEVA)
{
fxSndAttachEva.audio.Play();
}
else
{
fxSndAttachPart.audio.Play();
}
}
public void AttachStaticGrapple(float breakForce)
{
AttachStatic(staticBreakForce);
Events["ContextMenuDetach"].guiActive = true;
Events["ContextMenuDetach"].guiActiveUnfocused = true;
state = "Ground attached";
fxSndAttachStatic.audio.Play();
}
public void DetachGrapple()
{
state = "Idle";
Events["ContextMenuDetach"].guiActive = false;
Events["ContextMenuDetach"].guiActiveUnfocused = false;
if (attachMode.StaticJoint || attachMode.FixedJoint)
{
Detach();
fxSndDetach.audio.Play();
}
}
[KSPEvent(name = "ContextMenuDetach", active = true, guiActive = false, guiActiveUnfocused = false, guiName = "Detach")]
public void ContextMenuDetach()
{
DetachGrapple();
}
[KSPAction("Detach")]
public void ActionGroupDetach(KSPActionParam param)
{
if (!this.part.packed)
{
DetachGrapple();
}
}
}
}