123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using ExitGames.Client.Photon;
- using Photon.Realtime;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Photon.Pun.Demo.Asteroids
- {
- public class LobbyMainPanel : MonoBehaviourPunCallbacks
- {
- [Header("Login Panel")]
- public GameObject LoginPanel;
- public InputField PlayerNameInput;
- [Header("Selection Panel")]
- public GameObject SelectionPanel;
- [Header("Create Room Panel")]
- public GameObject CreateRoomPanel;
- public InputField RoomNameInputField;
- public InputField MaxPlayersInputField;
- [Header("Join Random Room Panel")]
- public GameObject JoinRandomRoomPanel;
- [Header("Room List Panel")]
- public GameObject RoomListPanel;
- public GameObject RoomListContent;
- public GameObject RoomListEntryPrefab;
- [Header("Inside Room Panel")]
- public GameObject InsideRoomPanel;
- public Button StartGameButton;
- public GameObject PlayerListEntryPrefab;
- private Dictionary<string, RoomInfo> cachedRoomList;
- private Dictionary<string, GameObject> roomListEntries;
- private Dictionary<int, GameObject> playerListEntries;
- #region UNITY
- public void Awake()
- {
- PhotonNetwork.AutomaticallySyncScene = true;
- cachedRoomList = new Dictionary<string, RoomInfo>();
- roomListEntries = new Dictionary<string, GameObject>();
-
- PlayerNameInput.text = "Player " + Random.Range(1000, 10000);
- }
- #endregion
- #region PUN CALLBACKS
- public override void OnConnectedToMaster()
- {
- this.SetActivePanel(SelectionPanel.name);
- }
- public override void OnRoomListUpdate(List<RoomInfo> roomList)
- {
- ClearRoomListView();
- UpdateCachedRoomList(roomList);
- UpdateRoomListView();
- }
- public override void OnJoinedLobby()
- {
- // whenever this joins a new lobby, clear any previous room lists
- cachedRoomList.Clear();
- ClearRoomListView();
- }
- // note: when a client joins / creates a room, OnLeftLobby does not get called, even if the client was in a lobby before
- public override void OnLeftLobby()
- {
- cachedRoomList.Clear();
- ClearRoomListView();
- }
- public override void OnCreateRoomFailed(short returnCode, string message)
- {
- SetActivePanel(SelectionPanel.name);
- }
- public override void OnJoinRoomFailed(short returnCode, string message)
- {
- SetActivePanel(SelectionPanel.name);
- }
- public override void OnJoinRandomFailed(short returnCode, string message)
- {
- string roomName = "Room " + Random.Range(1000, 10000);
- RoomOptions options = new RoomOptions {MaxPlayers = 8};
- PhotonNetwork.CreateRoom(roomName, options, null);
- }
- public override void OnJoinedRoom()
- {
- // joining (or entering) a room invalidates any cached lobby room list (even if LeaveLobby was not called due to just joining a room)
- cachedRoomList.Clear();
- SetActivePanel(InsideRoomPanel.name);
- if (playerListEntries == null)
- {
- playerListEntries = new Dictionary<int, GameObject>();
- }
- foreach (Player p in PhotonNetwork.PlayerList)
- {
- GameObject entry = Instantiate(PlayerListEntryPrefab);
- entry.transform.SetParent(InsideRoomPanel.transform);
- entry.transform.localScale = Vector3.one;
- entry.GetComponent<PlayerListEntry>().Initialize(p.ActorNumber, p.NickName);
- object isPlayerReady;
- if (p.CustomProperties.TryGetValue(AsteroidsGame.PLAYER_READY, out isPlayerReady))
- {
- entry.GetComponent<PlayerListEntry>().SetPlayerReady((bool) isPlayerReady);
- }
- playerListEntries.Add(p.ActorNumber, entry);
- }
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- Hashtable props = new Hashtable
- {
- {AsteroidsGame.PLAYER_LOADED_LEVEL, false}
- };
- PhotonNetwork.LocalPlayer.SetCustomProperties(props);
- }
- public override void OnLeftRoom()
- {
- SetActivePanel(SelectionPanel.name);
- foreach (GameObject entry in playerListEntries.Values)
- {
- Destroy(entry.gameObject);
- }
- playerListEntries.Clear();
- playerListEntries = null;
- }
- public override void OnPlayerEnteredRoom(Player newPlayer)
- {
- GameObject entry = Instantiate(PlayerListEntryPrefab);
- entry.transform.SetParent(InsideRoomPanel.transform);
- entry.transform.localScale = Vector3.one;
- entry.GetComponent<PlayerListEntry>().Initialize(newPlayer.ActorNumber, newPlayer.NickName);
- playerListEntries.Add(newPlayer.ActorNumber, entry);
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- }
- public override void OnPlayerLeftRoom(Player otherPlayer)
- {
- Destroy(playerListEntries[otherPlayer.ActorNumber].gameObject);
- playerListEntries.Remove(otherPlayer.ActorNumber);
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- }
- public override void OnMasterClientSwitched(Player newMasterClient)
- {
- if (PhotonNetwork.LocalPlayer.ActorNumber == newMasterClient.ActorNumber)
- {
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- }
- }
- public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
- {
- if (playerListEntries == null)
- {
- playerListEntries = new Dictionary<int, GameObject>();
- }
- GameObject entry;
- if (playerListEntries.TryGetValue(targetPlayer.ActorNumber, out entry))
- {
- object isPlayerReady;
- if (changedProps.TryGetValue(AsteroidsGame.PLAYER_READY, out isPlayerReady))
- {
- entry.GetComponent<PlayerListEntry>().SetPlayerReady((bool) isPlayerReady);
- }
- }
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- }
- #endregion
- #region UI CALLBACKS
- public void OnBackButtonClicked()
- {
- if (PhotonNetwork.InLobby)
- {
- PhotonNetwork.LeaveLobby();
- }
- SetActivePanel(SelectionPanel.name);
- }
- public void OnCreateRoomButtonClicked()
- {
- string roomName = RoomNameInputField.text;
- roomName = (roomName.Equals(string.Empty)) ? "Room " + Random.Range(1000, 10000) : roomName;
- byte maxPlayers;
- byte.TryParse(MaxPlayersInputField.text, out maxPlayers);
- maxPlayers = (byte) Mathf.Clamp(maxPlayers, 2, 8);
- RoomOptions options = new RoomOptions {MaxPlayers = maxPlayers, PlayerTtl = 10000 };
- PhotonNetwork.CreateRoom(roomName, options, null);
- }
- public void OnJoinRandomRoomButtonClicked()
- {
- SetActivePanel(JoinRandomRoomPanel.name);
- PhotonNetwork.JoinRandomRoom();
- }
- public void OnLeaveGameButtonClicked()
- {
- PhotonNetwork.LeaveRoom();
- }
- public void OnLoginButtonClicked()
- {
- string playerName = PlayerNameInput.text;
- if (!playerName.Equals(""))
- {
- PhotonNetwork.LocalPlayer.NickName = playerName;
- PhotonNetwork.ConnectUsingSettings();
- }
- else
- {
- Debug.LogError("Player Name is invalid.");
- }
- }
- public void OnRoomListButtonClicked()
- {
- if (!PhotonNetwork.InLobby)
- {
- PhotonNetwork.JoinLobby();
- }
- SetActivePanel(RoomListPanel.name);
- }
- public void OnStartGameButtonClicked()
- {
- PhotonNetwork.CurrentRoom.IsOpen = false;
- PhotonNetwork.CurrentRoom.IsVisible = false;
- PhotonNetwork.LoadLevel("DemoAsteroids-GameScene");
- }
- #endregion
- private bool CheckPlayersReady()
- {
- if (!PhotonNetwork.IsMasterClient)
- {
- return false;
- }
- foreach (Player p in PhotonNetwork.PlayerList)
- {
- object isPlayerReady;
- if (p.CustomProperties.TryGetValue(AsteroidsGame.PLAYER_READY, out isPlayerReady))
- {
- if (!(bool) isPlayerReady)
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- return true;
- }
-
- private void ClearRoomListView()
- {
- foreach (GameObject entry in roomListEntries.Values)
- {
- Destroy(entry.gameObject);
- }
- roomListEntries.Clear();
- }
- public void LocalPlayerPropertiesUpdated()
- {
- StartGameButton.gameObject.SetActive(CheckPlayersReady());
- }
- private void SetActivePanel(string activePanel)
- {
- LoginPanel.SetActive(activePanel.Equals(LoginPanel.name));
- SelectionPanel.SetActive(activePanel.Equals(SelectionPanel.name));
- CreateRoomPanel.SetActive(activePanel.Equals(CreateRoomPanel.name));
- JoinRandomRoomPanel.SetActive(activePanel.Equals(JoinRandomRoomPanel.name));
- RoomListPanel.SetActive(activePanel.Equals(RoomListPanel.name)); // UI should call OnRoomListButtonClicked() to activate this
- InsideRoomPanel.SetActive(activePanel.Equals(InsideRoomPanel.name));
- }
- private void UpdateCachedRoomList(List<RoomInfo> roomList)
- {
- foreach (RoomInfo info in roomList)
- {
- // Remove room from cached room list if it got closed, became invisible or was marked as removed
- if (!info.IsOpen || !info.IsVisible || info.RemovedFromList)
- {
- if (cachedRoomList.ContainsKey(info.Name))
- {
- cachedRoomList.Remove(info.Name);
- }
- continue;
- }
- // Update cached room info
- if (cachedRoomList.ContainsKey(info.Name))
- {
- cachedRoomList[info.Name] = info;
- }
- // Add new room info to cache
- else
- {
- cachedRoomList.Add(info.Name, info);
- }
- }
- }
- private void UpdateRoomListView()
- {
- foreach (RoomInfo info in cachedRoomList.Values)
- {
- GameObject entry = Instantiate(RoomListEntryPrefab);
- entry.transform.SetParent(RoomListContent.transform);
- entry.transform.localScale = Vector3.one;
- entry.GetComponent<RoomListEntry>().Initialize(info.Name, (byte)info.PlayerCount, (byte)info.MaxPlayers);
- roomListEntries.Add(info.Name, entry);
- }
- }
- }
- }
|