-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractionController.cs
337 lines (321 loc) · 13.6 KB
/
InteractionController.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
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
using System;
using UnityEngine;
using System.Net;
public class InteractionController : MonoBehaviour
{
private PlayerController playerController;
private MachineInteraction machineInteraction;
private StorageInteraction storageInteraction;
public BlockInteraction blockInteraction;
public Coroutine paintingCoroutine;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
playerController = GetComponent<PlayerController>();
machineInteraction = new MachineInteraction(playerController, this);
storageInteraction = new StorageInteraction(playerController, this);
blockInteraction = new BlockInteraction(playerController, this);
}
//! Called once per frame by unity engine.
public void Update()
{
if (!playerController.stateManager.Busy())
{
// Raycast and associated data for interacting with machines and other objects.
float range = playerController.gameManager.chunkSize * 0.75f;
Transform camPos = Camera.main.gameObject.transform;
if (Physics.Raycast(camPos.position, camPos.forward, out RaycastHit hit, range))
{
float distance = Vector3.Distance(camPos.position, hit.point);
GameObject obj = hit.collider.gameObject;
if (obj != playerController.gameObject && GuiFree() && !IsNonInteractive(obj))
{
playerController.objectInSight = obj;
if (IsStorageContainer(obj) && distance <= 40)
{
storageInteraction.InteractWithStorageContainer();
}
else if (obj.GetComponent<StorageComputer>() != null && distance <= 40)
{
storageInteraction.InteractWithStorageComputer();
}
else if (obj.GetComponent<PowerSource>() != null && distance <= 40)
{
machineInteraction.InteractWithPowerSource();
}
else if (obj.GetComponent<NuclearReactor>() != null && distance <= 40)
{
machineInteraction.InteractWithNuclearReactor();
}
else if (obj.GetComponent<PowerConduit>() != null && distance <= 40)
{
machineInteraction.InteractWithPowerConduit();
}
else if (obj.GetComponent<Turret>() != null && distance <= 40)
{
machineInteraction.InteractWithTurret();
}
else if (obj.GetComponent<MissileTurret>() != null && distance <= 40)
{
machineInteraction.InteractWithMissileTurret();
}
else if (obj.GetComponent<UniversalExtractor>() != null && distance <= 40)
{
machineInteraction.InteractWithUniversalExtractor();
}
else if (obj.GetComponent<Auger>() != null && distance <= 40)
{
machineInteraction.InteractWithAuger();
}
else if (obj.GetComponent<DarkMatterCollector>() != null && distance <= 40)
{
machineInteraction.InteractWithDarkMatterCollector();
}
else if (obj.GetComponent<UniversalConduit>() != null && distance <= 40)
{
machineInteraction.InteractWithUniversalConduit();
}
else if (obj.GetComponent<DarkMatterConduit>() != null && distance <= 40)
{
machineInteraction.InteractWithDarkMatterConduit();
}
else if (obj.GetComponent<Smelter>() != null && distance <= 40)
{
machineInteraction.InteractWithSmelter();
}
else if (obj.GetComponent<AlloySmelter>() != null && distance <= 40)
{
machineInteraction.InteractWithAlloySmelter();
}
else if (obj.GetComponent<Extruder>() != null && distance <= 40)
{
machineInteraction.InteractWithExtruder();
}
else if (obj.GetComponent<RailCartHub>() != null && distance <= 40)
{
machineInteraction.InteractWithRailCartHub();
}
else if (obj.GetComponent<Retriever>() != null && distance <= 40)
{
machineInteraction.InteractWithRetriever();
}
else if (obj.GetComponent<AutoCrafter>() != null && distance <= 40)
{
machineInteraction.InteractWithAutoCrafter();
}
else if (obj.GetComponent<HeatExchanger>() != null && distance <= 40)
{
machineInteraction.InteractWithHeatExchanger();
}
else if (obj.GetComponent<GearCutter>() != null && distance <= 40)
{
machineInteraction.InteractWithGearCutter();
}
else if (obj.GetComponent<Press>() != null && distance <= 40)
{
machineInteraction.InteractWithPress();
}
else if (obj.GetComponent<ElectricLight>() != null && distance <= 40)
{
machineInteraction.InteractWithElectricLight();
}
else if (obj.GetComponent<Door>() != null && distance <= 40)
{
machineInteraction.InteractWithDoor();
}
else if (obj.GetComponent<ModMachine>() != null && distance <= 40)
{
machineInteraction.InteractWithModMachine();
}
else if (obj.GetComponent<ProtectionBlock>() != null)
{
machineInteraction.InteractWithProtectionBlock();
}
else if (obj.GetComponent<IronBlock>() != null)
{
blockInteraction.InteractWithIronBlock();
}
else if (obj.GetComponent<Steel>() != null)
{
blockInteraction.InteractWithSteelBlock();
}
else if (obj.GetComponent<Glass>() != null)
{
blockInteraction.InteractWithGlass();
}
else if (obj.GetComponent<Brick>() != null)
{
blockInteraction.InteractWithBricks();
}
else if (obj.GetComponent<ModBlock>() != null)
{
blockInteraction.InteractWithModBlock(obj.GetComponent<ModBlock>().blockName);
}
else if (obj.tag.Equals("CombinedMesh"))
{
blockInteraction.InteractWithCombinedMesh();
}
else
{
EndInteraction();
}
}
else if (obj != playerController.gameObject && GuiFree() && distance <= 40 && IsNonInteractive(obj))
{
playerController.objectInSight = obj;
}
else
{
EndInteraction();
}
}
else
{
EndInteraction();
}
}
}
//! Returns true if the GUI is available.
private bool GuiFree()
{
return playerController.inventoryOpen == false
&& playerController.escapeMenuOpen == false
&& playerController.tabletOpen == false
&& playerController.marketGUIopen == false;
}
//! Returns true if the object in question cannot be interacted with.
private bool IsNonInteractive(GameObject obj)
{
return obj.GetComponent<DarkMatter>() != null
|| obj.GetComponent<UniversalResource>() != null
|| obj.GetComponent<NetworkPlayer>() != null;
}
//! Returns true if the object in question is a storage container.
private Boolean IsStorageContainer(GameObject obj)
{
if (obj.GetComponent<InventoryManager>() != null && obj.GetComponent<Retriever>() == null && obj.GetComponent<AutoCrafter>() == null)
{
return true;
}
return false;
}
//! Destroys an object in the world and adds it's associated inventory item to the player's inventory.
public void CollectObject(string type)
{
bool canRemove = true;
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
canRemove = CanInteract();
}
if (canRemove == true)
{
bool spaceAvailable = false;
foreach (InventorySlot slot in playerController.playerInventory.inventory)
{
if (slot.typeInSlot.Equals("nothing") || slot.typeInSlot.Equals(type) && slot.amountInSlot < 1000)
{
spaceAvailable = true;
}
}
if (spaceAvailable == true)
{
playerController.playerInventory.AddItem(type, 1);
Destroy(playerController.objectInSight);
playerController.PlayCraftingSound();
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
Vector3 pos = playerController.objectInSight.transform.position;
Quaternion rot = playerController.objectInSight.transform.rotation;
UpdateNetwork(1, type, pos, rot);
}
}
else
{
playerController.cannotCollect = true;
playerController.PlayMissingItemsSound();
}
}
else
{
playerController.PlayMissingItemsSound();
}
}
//! Returns true if the player is allowed to remove the object.
public bool CanInteract()
{
bool closeToProtectionBlock = false;
ProtectionBlock[] protectionBlocks = FindObjectsOfType<ProtectionBlock>();
foreach (ProtectionBlock protectionBlock in protectionBlocks)
{
Vector3 playerPos = playerController.gameObject.transform.position;
Vector3 blockPos = protectionBlock.transform.position;
Vector3 playerPosNoY = new Vector3(playerPos.x, 0, playerPos.z);
Vector3 blockPosNoY = new Vector3(blockPos.x, 0, blockPos.z);
float distance = Vector3.Distance(playerPosNoY, blockPosNoY);
if (distance <= 160)
{
closeToProtectionBlock = true;
if (protectionBlock.IsAuthorizedUser(PlayerPrefs.GetString("UserName")))
{
return true;
}
else
{
return false;
}
}
}
if (closeToProtectionBlock == false)
{
return true;
}
return false;
}
//! Opens the machine GUI.
public void ToggleMachineGUI()
{
if (playerController.machineGUIopen == false)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
playerController.storageGUIopen = false;
playerController.craftingGUIopen = false;
playerController.inventoryOpen = false;
playerController.machineGUIopen = true;
}
else
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
playerController.inventoryOpen = false;
playerController.craftingGUIopen = false;
playerController.storageGUIopen = false;
playerController.machineGUIopen = false;
}
}
//! Sends instantiated block info to the server in multiplayer games.
private void UpdateNetwork(int destroy, string type, Vector3 pos, Quaternion rot)
{
using(WebClient client = new WebClient())
{
Uri uri = new Uri(PlayerPrefs.GetString("serverURL") + "/blocks");
string position = Mathf.Round(pos.x) + "," + Mathf.Round(pos.y) + "," + Mathf.Round(pos.z);
string rotation = Mathf.Round(rot.x) + "," + Mathf.Round(rot.y) + "," + Mathf.Round(rot.z) + "," + Mathf.Round(rot.w);
client.UploadStringAsync(uri, "POST", "@" + destroy + ":" + type + ":" + position + ":" + rotation);
}
}
//! Called when the player is no longer looking at any interactive objects.
private void EndInteraction()
{
playerController.machineGUIopen = false;
playerController.lookingAtCombinedMesh = false;
playerController.objectInSight = null;
playerController.machineInSight = null;
playerController.machineInputID = "none";
playerController.machineOutputID = "none";
playerController.machineType = "none";
playerController.machineAmount = 0;
playerController.machineInputAmount = 0;
playerController.machineOutputAmount = 0;
}
}