-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainMenu.cs
1178 lines (1103 loc) · 49.4 KB
/
MainMenu.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using System;
using System.Linq;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Collections;
public class MainMenu : MonoBehaviour
{
public GUISkin thisGUIskin;
public Texture2D titleTexture;
public Texture2D title2Texture;
public Texture2D titleTextureSD;
public Texture2D guiBackground;
public Texture2D worldListBackground;
public Texture2D playerModel;
private Texture2D colorSelectTexture;
public GameObject videoPlayer;
public GameObject menuSoundObject;
public GameObject ambientSoundObject;
public bool worldSelected;
public bool finishedLoading;
private string worldName = "Enter world name.";
private string username = "Enter user name.";
private string password = "Enter password.";
private string serverURL = "Enter server IP address.";
private string[] worlds;
private StateManager stateManager;
private List<string> worldList;
private AudioSource buttonSounds;
private AudioSource ambient;
private Coroutine worldDownloadCoroutine;
private int scene;
private bool local;
private float waitForVideoTimer;
private bool notWideScreen;
private bool worldSelectPrompt;
private bool playingVideo;
private bool hosting;
private bool downloadPrompt;
private bool worldNamePrompt;
private bool deletePrompt;
private bool escapePrompt;
private bool multiplayerPrompt;
private bool localPrompt;
private bool publicPrompt;
private bool playerColorPrompt;
private bool userNamePrompt;
private bool passwordPrompt;
private bool networkAddressPrompt;
private bool downloadingWorld;
private string serverList = "none";
private float playerRed = 1.0f;
private float playerGreen = 1.0f;
private float playerBlue = 1.0f;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
string[] commandLineOptions = Environment.GetCommandLineArgs();
stateManager = FindObjectOfType<StateManager>();
worldList = new List<string>();
colorSelectTexture = new Texture2D(512, 128);
buttonSounds = menuSoundObject.GetComponent<AudioSource>();
ambient = ambientSoundObject.GetComponent<AudioSource>();
if (!commandLineOptions.Contains("-batchmode"))
{
videoPlayer.GetComponent<VP>().PlayVideo("QE_Title.webm",true,0);
ambient.Play();
}
if (PlayerPrefsX.GetPersistentBool("changingWorld") == true)
{
stateManager.worldName = PlayerPrefs.GetString("worldName");
PlayerPrefs.DeleteKey("changingWorld");
PlayerPrefs.DeleteKey("worldName");
PlayerPrefs.Save();
worldSelected = true;
ambient.enabled = false;
}
else if (commandLineOptions.Contains("-batchmode"))
{
SetupDedicatedServer(commandLineOptions);
}
}
//! Called once per frame by unity engine.
public void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GuiFree())
{
escapePrompt = !escapePrompt;
}
else
{
networkAddressPrompt = false;
worldSelectPrompt = false;
deletePrompt = false;
escapePrompt = false;
multiplayerPrompt = false;
localPrompt = false;
playerColorPrompt = false;
userNamePrompt = false;
downloadPrompt = false;
userNamePrompt = false;
passwordPrompt = false;
publicPrompt = false;
}
buttonSounds.Play();
}
}
//! Gets the local address for LAN games.
private string GetLocalAddress()
{
string[] commandLineOptions = Environment.GetCommandLineArgs();
string hostName = Dns.GetHostName();
string address = Dns.GetHostEntry(hostName).AddressList[0].ToString();
return address;
}
//! Gets external IP address for online games.
private string GetExternalAddress()
{
using (WebClient client = new WebClient())
{
Uri uri = new Uri("https://api.ipify.org");
return client.DownloadString(uri);
}
}
//! Gets server data from master server.
private async Task GetServerList()
{
using (WebClient client = new WebClient())
{
Uri uri = new Uri("http://45.77.158.179:48000/servers");
serverList = await client.DownloadStringTaskAsync(uri);
}
}
//! Sets up a dedicated server.
private void SetupDedicatedServer(string[] commandLineOptions)
{
worldName = commandLineOptions.Contains("-local") ? GetLocalAddress() : GetExternalAddress();
PlayerPrefs.SetString("ip", worldName);
FileBasedPrefs.SetWorldName(worldName);
PlayerPrefsX.SetPersistentBool("multiplayer", true);
PlayerPrefsX.SetPersistentBool("hosting", true);
PlayerPrefs.SetString("serverURL", "http://" + worldName + ":5000");
PlayerPrefs.SetString("UserName", worldName);
bool creativeMode = commandLineOptions.Contains("-creative");
FileBasedPrefs.SetBool(worldName + "creativeMode", creativeMode);
bool hazardsEnabled = commandLineOptions.Contains("-hazards");
PlayerPrefsX.SetPersistentBool("hazardsEnabled", hazardsEnabled);
bool announce = commandLineOptions.Contains("-announce");
PlayerPrefsX.SetPersistentBool("announce", announce);
UnityEngine.Debug.Log("Creative Mode: " + FileBasedPrefs.GetBool(worldName + "creativeMode"));
UnityEngine.Debug.Log("Hazards: " + PlayerPrefsX.GetPersistentBool("hazardsEnabled"));
UnityEngine.Debug.Log("Announce: " + PlayerPrefsX.GetPersistentBool("announce"));
StartServer();
Thread.Sleep(5000);
worldList = PlayerPrefsX.GetPersistentStringArray("Worlds").ToList();
if (worldList.Count < 10)
{
if (!worldList.Contains(worldName))
{
worldList.Add(worldName);
foreach (string option in commandLineOptions)
{
if (option.Contains("-scene"))
{
int cmd = 0;
try
{
cmd = int.Parse(option.Split('=')[1]);
}
catch
{
break;
}
scene = cmd == 1 || cmd == 3 ? cmd : 0;
FileBasedPrefs.SetInt(worldName + "scene", scene);
break;
}
}
if (scene != 0)
{
ChangeScene();
}
else
{
PreStart();
}
}
else
{
PreStart();
}
}
else if (worldList.Contains(worldName))
{
PreStart();
}
else
{
UnityEngine.Debug.Log("Failed to start server. World list is full.");
}
}
//! Starts the external server program.
private void StartServer()
{
string[] commandLineOptions = Environment.GetCommandLineArgs();
bool headless = commandLineOptions.Contains("-headless");
bool devel = commandLineOptions.Contains("-devel");
bool hazards = commandLineOptions.Contains("-hazards");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
StartLinuxServer(headless, devel, hazards);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
StartWindowsServer(headless, devel, hazards);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
StartMacServer(headless, devel, hazards);
}
}
//! Starts the server python app for Linux OS.
private void StartLinuxServer(bool headless, bool devel, bool hazards)
{
if (Application.isEditor || UnityEngine.Debug.isDebugBuild || devel == true)
{
UnityEngine.Debug.Log("Starting development server...");
string options = headless == true ? "local devel headless" : "local devel";
if (hazards == true) { options += " hazards"; }
Process.Start(Application.dataPath + "/Linux_Server/qe_server", options);
}
else if (local == true)
{
UnityEngine.Debug.Log("Starting LAN server...");
string options = headless == true ? "local headless" : "local";
if (hazards == true) { options += " hazards"; }
Process.Start(Application.dataPath + "/Linux_Server/qe_server", options);
}
else if (headless == true)
{
UnityEngine.Debug.Log("Starting online server...");
string options = hazards == true ? "headless hazards" : "headless";
Process.Start(Application.dataPath + "/Linux_Server/qe_server", options);
}
else if (hazards == true)
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Linux_Server/qe_server", "hazards");
}
else
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Linux_Server/qe_server");
}
}
//! Starts the server python app for Windows OS.
private void StartWindowsServer(bool headless, bool devel, bool hazards)
{
if (local == true)
{
UnityEngine.Debug.Log("Starting LAN server...");
string options = headless == true ? "local headless" : "local";
if (hazards == true) { options += " hazards"; }
Process.Start(Application.dataPath + "/Windows_Server/qe_server.exe", options);
}
else if (headless == true)
{
UnityEngine.Debug.Log("Starting online server...");
string options = hazards == true ? "headless hazards" : "headless";
Process.Start(Application.dataPath + "/Windows_Server/qe_server.exe", options);
}
else if (hazards == true)
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Windows_Server/qe_server.exe", "hazards");
}
else
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Windows_Server/qe_server.exe");
}
}
//! Starts the server python app for Mac OS.
private void StartMacServer(bool headless, bool devel, bool hazards)
{
if (local == true)
{
UnityEngine.Debug.Log("Starting LAN server...");
string options = headless == true ? "local headless" : "local";
if (hazards == true) { options += " hazards"; }
Process.Start(Application.dataPath + "/Mac_Server/qe_server", options);
}
else if (headless == true)
{
UnityEngine.Debug.Log("Starting online server...");
string options = hazards == true ? "headless hazards" : "headless";
Process.Start(Application.dataPath + "/Mac_Server/qe_server", options);
}
else if (hazards == true)
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Mac_Server/qe_server", "hazards");
}
else
{
UnityEngine.Debug.Log("Starting online server...");
Process.Start(Application.dataPath + "/Mac_Server/qe_server");
}
}
//! Downloads the .sav file from the server using UnityWebRequest.
private IEnumerator DownloadWorld()
{
downloadingWorld = true;
string url = PlayerPrefs.GetString("serverURL") + "/world";
using (UnityWebRequest www = new UnityWebRequest(url))
{
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
UnityEngine.Debug.Log(www.error);
worldName = "Enter world name.";
videoPlayer.GetComponent<VP>().PlayVideo("QE_Title.webm",true,0);
downloadingWorld = false;
downloadPrompt = true;
}
else
{
string savePath = Path.Combine(Application.persistentDataPath, "SaveData");
string saveFileLocation = Path.Combine(savePath + "/" + worldName + ".sav");
Directory.CreateDirectory(savePath);
File.WriteAllText(saveFileLocation, www.downloadHandler.text);
UnityEngine.Debug.Log("World downloaded.");
FileBasedPrefs.SetWorldName(worldName);
PreStart();
}
}
downloadingWorld = false;
}
//! Confirms world selection and loads the world.
private void SelectWorld()
{
if (worldSelected == false && worldName != "Enter world name.")
{
worldList = PlayerPrefsX.GetPersistentStringArray("Worlds").ToList();
if (worldList.Count < 10)
{
if (!worldList.Contains(worldName))
{
worldList.Add(worldName);
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
if (hosting == false)
{
worldDownloadCoroutine = StartCoroutine(DownloadWorld());
}
else
{
FileBasedPrefs.SetWorldName(worldName);
worldSelectPrompt = true;
}
}
else
{
FileBasedPrefs.SetWorldName(worldName);
worldSelectPrompt = true;
}
}
else
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
if (hosting == false)
{
worldDownloadCoroutine = StartCoroutine(DownloadWorld());
}
else
{
FileBasedPrefs.SetWorldName(worldName);
PreStart();
}
}
else
{
FileBasedPrefs.SetWorldName(worldName);
PreStart();
}
}
}
else if (worldList.Contains(worldName))
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true && hosting == false)
{
worldDownloadCoroutine = StartCoroutine(DownloadWorld());
}
else
{
FileBasedPrefs.SetWorldName(worldName);
PreStart();
}
}
}
}
//! Selects the scene and starts the game.
private void PreStart()
{
if (FileBasedPrefs.GetBool(worldName + "sceneChangeRequired") == true)
{
scene = FileBasedPrefs.GetInt(worldName + "scene");
ChangeScene();
}
else
{
StartGame();
}
}
//! Called when Gliese 876 or Kepler-452b is selected and the scene needs to be changed.
private void ChangeScene()
{
FileBasedPrefs.SetBool(worldName + "sceneChangeRequired", true);
PlayerPrefsX.SetPersistentStringArray("Worlds", worldList.ToArray());
PlayerPrefsX.SetPersistentBool("changingWorld", true);
PlayerPrefs.SetString("worldName", worldName);
FileBasedPrefs.ManuallySave();
PlayerPrefs.Save();
SceneManager.LoadScene(scene);
}
//! Called when Gliese 876 is selected and the scene does not need to be changed.
private void StartGame()
{
FileBasedPrefs.SetBool(worldName + "sceneChangeRequired", false);
PlayerPrefsX.SetPersistentStringArray("Worlds", worldList.ToArray());
FileBasedPrefs.ManuallySave();
PlayerPrefs.Save();
stateManager.worldName = worldName;
worldSelected = true;
ambient.enabled = false;
}
//! Gets the size of in pixels of text so it can be positioned on the screen accordingly.
private Vector2 GetStringSize(string str)
{
GUIContent content = new GUIContent(str);
GUIStyle style = GUI.skin.box;
style.alignment = TextAnchor.MiddleCenter;
return style.CalcSize(content);
}
//! Returns true if the GUI is available.
private bool GuiFree()
{
return deletePrompt == false
&& worldSelectPrompt == false
&& escapePrompt == false
&& multiplayerPrompt == false
&& userNamePrompt == false
&& networkAddressPrompt == false
&& playerColorPrompt == false
&& localPrompt == false
&& downloadPrompt == false
&& publicPrompt == false
&& passwordPrompt == false
&& userNamePrompt == false;
}
//! Returns true if the world is currently being loaded.
private bool LoadingWorld()
{
return stateManager.worldLoaded == false || stateManager.GetComponent<GameManager>().working == true;
}
//! Called by unity engine for rendering and handling GUI events.
public void OnGUI()
{
// STYLE
GUI.skin = thisGUIskin;
// ASPECT RATIO
float ScreenHeight = Screen.height;
float ScreenWidth = Screen.width;
if (ScreenWidth / ScreenHeight < 1.7f)
{
ScreenHeight = (ScreenHeight * 0.75f);
notWideScreen = true;
}
if (ScreenHeight < 700)
{
GUI.skin.label.fontSize = 10;
}
Rect backgroundRect = new Rect(0, 0, Screen.width, Screen.height);
Rect textEntryRect = new Rect((ScreenWidth * 0.36f), (ScreenHeight * 0.235f), (ScreenWidth * 0.16f), (ScreenHeight * 0.03f));
Rect worldListBackgroundRect = new Rect((ScreenWidth * 0.40f), (ScreenHeight * 0.45f), (ScreenWidth * 0.17f), (ScreenHeight * 0.40f));
Rect worldListRect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.47f), (ScreenWidth * 0.15f), (ScreenHeight * 0.55f));
Rect worldListTitleRect = new Rect((ScreenWidth * 0.45f), (ScreenHeight * 0.46f), (ScreenWidth * 0.15f), (ScreenHeight * 0.55f));
Rect promptBackgroundRect = new Rect(((ScreenWidth / 2) - 300), ScreenHeight * 0.14f, 600, ScreenHeight * 0.20f);
Rect serverListBackgroundRect = new Rect((ScreenWidth * 0.01f), (ScreenHeight * 0.01f), (ScreenWidth * 0.23f), (ScreenHeight * 0.9f));
Rect serverListRect = new Rect((ScreenWidth * 0.015f), (ScreenHeight * 0.018f), (ScreenWidth * 0.22f), (ScreenHeight * 0.88f));
Rect deletePromptLabelRect = new Rect((ScreenWidth * 0.435f), (ScreenHeight * 0.18f), (ScreenWidth * 0.20f), (ScreenHeight * 0.05f));
Rect deletePromptButton1Rect = new Rect((ScreenWidth * 0.39f), (ScreenHeight * 0.22f), (ScreenWidth * 0.10f), (ScreenHeight * 0.05f));
Rect deletePromptButton2Rect = new Rect((ScreenWidth * 0.51f), (ScreenHeight * 0.22f), (ScreenWidth * 0.10f), (ScreenHeight * 0.05f));
Rect scenePromptButton1Rect = new Rect((ScreenWidth * 0.33f), (ScreenHeight * 0.22f), (ScreenWidth * 0.10f), (ScreenHeight * 0.05f));
Rect scenePromptButton2Rect = new Rect((ScreenWidth * 0.45f), (ScreenHeight * 0.22f), (ScreenWidth * 0.10f), (ScreenHeight * 0.05f));
Rect scenePromptButton3Rect = new Rect((ScreenWidth * 0.57f), (ScreenHeight * 0.22f), (ScreenWidth * 0.10f), (ScreenHeight * 0.05f));
Rect creativeButtonRect = new Rect((ScreenWidth / 2) - (ScreenWidth * 0.025f), (ScreenHeight * 0.30f), (ScreenWidth * 0.014f), (ScreenHeight * 0.0225f));
Rect creativeLabelrect = new Rect((ScreenWidth / 2), (ScreenHeight * 0.299f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect startGameButtonRect = new Rect(ScreenWidth * 0.58f, ScreenHeight * 0.4f, ScreenWidth * 0.15f, ScreenHeight * 0.03f);
Rect multiplayerButtonRect = new Rect(ScreenWidth * 0.58f, ScreenHeight * 0.44f, ScreenWidth * 0.15f, ScreenHeight * 0.03f);
Rect modioButtonRect = new Rect(ScreenWidth * 0.58f, ScreenHeight * 0.48f, ScreenWidth * 0.15f, ScreenHeight * 0.03f);
Rect textFieldRect = new Rect(ScreenWidth * 0.41f, ScreenHeight * 0.4f, ScreenWidth * 0.15f, ScreenHeight * 0.03f);
Rect loadingMessageRect = new Rect((ScreenWidth * 0.46f), (ScreenHeight * 0.30f), (ScreenWidth * 0.5f), (ScreenHeight * 0.5f));
Rect buttonRect1 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.50f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect2 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.532f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect3 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.564f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect4 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.596f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect5 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.628f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect6 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.660f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect7 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.692f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect8 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.724f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect9 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.756f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect buttonRect10 = new Rect((ScreenWidth * 0.405f), (ScreenHeight * 0.788f), (ScreenWidth * 0.020f), (ScreenHeight * 0.025f));
Rect world1Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.50f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world2Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.532f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world3Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.564f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world4Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.596f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world5Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.628f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world6Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.660f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world7Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.692f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world8Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.724f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world9Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.756f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
Rect world10Rect = new Rect((ScreenWidth * 0.43f), (ScreenHeight * 0.788f), (ScreenWidth * 0.12f), (ScreenHeight * 0.05f));
if (worldSelected == false)
{
if (waitForVideoTimer > 1 && playingVideo == false)
{
playingVideo = true;
}
if (playingVideo == false)
{
Texture2D bgTexture = notWideScreen ? titleTextureSD : titleTexture;
GUI.DrawTexture(backgroundRect, bgTexture);
waitForVideoTimer += 1 * Time.deltaTime;
}
else
{
GUI.DrawTexture(backgroundRect, title2Texture);
}
if (playerColorPrompt == false)
{
if (GUI.Button(startGameButtonRect, "SINGLE PLAYER"))
{
buttonSounds.Play();
if (worldName != "Enter world name.")
{
PlayerPrefsX.SetPersistentBool("multiplayer", false);
SelectWorld();
}
else
{
worldNamePrompt = true;
}
}
if (GUI.Button(multiplayerButtonRect, "MULTIPLAYER"))
{
buttonSounds.Play();
PlayerPrefsX.SetPersistentBool("multiplayer", true);
multiplayerPrompt = true;
}
if (GUI.Button(modioButtonRect, "MODS"))
{
buttonSounds.Play();
SceneManager.LoadScene(2);
}
GUI.DrawTexture(worldListBackgroundRect, worldListBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
GUI.Label(worldListTitleRect, "SAVED WORLDS");
GUI.skin.label.fontSize = f;
worlds = PlayerPrefsX.GetPersistentStringArray("Worlds");
if (worlds.Length > 0)
{
GUI.Label(world1Rect, "1. " + worlds[0]);
}
if (worlds.Length > 1)
{
GUI.Label(world2Rect, "2. " + worlds[1]);
}
if (worlds.Length > 2)
{
GUI.Label(world3Rect, "3. " + worlds[2]);
}
if (worlds.Length > 3)
{
GUI.Label(world4Rect, "4. " + worlds[3]);
}
if (worlds.Length > 4)
{
GUI.Label(world5Rect, "5. " + worlds[4]);
}
if (worlds.Length > 5)
{
GUI.Label(world6Rect, "6. " + worlds[5]);
}
if (worlds.Length > 6)
{
GUI.Label(world7Rect, "7. " + worlds[6]);
}
if (worlds.Length > 7)
{
GUI.Label(world8Rect, "8. " + worlds[7]);
}
if (worlds.Length > 8)
{
GUI.Label(world9Rect, "9. " + worlds[8]);
}
if (worlds.Length > 9)
{
GUI.Label(world10Rect, "10. " + worlds[9]);
}
worldName = GUI.TextField(textFieldRect, worldName, 20);
if (GUI.Button(buttonRect1, "1"))
{
buttonSounds.Play();
if (worlds.Length > 0)
{
worldName = worlds[0];
}
}
if (GUI.Button(buttonRect2, "2"))
{
buttonSounds.Play();
if (worlds.Length > 1)
{
worldName = worlds[1];
}
}
if (GUI.Button(buttonRect3, "3"))
{
buttonSounds.Play();
if (worlds.Length > 2)
{
worldName = worlds[2];
}
}
if (GUI.Button(buttonRect4, "4"))
{
buttonSounds.Play();
if (worlds.Length > 3)
{
worldName = worlds[3];
}
}
if (GUI.Button(buttonRect5, "5"))
{
buttonSounds.Play();
if (worlds.Length > 4)
{
worldName = worlds[4];
}
}
if (GUI.Button(buttonRect6, "6"))
{
buttonSounds.Play();
if (worlds.Length > 5)
{
worldName = worlds[5];
}
}
if (GUI.Button(buttonRect7, "7"))
{
buttonSounds.Play();
if (worlds.Length > 6)
{
worldName = worlds[6];
}
}
if (GUI.Button(buttonRect8, "8"))
{
buttonSounds.Play();
if (worlds.Length > 7)
{
worldName = worlds[7];
}
}
if (GUI.Button(buttonRect9, "9"))
{
buttonSounds.Play();
if (worlds.Length > 8)
{
worldName = worlds[8];
}
}
if (GUI.Button(buttonRect10, "10"))
{
buttonSounds.Play();
if (worlds.Length > 9)
{
worldName = worlds[9];
}
}
if (Input.GetKeyDown(KeyCode.Delete))
{
deletePrompt |= GuiFree();
buttonSounds.Play();
}
}
if (worldSelectPrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, worldListBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Choose location.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Choose location.");
GUI.skin.label.fontSize = f;
if (GUI.Button(scenePromptButton1Rect, "Kepler-1625"))
{
buttonSounds.Play();
StartGame();
}
if (GUI.Button(scenePromptButton2Rect, "Kepler-452b"))
{
buttonSounds.Play();
scene = 3;
FileBasedPrefs.SetInt(worldName + "scene", scene);
ChangeScene();
}
if (GUI.Button(scenePromptButton3Rect, "Gliese 876"))
{
buttonSounds.Play();
scene = 1;
FileBasedPrefs.SetInt(worldName + "scene", scene);
ChangeScene();
}
GUI.Label(creativeLabelrect, "Creative Mode");
bool creativeMode = FileBasedPrefs.GetBool(worldName + "creativeMode");
string check = creativeMode == true ? "X" : "";
if (GUI.Button(creativeButtonRect, check))
{
creativeMode = !creativeMode;
FileBasedPrefs.SetBool(worldName + "creativeMode", creativeMode);
}
}
if (deletePrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Delete selected world?");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Delete selected world?");
GUI.skin.label.fontSize = f;
if (GUI.Button(deletePromptButton1Rect, "Yes"))
{
buttonSounds.Play();
string savePath = Path.Combine(Application.persistentDataPath, "SaveData");
Directory.CreateDirectory(savePath);
string savFile = "SaveData/" + worldName + ".sav";
string bakFile = "SaveData/" + worldName + ".bak";
string savPath = Path.Combine(Application.persistentDataPath, savFile);
string bakPath = Path.Combine(Application.persistentDataPath, bakFile);
File.Delete(savPath);
File.Delete(bakPath);
worldList = worlds.ToList();
foreach (string w in worlds)
{
if (w == worldName)
{
worldList.Remove(w);
}
}
PlayerPrefsX.SetPersistentStringArray("Worlds", worldList.ToArray());
PlayerPrefs.Save();
deletePrompt = false;
}
if (GUI.Button(deletePromptButton2Rect, "No"))
{
buttonSounds.Play();
deletePrompt = false;
}
}
if (multiplayerPrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Multiplayer.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Multiplayer");
GUI.skin.label.fontSize = f;
if (GUI.Button(deletePromptButton1Rect, "Host"))
{
buttonSounds.Play();
hosting = true;
PlayerPrefsX.SetPersistentBool("hosting", true);
multiplayerPrompt = false;
localPrompt = true;
}
if (GUI.Button(deletePromptButton2Rect, "Join"))
{
buttonSounds.Play();
hosting = false;
PlayerPrefsX.SetPersistentBool("hosting", false);
PlayerPrefs.SetString("ip", GetExternalAddress());
GetServerList();
multiplayerPrompt = false;
networkAddressPrompt = true;
}
}
if (localPrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Multiplayer.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Multiplayer");
GUI.skin.label.fontSize = f;
if (GUI.Button(deletePromptButton1Rect, "Local"))
{
buttonSounds.Play();
local = true;
localPrompt = false;
userNamePrompt = true;
worldName = local == true ? GetLocalAddress() : GetExternalAddress();
PlayerPrefs.SetString("ip", GetLocalAddress());
PlayerPrefs.SetString("serverURL", "http://" + worldName + ":5000");
PlayerPrefsX.SetPersistentBool("announce", false);
StartServer();
}
if (GUI.Button(deletePromptButton2Rect, "Online"))
{
buttonSounds.Play();
local = false;
localPrompt = false;
publicPrompt = true;
worldName = local == true ? GetLocalAddress() : GetExternalAddress();
PlayerPrefs.SetString("ip", GetExternalAddress());
PlayerPrefs.SetString("serverURL", "http://" + worldName + ":5000");
StartServer();
}
}
if (publicPrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Multiplayer.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Multiplayer");
GUI.skin.label.fontSize = f;
if (GUI.Button(deletePromptButton1Rect, "Public"))
{
buttonSounds.Play();
publicPrompt = false;
userNamePrompt = true;
PlayerPrefsX.SetPersistentBool("announce", true);
}
if (GUI.Button(deletePromptButton2Rect, "Private"))
{
buttonSounds.Play();
publicPrompt = false;
userNamePrompt = true;
PlayerPrefsX.SetPersistentBool("announce", false);
}
}
if (networkAddressPrompt == true)
{
GUI.DrawTexture(serverListBackgroundRect, worldListBackground);
int textAreaFontSize = GUI.skin.textArea.fontSize;
GUI.skin.textArea.fontSize = 18;
string[] actualList;
string displayList = " Public Servers\n\n";
try
{
if (serverList.Split(':').Length > 1)
{
actualList = serverList.Split(':')[1].Split('[');
foreach (string serverEntry in actualList)
{
string serverInfo = serverEntry.Split(']')[0].Replace("\"","").Trim();
if (serverInfo != "")
{
displayList += serverInfo + " players\n\n";
}
}
}
}
catch(Exception e)
{
UnityEngine.Debug.Log("Error reading server list: " + e.Message);
}
GUI.TextArea(serverListRect, displayList);
GUI.skin.textArea.fontSize = textAreaFontSize;
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
GUI.DrawTexture(promptBackgroundRect, guiBackground);
Vector2 size = GetStringSize("Enter server IP address.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Enter server IP address.");
GUI.skin.label.fontSize = f;
serverURL = GUI.TextField(textEntryRect, serverURL, 30);
if (GUI.Button(scenePromptButton3Rect, "OK"))
{
buttonSounds.Play();
worldName = serverURL;
PlayerPrefs.SetString("serverURL", "http://" + serverURL + ":5000");
networkAddressPrompt = false;
userNamePrompt = true;
}
}
if (userNamePrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;
Vector2 size = GetStringSize("Enter user name.");
Rect messagePos = new Rect((Screen.width / 2) - (size.x / 2.2f), ScreenHeight * 0.18f, size.x, size.y);
GUI.Label(messagePos, "Enter user name.");
GUI.skin.label.fontSize = f;
username = GUI.TextField(textEntryRect, username, 30);
if (GUI.Button(scenePromptButton3Rect, "OK"))
{
buttonSounds.Play();
if (username != "" && username != "Enter user name.")
{
PlayerPrefs.SetString("UserName", username);
userNamePrompt = false;
passwordPrompt = true;
}
}
}
if (passwordPrompt == true)
{
GUI.DrawTexture(promptBackgroundRect, guiBackground);
int f = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 14;