-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLauncher.cs
310 lines (255 loc) · 6.77 KB
/
Launcher.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
public class Launcher : MonoBehaviourPunCallbacks
{
#region Fields & Properties
public static Launcher Instance;
[Header("Loading Screen")]
[SerializeField] GameObject _loadingScreen;
[SerializeField] GameObject _menuButtons;
[SerializeField] TMP_Text _loadingText;
[Header("Create Room Screen")]
[SerializeField] GameObject _createRoomScreen;
[SerializeField] TMP_InputField _roomNameInput;
[Header("Room Screen")]
[SerializeField] GameObject _roomScreen;
[SerializeField] GameObject _startGameButton;
[SerializeField] TMP_Text _roomNameText, _playerNameLabel;
List<TMP_Text> _allPlayerNames = new List<TMP_Text>();
[Header("Error Screen")]
[SerializeField] GameObject _errorScreen;
[SerializeField] TMP_Text _errorText;
[Header("Room Browser Screen")]
[SerializeField] GameObject _roomBrowserScreen;
[SerializeField] RoomButton _theRoomButton;
List<RoomButton> _allRoomButtons = new List<RoomButton>();
[Header("Name Input Screen")]
[SerializeField] GameObject _nameInputScreen;
[SerializeField] TMP_InputField _nameInput;
public static bool _hasSetNickname;
[Header("Misc")]
public string[] _allMaps;
public bool _changeMapsBetweenMaps = true;
[SerializeField] GameObject _roomTestButton;
[SerializeField] string _gameScene;
#endregion
#region Getters
#endregion
#region Unity Methods
void Awake()
{
if (Instance == null)
{
Instance = this;
//DontDestroyOnLoad(gameObject);
}
else if (Instance != this)
Destroy(gameObject);
}
void Start()
{
CloseMenus();
_loadingScreen.SetActive(true);
_loadingText.text = "Connecting to Network...";
if(!PhotonNetwork.IsConnected)
PhotonNetwork.ConnectUsingSettings();
#if UNITY_EDITOR
_roomTestButton.SetActive(true);
#endif
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
#endregion
#region Photon Callbacks
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
PhotonNetwork.AutomaticallySyncScene = true;
_loadingText.text = "Joining Lobby...";
}
public override void OnJoinedLobby()
{
CloseMenus();
_menuButtons.SetActive(true);
//PhotonNetwork.NickName = $"Player{Random.Range(0, 1001)}";
if (!_hasSetNickname)
{
CloseMenus();
_nameInputScreen.SetActive(true);
if (PlayerPrefs.HasKey("PlayerName"))
_nameInput.text = PlayerPrefs.GetString("PlayerName");
}
else
{
PhotonNetwork.NickName = PlayerPrefs.GetString("PlayerName");
}
}
public override void OnJoinedRoom()
{
CloseMenus();
_roomScreen.SetActive(true);
_roomNameText.text = PhotonNetwork.CurrentRoom.Name;
ListAllPlayers();
if (PhotonNetwork.IsMasterClient)
_startGameButton.SetActive(true);
else
_startGameButton.SetActive(false);
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
_errorText.text = $"Failed to Create the Room: {message}";
CloseMenus();
_errorScreen.SetActive(true);
}
public override void OnLeftRoom()
{
CloseMenus();
_menuButtons.SetActive(true);
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
foreach (RoomButton roomButton in _allRoomButtons)
Destroy(roomButton.gameObject);
_allRoomButtons.Clear();
_theRoomButton.gameObject.SetActive(false);
for (int i = 0; i < roomList.Count; i++)
{
if (roomList[i].PlayerCount != roomList[i].MaxPlayers && !roomList[i].RemovedFromList)
{
RoomButton newButton = Instantiate(_theRoomButton, _theRoomButton.transform.parent);
newButton.SetButtonDetails(roomList[i]);
newButton.gameObject.SetActive(true);
_allRoomButtons.Add(newButton);
}
}
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
TMP_Text newPlayerLabel = Instantiate(_playerNameLabel, _playerNameLabel.transform.parent);
newPlayerLabel.text = newPlayer.NickName;
newPlayerLabel.gameObject.SetActive(true);
_allPlayerNames.Add(newPlayerLabel);
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
ListAllPlayers();
}
public override void OnMasterClientSwitched(Player newMasterClient)
{
if (PhotonNetwork.IsMasterClient)
_startGameButton.SetActive(true);
else
_startGameButton.SetActive(false);
}
#endregion
#region Public Methods
public void OpenCreateRoom()
{
CloseMenus();
_createRoomScreen.SetActive(true);
}
public void CreateRoom()
{
if (!string.IsNullOrEmpty(_roomNameInput.text))
{
RoomOptions options = new RoomOptions();
options.MaxPlayers = 8;
PhotonNetwork.CreateRoom(_roomNameInput.text, options);
CloseMenus();
_loadingText.text = "Creating Room...";
_loadingScreen.SetActive(true);
}
}
public void CloseErrorScreen()
{
CloseMenus();
_menuButtons.SetActive(true);
}
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
CloseMenus();
_loadingText.text = "Leaving Room...";
_loadingScreen.SetActive(true);
}
public void OpenRoomBrowser()
{
CloseMenus();
_roomBrowserScreen.SetActive(true);
}
public void CloseRoomBrowser()
{
CloseMenus();
_menuButtons.SetActive(true);
}
public void JoinRoom(RoomInfo inputInfo)
{
PhotonNetwork.JoinRoom(inputInfo.Name);
CloseMenus();
_loadingText.text = "Joining Room...";
_loadingScreen.SetActive(true);
}
public void SetNickname()
{
if (!string.IsNullOrEmpty(_nameInput.text))
{
PhotonNetwork.NickName = _nameInput.text;
PlayerPrefs.SetString("PlayerName", _nameInput.text);
CloseMenus();
_menuButtons.SetActive(true);
_hasSetNickname = true;
}
}
public void StartGame()
{
//PhotonNetwork.LoadLevel(_gameScene);
PhotonNetwork.LoadLevel(_allMaps[Random.Range(0, _allMaps.Length)]);
}
public void QuitGame()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
Application.Quit();
}
public void QuickJoin()
{
RoomOptions options = new RoomOptions();
options.MaxPlayers = 8;
PhotonNetwork.CreateRoom("Test", options);
CloseMenus();
_loadingText.text = "Creating Test Room...";
_loadingScreen.SetActive(true);
}
#endregion
#region Private Methods
void CloseMenus()
{
_loadingScreen.SetActive(false);
_menuButtons.SetActive(false);
_createRoomScreen.SetActive(false);
_roomScreen.SetActive(false);
_errorScreen.SetActive(false);
_roomBrowserScreen.SetActive(false);
_nameInputScreen.SetActive(false);
}
void ListAllPlayers()
{
foreach (TMP_Text player in _allPlayerNames)
Destroy(player.gameObject);
_allPlayerNames.Clear();
Player[] players = PhotonNetwork.PlayerList;
for (int i = 0; i < players.Length; i++)
{
TMP_Text newPlayerLabel = Instantiate(_playerNameLabel, _playerNameLabel.transform.parent);
newPlayerLabel.text = players[i].NickName;
newPlayerLabel.gameObject.SetActive(true);
_allPlayerNames.Add(newPlayerLabel);
}
}
#endregion
}