-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerController.cs
985 lines (898 loc) · 35.2 KB
/
PlayerController.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.SceneManagement;
//! This is the core class for the player which handles most of the player's interactions with the game.
public class PlayerController : MonoBehaviour
{
private InputManager inputManager;
public NetworkController networkController;
private Coroutine saveCoroutine;
private Coroutine networkWorldUpdateCoroutine;
private Vector3 originalPosition;
private List<Vector3> networkItemLocations;
public Vector3 destroyStartPosition;
public Vector3 buildStartPosition;
public StateManager stateManager;
public GameManager gameManager;
public Camera mCam;
public InventoryManager playerInventory;
public InventoryManager storageInventory;
public AudioSource builderSound;
public AudioSource guiSound;
public GameObject objectInSight;
public GameObject machineInSight;
public LaserController laserController;
public BlockSelector blockSelector;
public Door doorToEdit;
public bool cannotCollect;
public bool building;
public bool inventoryOpen;
public bool storageGUIopen;
public bool machineGUIopen;
public bool craftingGUIopen;
public bool marketGUIopen;
public bool buildSettingsGuiOpen;
public bool doorGUIopen;
public bool remoteStorageActive;
public bool escapeMenuOpen;
public bool machineHasPower;
public bool lookingAtCombinedMesh;
public bool tabletOpen;
public bool optionsGUIopen;
public bool creativeMode;
public bool timeToDeliverWarningRecieved;
public bool draggingItem;
public bool displayingBuildItem;
public bool exiting;
public bool requestedSave;
public bool invalidAugerPlacement;
public bool autoAxisMessage;
public bool invalidRailCartPlacement;
public bool helpMenuOpen;
public bool requestedBuildingStop;
public bool pirateAttackWarningActive;
public bool meteorShowerWarningActive;
public bool destructionMessageActive;
public bool timeToDeliver;
public bool paintGunActive;
public bool paintColorSelected;
public bool videoMenuOpen;
public bool schematicMenuOpen;
public bool crosshairEnabled = true;
public bool stoppingBuildCoRoutine;
public bool separatedBlocks;
public bool destroying;
public bool requestedChunkLoad;
public bool blockLimitMessage;
public bool laserCannonActive;
public bool scannerActive;
public bool firing;
public bool scanning;
public bool moveForward;
public bool moveBackward;
public bool moveLeft;
public bool moveRight;
public bool moveUp;
public bool moveDown;
public bool stopMovement;
private bool gotPosition;
private bool gameStarted;
private bool checkedForCreativeMode;
private bool meteorShowerWarningReceived;
private bool pirateAttackWarningReceived;
private bool destructionMessageReceived;
private bool movedPlayer;
private bool introDisplayed;
public string cubeloc = "up";
public string machineID = "unassigned";
public string machineOutputID = "unassigned";
public string machineOutputID2 = "unassigned";
public string machineInputID = "unassigned";
public string machineInputID2 = "unassigned";
public string machineType;
public string machineType2;
public string machineInputType;
public string machineInputType2;
public string machineOutputType;
public string currentTabletMessage = "";
public string buildType = "Dark Matter Collector";
public string nextBuildType = "Turret";
public string previousBuildType = "Dark Matter Conduit";
public float cannotCollectTimer;
public float collectorAmount;
public float hxAmount;
public float machineAmount;
public float machineAmount2;
public float machineInputAmount;
public float machineInputAmount2;
public float machineOutputAmount;
public float deliveryWarningTimer;
public float buildItemDisplayTimer;
public float footStepTimer;
public float footStepSoundFrquency;
public float buildIncrementTimer;
public float invalidAugerPlacementTimer;
public float autoAxisMessageTimer;
public float invalidRailCartPlacementTimer;
public float paintRed;
public float paintGreen;
public float paintBlue;
public float requestedSaveTimer;
public float blockLimitMessageTimer;
public float graphicsQuality = 999;
public int playerMoveSpeed;
public int machinePower;
public int machineSpeed;
public double machineRange;
public int networkedConduitRange;
public int networkedMachineSpeed;
public bool networkedDualPower;
public int networkedHubCircuit;
public int networkedHubRange;
public bool networkedHubStop;
public float networkedHubStopTime;
public int machineHeat;
public int machineCooling;
public int buildMultiplier = 1;
public int defaultRange = 6;
public int money;
public int destructionMessageCount;
public int storageComputerInventory;
public string protectionList;
public AudioClip footStep1;
public AudioClip footStep2;
public AudioClip footStep3;
public AudioClip footStep4;
public AudioClip metalFootStep1;
public AudioClip metalFootStep2;
public AudioClip metalFootStep3;
public AudioClip metalFootStep4;
public AudioClip missingItemsClip;
public AudioClip buttonClip;
public AudioClip craftingClip;
public GameObject ping;
public GameObject tablet;
public GameObject scanner;
public GameObject laserCannon;
public GameObject paintGun;
public GameObject paintGunTank;
public GameObject adjustedPaintGunTank;
public GameObject adjustedPaintGunTank2;
public GameObject muzzleFlash;
public GameObject scannerFlash;
public GameObject weaponHit;
public GameObject darkMatterCollector;
public GameObject darkMatter;
public GameObject darkMatterConduit;
public GameObject ironBlock;
public GameObject ironRamp;
public GameObject steel;
public GameObject steelRamp;
public GameObject storageContainer;
public GameObject universalExtractor;
public GameObject auger;
public GameObject quantumHatchway;
public GameObject door;
public GameObject universalConduit;
public GameObject glass;
public GameObject brick;
public GameObject electricLight;
public GameObject smelter;
public GameObject turret;
public GameObject missileTurret;
public GameObject solarPanel;
public GameObject generator;
public GameObject reactorTurbine;
public GameObject nuclearReactor;
public GameObject press;
public GameObject extruder;
public GameObject retriever;
public GameObject railCart;
public GameObject railCartHub;
public GameObject storageComputer;
public GameObject autoCrafter;
public GameObject heatExchanger;
public GameObject gearCutter;
public GameObject alloySmelter;
public GameObject powerConduit;
public GameObject playerBody;
public GameObject builder;
public GameObject headlamp;
public GameObject guiObject;
public GameObject currentStorageComputer;
public GameObject buildObject;
public GameObject modMachine;
public GameObject modBlock;
public GameObject item;
public GameObject networkPlayer;
public GameObject protectionBlock;
public Material constructionMat;
public bool addedModBlocks;
// Called by unity engine on start up to initialize variables.
public void Start()
{
// Reference to the game manager and state manager.
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
stateManager = GameObject.Find("GameManager").GetComponent<StateManager>();
// Initialize component classes.
playerInventory = GetComponent<InventoryManager>();
laserController = new LaserController(this, gameManager);
// Load mouse settings.
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
if (PlayerPrefs.GetFloat("xSensitivity") != 0)
{
GetComponent<MSCameraController>().CameraSettings.firstPerson.sensibilityX = PlayerPrefs.GetFloat("xSensitivity");
}
if (PlayerPrefs.GetFloat("ySensitivity") != 0)
{
GetComponent<MSCameraController>().CameraSettings.firstPerson.sensibilityY = PlayerPrefs.GetFloat("ySensitivity");
}
GetComponent<MSCameraController>().CameraSettings.firstPerson.invertYInput = PlayerPrefsX.GetPersistentBool("mouseInverted");
// Loading volume settings.
if (PlayerPrefs.GetFloat("volume") > 0)
{
AudioListener.volume = PlayerPrefs.GetFloat("volume");
GetComponent<MSCameraController>().cameras[0].volume = AudioListener.volume;
}
else
{
GetComponent<MSCameraController>().cameras[0].volume = 2.5f;
}
// Audio source for placing blocks.
builderSound = builder.GetComponent<AudioSource>();
// Audio source for GUI related sounds.
guiSound = guiObject.GetComponent<AudioSource>();
// Graphics quality.
if (PlayerPrefsX.GetPersistentBool("changedGraphicsQuality") == true)
{
QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("graphicsQuality"));
}
graphicsQuality = QualitySettings.GetQualityLevel();
// Vsync.
QualitySettings.vSyncCount = PlayerPrefs.GetInt("vSyncCount");
int range = PlayerPrefs.GetInt("defaultRange");
defaultRange = range >= 10 ? range : 10;
// Fog and Scanner color for atmospheric worlds.
if (SceneManager.GetActiveScene().name.Equals("QE_World_Atmo"))
{
scannerFlash.GetComponent<Light>().color = Color.white;
scannerFlash.GetComponent<Light>().intensity = 1;
}
if (!SceneManager.GetActiveScene().name.Equals("QE_World"))
{
float fogDensity = PlayerPrefs.GetFloat("fogDensity");
RenderSettings.fogDensity = fogDensity > 0 ? fogDensity : 0.00025f;
RenderSettings.fog = PlayerPrefsX.GetPersistentBool("fogEnabled");
}
inputManager = new InputManager(this);
blockSelector = new BlockSelector(this);
networkController = new NetworkController(this);
networkItemLocations = new List<Vector3>();
}
//! Called once per frame by unity engine.
public void Update()
{
if (addedModBlocks == false)
{
if (ReadyToLoadModBlocks())
{
BlockDictionary blockDictionary = GetComponent<BuildController>().blockDictionary;
blockDictionary.AddModBlocks(blockDictionary.blockDictionary);
blockDictionary.AddModMachines(blockDictionary.machineDictionary);
blockDictionary.AddModMeshes(blockDictionary.meshDictionary);
addedModBlocks = true;
}
}
// Get a refrence to the camera.
if (mCam == null)
{
mCam = Camera.main;
if (PlayerPrefs.GetFloat("FOV") != 0)
{
mCam.fieldOfView = PlayerPrefs.GetFloat("FOV");
}
if (PlayerPrefs.GetFloat("drawDistance") != 0)
{
mCam.farClipPlane = PlayerPrefs.GetFloat("drawDistance");
}
}
else
{
// Disable mouse look during main menu sequence.
gameObject.GetComponent<MSCameraController>().enabled &= gameStarted != false;
// Get the spawn location, for respawning the player character.
if (gotPosition == false)
{
originalPosition = transform.position;
gotPosition = true;
}
// The state manager has finished loading the world.
if (stateManager.worldLoaded == true)
{
gameStarted = true;
if (FileBasedPrefs.GetBool(stateManager.worldName + "oldWorld") == false)
{
OpenTabletOnFirstLoad();
}
else if (movedPlayer == false)
{
MovePlayerToSavedLocation();
}
if (ShouldShowTabletIntro())
{
ShowTabletIntro();
}
if (checkedForCreativeMode == false && stateManager.worldLoaded == true)
{
creativeMode = FileBasedPrefs.GetBool(stateManager.worldName + "creativeMode");
if (creativeMode == true)
{
Debug.Log("World [" + stateManager.worldName + "] running in creative mode.");
}
else
{
Debug.Log("World [" + stateManager.worldName + "] running in standard mode.");
}
checkedForCreativeMode = true;
}
// Destruction messages.
if (destructionMessageActive == true)
{
if (destructionMessageCount > 10)
{
currentTabletMessage = "";
destructionMessageCount = 0;
}
if (destructionMessageReceived == false)
{
tablet.GetComponent<AudioSource>().Play();
destructionMessageReceived = true;
}
}
else
{
destructionMessageCount = 0;
destructionMessageReceived = false;
}
// Pirate attack warnings.
if (pirateAttackWarningActive == true)
{
currentTabletMessage = "Warning! Warning! Warning! Warning! Warning!\n\nIncoming pirate attack!";
if (pirateAttackWarningReceived == false)
{
tablet.GetComponent<AudioSource>().Play();
pirateAttackWarningReceived = true;
}
}
else
{
pirateAttackWarningReceived = false;
}
// Meteor shower warnings.
if (meteorShowerWarningActive == true)
{
currentTabletMessage = "Warning! Warning! Warning! Warning! Warning!\n\nIncoming meteor shower!";
if (meteorShowerWarningReceived == false)
{
tablet.GetComponent<AudioSource>().Play();
meteorShowerWarningReceived = true;
}
}
else
{
meteorShowerWarningReceived = false;
}
if (timeToDeliver == true)
{
DisplayDeliveryWarning();
}
else
{
timeToDeliverWarningRecieved = false;
}
if (destroying == true)
{
ModifyCombinedMeshes();
}
if (requestedBuildingStop == true)
{
HandleBuildingStopRequest();
}
// The player controller is notified that the game manager finished combining meshes.
if (stoppingBuildCoRoutine == true && gameManager.working == false)
{
stoppingBuildCoRoutine = false;
}
if (requestedChunkLoad == true)
{
HandleChunkLoadRequest();
}
// Locking or unlocking the mouse cursor for GUI interaction.
if (GuiOpen())
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
gameObject.GetComponent<MSCameraController>().enabled = false;
}
else
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
gameObject.GetComponent<MSCameraController>().enabled = true;
}
if (requestedSave == true)
{
HandleSaveRequest();
}
if (storageInventory != null && storageGUIopen == true)
{
CheckStorageDistance();
}
if (stateManager.saving == false)
{
inputManager.HandleInput();
EnforceWorldLimits();
}
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
networkController.NetworkFrame();
if (networkController.networkWorldUpdateCoroutineBusy == false)
{
networkWorldUpdateCoroutine = StartCoroutine(networkController.NetWorkWorldUpdate());
}
}
}
}
}
//! Frame-rate independent physics calculations.
public void FixedUpdate()
{
if (moveForward == true)
{
Vector3 moveDir = Vector3.Normalize(new Vector3(mCam.gameObject.transform.forward.x, 0, mCam.gameObject.transform.forward.z));
GetComponent<Rigidbody>().AddForce(moveDir * playerMoveSpeed * 200);
}
if (moveBackward == true)
{
Vector3 moveDir = Vector3.Normalize(new Vector3(mCam.gameObject.transform.forward.x, 0, mCam.gameObject.transform.forward.z));
GetComponent<Rigidbody>().AddForce(-moveDir * playerMoveSpeed * 200);
}
if (moveLeft == true)
{
GetComponent<Rigidbody>().AddForce(-mCam.gameObject.transform.right * playerMoveSpeed * 200);
}
if (moveRight == true)
{
GetComponent<Rigidbody>().AddForce(mCam.gameObject.transform.right * playerMoveSpeed * 200);
}
if (moveUp == true)
{
if (cInput.GetKey("Sprint/Boost"))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 10000);
}
else
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 5800);
}
}
if (stopMovement == true)
{
GetComponent<Rigidbody>().velocity = new Vector3(0,0,0);
stopMovement = false;
}
if (!Physics.Raycast(transform.position, -transform.up, out RaycastHit hit, 10))
{
GetComponent<Rigidbody>().AddForce(-transform.up * 5000);
}
}
//! Returns true when prerequisites are met for loading machines added by mods.
private bool ReadyToLoadModBlocks()
{
return addedModBlocks == false
&& modBlock != null
&& modMachine != null
&& blockSelector != null
&& BlockDictionaryInitiazlied()
&& LoadedModTextures();
}
//! Returns true if block dictionary is non null.
private bool BlockDictionaryInitiazlied()
{
if (GetComponent<BuildController>() != null)
{
return GetComponent<BuildController>().blockDictionary != null;
}
return false;
}
//! Returns true when all mod textures have finished loading.
private bool LoadedModTextures()
{
if (gameManager.GetComponent<TextureDictionary>() != null)
{
return gameManager.GetComponent<TextureDictionary>().addedModTextures;
}
return false;
}
//! Saves the player's location and money.
public void SavePlayerData()
{
playerInventory.SaveData();
GameObject.Find("LanderCargo").GetComponent<InventoryManager>().SaveData();
FileBasedPrefs.SetBool(stateManager.worldName + "oldWorld", true);
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
PlayerPrefsX.SetPersistentVector3(stateManager.worldName + "playerPosition", transform.position);
PlayerPrefs.SetInt(stateManager.worldName + "money", money);
}
else
{
PlayerPrefsX.SetVector3(stateManager.worldName + "playerPosition", transform.position);
FileBasedPrefs.SetInt(stateManager.worldName + "money", money);
}
}
//! Applies global settings.
public void ApplySettings()
{
if ((int)graphicsQuality != 999)
{
QualitySettings.SetQualityLevel((int)graphicsQuality, true);
PlayerPrefsX.SetPersistentBool("changedGraphicsQuality", true);
}
PlayerPrefs.SetInt("graphicsQuality", (int)graphicsQuality);
PlayerPrefsX.SetPersistentBool("mouseInverted", GetComponent<MSCameraController>().CameraSettings.firstPerson.invertYInput);
PlayerPrefs.SetFloat("xSensitivity", GetComponent<MSCameraController>().CameraSettings.firstPerson.sensibilityX);
PlayerPrefs.SetFloat("ySensitivity", GetComponent<MSCameraController>().CameraSettings.firstPerson.sensibilityY);
PlayerPrefs.SetFloat("FOV", mCam.fieldOfView);
PlayerPrefs.SetFloat("drawDistance", mCam.farClipPlane);
PlayerPrefs.SetFloat("volume", GetComponent<MSCameraController>().cameras[0].volume);
PlayerPrefsX.SetPersistentBool("blockPhysics", gameManager.blockPhysics);
PlayerPrefsX.SetPersistentBool("hazardsEnabled", gameManager.hazardsEnabled);
PlayerPrefsX.SetPersistentBool("fogEnabled", RenderSettings.fog);
PlayerPrefs.SetFloat("fogDensity", RenderSettings.fogDensity);
PlayerPrefs.SetInt("chunkSize", gameManager.chunkSize);
PlayerPrefs.SetFloat("simulationSpeed", gameManager.simulationSpeed);
PlayerPrefs.SetInt("defaultRange", defaultRange);
PlayerPrefs.SetInt("vSyncCount", QualitySettings.vSyncCount);
PlayerPrefs.Save();
}
//! Plays a sound.
public void PlayButtonSound()
{
guiSound.volume = 0.15f;
guiSound.clip = buttonClip;
guiSound.Play();
}
//! Plays a sound.
public void PlayCraftingSound()
{
guiSound.volume = 0.3f;
guiSound.clip = craftingClip;
guiSound.Play();
}
//! Plays a sound.
public void PlayMissingItemsSound()
{
guiSound.volume = 0.6f;
guiSound.clip = missingItemsClip;
guiSound.Play();
}
//! Returns true if any GUI is open.
public bool GuiOpen()
{
return cGUI.showingInputGUI == true
|| inventoryOpen == true
|| escapeMenuOpen == true
|| machineGUIopen == true
|| tabletOpen == true
|| marketGUIopen == true
|| (paintGunActive == true && paintColorSelected == false)
|| buildSettingsGuiOpen == true
|| doorGUIopen == true;
}
//! Returns true at the beginning of the first in-game day when the intro should be displayed on the tablet.
private bool ShouldShowTabletIntro()
{
return currentTabletMessage.Equals("")
&& GameObject.Find("Rocket").GetComponent<Rocket>().day == 1
&& (int)GameObject.Find("Rocket").GetComponent<Rocket>().gameTime < 200;
}
//! Displays the intro message on the tablet.
private void ShowTabletIntro()
{
currentTabletMessage = "To all deployed members of:\nQuantum Engineering, \nDark Matter Research, \nMoon Base Establishment Division" +
"\n\n" + "Manufacture of Quantum Manifestation Projection Units \nis going ahead as planned. " + "Our systems show all \nengineers have " +
"successfully arrived at their \ndesignated FOB establishment locations. " + "\nExpected return payload to planetary facilities \nat the end " +
"of day 1 is currently 50 units \nof non-anomylous dark matter as planned. " + "\n\n" + "As always, thank you for your service." + "\n" +
"Agrat Eirelis:" + "\n" + "Chief Officer," + "\n" + "Quantum Engineering," + "\n" + "Dark Matter Research," + "\n" + "Moon Base Establishment Division";
}
//! Moves the player to their previous location when a game is loaded.
private void MovePlayerToSavedLocation()
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
transform.position = PlayerPrefsX.GetPersistentVector3(stateManager.worldName + "playerPosition");
money = PlayerPrefs.GetInt(stateManager.worldName + "money");
}
else
{
transform.position = PlayerPrefsX.GetVector3(stateManager.worldName + "playerPosition");
money = FileBasedPrefs.GetInt(stateManager.worldName + "money");
}
movedPlayer = true;
}
//! Opens the tablet to display a message the first time the world is loaded.
private void OpenTabletOnFirstLoad()
{
if (GetComponent<MainMenu>().finishedLoading == true)
{
if (introDisplayed == false)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
tabletOpen = true;
introDisplayed = true;
}
}
}
//! Displays a message on the player's tablet when the rocket is landing to collect dark matter.
private void DisplayDeliveryWarning()
{
deliveryWarningTimer += 1 * Time.deltaTime;
if (deliveryWarningTimer > 30)
{
deliveryWarningTimer = 0;
}
int payload = GameObject.Find("Rocket").GetComponent<Rocket>().payloadRequired;
currentTabletMessage = "Please load: " + payload + " dark matter\ninto the rocket near the lunar lander."
+ "\nExpected return payload to planetary facilities \nat the end " +
"of the day tomorrow is currently:" + "\n" + (payload * 2) + " units of dark matter. "
+ "\n\n" + "As always, Thank you for your service." + "\n" +
"Agrat Eirelis:" + "\n" + "Chief Officer," + "\n" + "Quantum Engineering," + "\n"
+ "Dark Matter Research," + "\n" + "Moon Base Establishment Division";
if (timeToDeliverWarningRecieved == false)
{
tablet.GetComponent<AudioSource>().Play();
timeToDeliverWarningRecieved = true;
}
}
//! Stops the players building mode and sends a request to the game manager to recombine any edited combined meshes.
private void HandleBuildingStopRequest()
{
if (gameManager.working == false)
{
stoppingBuildCoRoutine = true;
gameManager.meshManager.CombineBlocks();
separatedBlocks = false;
building = false;
destroying = false;
requestedBuildingStop = false;
}
else
{
requestedBuildingStop = true;
}
}
//! Handles the sending of chunk load requests for modifying combined meshes.
private void ModifyCombinedMeshes()
{
float distance = Vector3.Distance(transform.position, destroyStartPosition);
if (distance > gameManager.chunkSize * 0.75f)
{
if (gameManager.working == false)
{
gameManager.meshManager.SeparateBlocks(transform.position, "all", true);
separatedBlocks = true;
}
else
{
requestedChunkLoad = true;
}
destroyStartPosition = transform.position;
}
}
//! Handles requests to load chunks of blocks from combined meshes near the player.
private void HandleChunkLoadRequest()
{
if (gameManager.working == false)
{
if (destroying == false)
{
gameManager.meshManager.SeparateBlocks(transform.position, "all", true);
}
else
{
gameManager.meshManager.SeparateBlocks(transform.position, "all", false);
}
separatedBlocks = true;
requestedChunkLoad = false;
}
}
//! Opens the escape menu.
public void OpenEscapeMenu()
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
gameObject.GetComponent<MSCameraController>().enabled = false;
escapeMenuOpen = true;
}
//! Enforces world size limitations.
private void EnforceWorldLimits()
{
if (gameObject.transform.position.x > 4500)
{
gameObject.transform.position = new Vector3(4500, gameObject.transform.position.y, gameObject.transform.position.z);
}
if (gameObject.transform.position.z > 4500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 4500);
}
if (gameObject.transform.position.x < -4500)
{
gameObject.transform.position = new Vector3(-4500, gameObject.transform.position.y, gameObject.transform.position.z);
}
if (gameObject.transform.position.z < -4500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, -4500);
}
if (gameObject.transform.position.y > 500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, 500, gameObject.transform.position.z);
}
if (gameObject.transform.position.y < -100)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, 500, gameObject.transform.position.z);
}
}
//! Handles requests to exit the game
private void HandleSaveRequest()
{
requestedSaveTimer += 1 * Time.deltaTime;
if (requestedSaveTimer >= 5)
{
if (gameManager.working == false)
{
SavePlayerData();
GameObject.Find("GameManager").GetComponent<GameManager>().RequestSaveOperation();
saveCoroutine = StartCoroutine(Save());
requestedSaveTimer = 0;
requestedSave = false;
}
}
}
//! Closes the storage GUI if the player has moved too far from the container.
private void CheckStorageDistance()
{
if (remoteStorageActive == false)
{
float distance = Vector3.Distance(transform.position, storageInventory.gameObject.transform.position);
if (distance > 40)
{
storageGUIopen = false;
inventoryOpen = false;
}
}
else
{
float distance = Vector3.Distance(transform.position, currentStorageComputer.transform.position);
if (distance > 40)
{
storageGUIopen = false;
inventoryOpen = false;
}
}
}
//! Drops a stack of items on the ground.
public void DropItem(InventorySlot slot)
{
Vector3 dropPos = mCam.transform.position + mCam.transform.forward * 10;
GameObject droppedItem = Instantiate(item, dropPos, mCam.transform.rotation);
float x = Mathf.Round(dropPos.x);
float y = Mathf.Round(dropPos.y);
float z = Mathf.Round(dropPos.z);
Vector3 roundedPos = new Vector3(x, y, z);
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
while (networkItemLocations.Contains(roundedPos))
{
roundedPos.y++;
}
networkItemLocations.Add(roundedPos);
}
droppedItem.GetComponent<Item>().startPosition = roundedPos;
droppedItem.GetComponent<Item>().type = slot.typeInSlot;
droppedItem.GetComponent<Item>().amount = slot.amountInSlot;
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
networkController.networkSend.SendItemData(0, slot.typeInSlot, slot.amountInSlot, roundedPos);
}
slot.typeInSlot = "nothing";
slot.amountInSlot = 0;
PlayCraftingSound();
}
//! Used to handle walking up stairs.
public void OnCollisionStay(Collision collision)
{
if (collision.gameObject.name == "modBlockHolder(Clone)")
{
Transform[] transforms = collision.gameObject.GetComponentsInChildren<Transform>(true);
foreach (Transform t in transforms)
{
if (t.GetComponent<ModBlock>() != null)
{
string blockName = t.GetComponent<ModBlock>().blockName.ToUpper();
if (blockName.Contains("STAIR"))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 1000);
}
}
}
}
else if (collision.gameObject.GetComponent<ModBlock>() != null)
{
string blockName = collision.gameObject.GetComponent<ModBlock>().blockName.ToUpper();
if (blockName.Contains("STAIR"))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 1000);
}
}
}
//! Used to handle picking up items.
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Item>() != null)
{
Item colItem = collision.gameObject.GetComponent<Item>();
playerInventory.AddItem(colItem.type, colItem.amount);
if (playerInventory.itemAdded)
{
Destroy(collision.gameObject);
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
networkController.networkSend.SendItemData(1, colItem.type, colItem.amount, colItem.startPosition);
networkController.networkReceive.itemDatabaseDelay = 0;
}
PlayCraftingSound();
}
}
}
//! Handles saving world and exiting to the main menu.
public static IEnumerator Save()
{
float f = 0;
while (f < 6000)
{
f++;
if (GameObject.Find("GameManager").GetComponent<GameManager>().blocksCombined == false)
{
if (GameObject.Find("GameManager").GetComponent<GameManager>().dataSaveRequested == false)
{
if (GameObject.Find("GameManager").GetComponent<StateManager>().saving == false)
{
Debug.Log("Game saved to " + FileBasedPrefs.GetSaveFilePath());
Debug.Log("Creating backup...");
string fileName = GameObject.Find("GameManager").GetComponent<StateManager>().worldName;
string destinationPath = Path.Combine(Application.persistentDataPath, "SaveData/" + fileName + ".bak");
File.Copy(FileBasedPrefs.GetSaveFilePath(), destinationPath, true);
Debug.Log("Backup saved to " + destinationPath);
if (GameObject.Find("Player").GetComponent<PlayerController>().exiting == true)
{
Debug.Log("Loading main menu...");
FileBasedPrefs.initialized = false;
FileBasedPrefs._latestData = null;
SceneManager.LoadScene(0);
}
else
{
break;
}
}
}
}
yield return new WaitForSeconds(0.1f);
}
}
}