forked from ihsoft/KAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKASModulePartBay.cs
More file actions
168 lines (151 loc) · 6.66 KB
/
KASModulePartBay.cs
File metadata and controls
168 lines (151 loc) · 6.66 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
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace KAS
{
public class KASModulePartBay : PartModule
{
[KSPField] public string sndStorePath = "KAS/Sounds/hookBayStore";
[KSPField] public bool allowRelease = true;
public Dictionary<AttachNode, List<string>> bays = new Dictionary<AttachNode, List<string>>();
public FXGroup fxSndStore;
public override string GetInfo()
{
string info = base.GetInfo();
info += "---- Part bay ----";
info += "\n";
info += "Allow release : " + allowRelease;
return info;
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (state != StartState.None)
{
if (allowRelease)
{
Actions["ActionGroupRelease"].active = true;
Events["ContextMenuRelease"].guiActive = true;
Events["ContextMenuRelease"].guiActiveUnfocused = true;
}
else
{
Actions["ActionGroupRelease"].active = false;
Events["ContextMenuRelease"].guiActive = false;
Events["ContextMenuRelease"].guiActiveUnfocused = false;
}
}
if (state == StartState.Editor || state == StartState.None) return;
KAS_Shared.createFXSound(this.part, fxSndStore, sndStorePath, false);
LoadBays();
}
private void LoadBays()
{
bays.Clear();
ConfigNode node = KAS_Shared.GetBaseConfigNode(this);
foreach (ConfigNode bayNode in node.GetNodes("BAY"))
{
if (bayNode.HasValue("attachNode") && bayNode.HasValue("type"))
{
string attachNodeName = bayNode.GetValue("attachNode");
AttachNode an = this.part.findAttachNode(attachNodeName);
if (an == null)
{
KAS_Shared.DebugError("LoadBays(PartBay) - Node : " + attachNodeName + " not found !");
continue;
}
List<string> allTypes = new List<string>(bayNode.GetValues("type"));
KAS_Shared.AddNodeTransform(this.part, an);
bays.Add(an, allTypes);
}
}
}
[KSPEvent(name = "ContextMenuStore", active = true, guiActive = false, guiActiveUnfocused = true, guiName = "Store")]
public void ContextMenuStore()
{
KASModuleGrab moduleEvaGrab = KAS_Shared.GetGrabbedPartModule(FlightGlobals.ActiveVessel);
if (!moduleEvaGrab)
{
ScreenMessages.PostScreenMessage("You need to grab a part before", 2, ScreenMessageStyle.UPPER_CENTER);
KAS_Shared.DebugWarning("ContextMenuStore - GetGrabbedPartModule return null !");
return;
}
// Select the nearest compatible bay
float shorterDist = Mathf.Infinity;
AttachNode nearestBayNode = null;
foreach (KeyValuePair<AttachNode, List<string>> bay in bays)
{
if (bay.Value != null)
{
if (!bay.Value.Contains(moduleEvaGrab.bayType))
{
KAS_Shared.DebugWarning("ContextMenuStore - Part type : " + moduleEvaGrab.bayType + " is not allowed | Attach node : " + bay.Key.id);
foreach (string type in bay.Value)
{
KAS_Shared.DebugWarning("ContextMenuStore - Allowed type : " + type);
}
continue;
}
}
if (bay.Key.attachedPart)
{
KAS_Shared.DebugWarning("ContextMenuStore - This node are used");
continue;
}
float distToBay = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, bay.Key.nodeTransform.position);
if (distToBay <= shorterDist)
{
shorterDist = distToBay;
nearestBayNode = bay.Key;
}
}
if (nearestBayNode == null)
{
ScreenMessages.PostScreenMessage("Part is not compatible or there is no free space", 2, ScreenMessageStyle.UPPER_CENTER);
KAS_Shared.DebugWarning("ContextMenuStore - Part is not compatible or there is no free space");
return;
}
AttachNode grabbedPartAn = moduleEvaGrab.part.findAttachNode(moduleEvaGrab.bayNode);
if (grabbedPartAn == null)
{
KAS_Shared.DebugError("ContextMenuStore - Grabbed part bay node not found !");
return;
}
KAS_Shared.DebugLog("ContextMenuStore - Drop part...");
moduleEvaGrab.Drop();
KAS_Shared.DebugLog("ContextMenuStore - Add node transform if not exist...");
KAS_Shared.AddNodeTransform(moduleEvaGrab.part, grabbedPartAn);
KAS_Shared.DebugLog("ContextMenuStore - Move part...");
KAS_Shared.MoveAlign(moduleEvaGrab.part.transform, grabbedPartAn.nodeTransform, nearestBayNode.nodeTransform);
moduleEvaGrab.part.transform.rotation *= Quaternion.Euler(moduleEvaGrab.bayRot);
//Couple part with bay
KAS_Shared.DebugLog("ContextMenuStore - Couple part with bay...");
moduleEvaGrab.part.Couple(this.part);
nearestBayNode.attachedPart = moduleEvaGrab.part;
fxSndStore.audio.Play();
moduleEvaGrab.part.SendMessage("OnBayStore", SendMessageOptions.DontRequireReceiver);
}
[KSPEvent(name = "ContextMenuRelease", active = true, guiActive = true, guiActiveUnfocused = true, guiName = "Release")]
public void ContextMenuRelease()
{
foreach (KeyValuePair<AttachNode, List<string>> bay in bays)
{
if (bay.Key.attachedPart)
{
Part tmpPart = bay.Key.attachedPart;
KAS_Shared.DecoupleFromAll(bay.Key.attachedPart);
Physics.IgnoreCollision(tmpPart.collider, this.part.collider);
}
}
}
[KSPAction("Release")]
public void ActionGroupRelease(KSPActionParam param)
{
if (!this.part.packed)
{
ContextMenuRelease();
}
}
}
}