using System.Collections; using System.Collections.Generic; using System.Linq; using ExitGames.Client.Photon; using ExitGames.Client.Photon.StructWrapping; using Photon.Pun; using Photon.Realtime; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class MainMenu : MonoBehaviourPunCallbacks, IOnEventCallback { public const byte MatchmadeEventCode = 1; public const byte StartMatchmakeEventCode = 2; public const byte StopMatchmakeEventCode = 3; public const byte ChangeGameModeEventCode= 4; #region overrides public override void OnRoomListUpdate(List roomList) { base.OnRoomListUpdate(roomList); RoomList = roomList; Debug.Log($"{RoomList.Count} rooms available"); } public override void OnConnectedToMaster() { base.OnConnectedToMaster(); Debug.Log("Connected to photon"); PhotonNetwork.JoinLobby(); PhotonNetwork.LocalPlayer.NickName = UserDataManager.instance.Username; } public override void OnJoinedLobby() { base.OnJoinedLobby(); PhotonNetwork.LocalPlayer.NickName = UserDataManager.instance.Username; if(queuedRoomname == "GAME_READY"){ Debug.Log($"Loading new match, roomname:{GameManager.RoomName}, mode:{GameManager.desiredGameMode}"); SceneManager.LoadScene("SampleScene"); return; } JoinQueuedRoom(); } public override void OnPlayerEnteredRoom(Player newPlayer) { base.OnPlayerEnteredRoom(newPlayer); UpdateLobbyPlayers(); } public override void OnPlayerLeftRoom(Player otherPlayer) { base.OnPlayerLeftRoom(otherPlayer); UpdateLobbyPlayers(); } public override void OnJoinedRoom() { base.OnJoinedRoom(); Debug.Log("Connected to new room : " + PhotonNetwork.CurrentRoom.Name); if(PhotonNetwork.CurrentRoom.PlayerCount >3 ){ Debug.Log("NO room for me!"); JoinAnotherRoom(myRoomname); } loadingPanel.SetActive(false); UpdateLobbyPlayers(); } #endregion public GameObject[] panels; public TMP_Text usernameTxt; public TMP_Text xpTxt; public Button btnPlay; public Button btnCancel; public Transform InviteListParent; public GameObject InviteListItemPrefab; public GameObject[] LobbyPlayerObjects; public TMP_Text[] LobbyPlayerNameTexts; public ComboSelection GameModeSelector; public string myRoomname; public GameObject loadingPanel; void Start() { loadingPanel.SetActive(true); DisableLobbyPlayers(); myRoomname = UserDataManager.instance.Username +"#"+UserDataManager.instance.UserID; queuedRoomname = myRoomname; if(!PhotonNetwork.IsConnected){ PhotonNetwork.ConnectUsingSettings(); }else{ if(PhotonNetwork.InLobby){PhotonNetwork.LeaveLobby();} Debug.Log("Already connected to photon, Joining lobby"); PhotonNetwork.JoinLobby(); } GameModeSelector.OnSelectedChanged.AddListener(OnSelectedGameModeChanged); RefreshUserdata(); UserDataManager.instance.OnInvitesUpdated = OnInvitesUpdated; btnPlay.onClick.AddListener(PlayClicked); btnCancel.onClick.AddListener(CancelClicked); } void Update(){ if(btnCancel.gameObject.activeSelf){ matchmakingTimer+=Time.deltaTime; btnCancel.transform.GetChild(1).GetComponent().text = Helpers.secondsToTimerString(matchmakingTimer); } } public string gameModeSelection => GameModeSelector.SelectedOption.ToLower(); void OnSelectedGameModeChanged(){ UpdateGameMode(gameModeSelection); if(PhotonNetwork.IsMasterClient){ PhotonNetwork.RaiseEvent(ChangeGameModeEventCode, gameModeSelection, EveryoneRaiseEventOptions, new SendOptions()); } } void UpdateGameMode(string val){ GameModes gameMode = GameModes.Deathmatch; if(val.ToLower() == "dm"){gameMode = GameModes.Deathmatch;} if(val.ToLower()== "tdm"){gameMode = GameModes.TeamDeathmatch;} if(val.ToLower() == "br"){gameMode = GameModes.BattleRoyale;} Debug.Log("Set game mode to : " + gameMode.ToString() + $" ({val})"); GameManager.desiredGameMode = gameMode.ToString(); UpdatePlayBtnInteractability(); } void OnInvitesUpdated(){ foreach(Transform t in InviteListParent.GetComponentsInChildren()){ if(t== InviteListParent){continue;} Destroy(t.gameObject); } for(int i=0; i < UserDataManager.instance.Invites.Count; i++){ Invite invite = UserDataManager.instance.Invites[i]; GameObject newInviteItem = Instantiate(InviteListItemPrefab, InviteListParent); newInviteItem.transform.GetChild(0).GetComponent().text = $"Lobby Invite from {invite.host}"; newInviteItem.transform.GetChild(1).GetComponent