|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Photon.Pun;
|
|
|
|
|
using Photon.Realtime;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class LobbyManager : MonoBehaviourPunCallbacks {
|
|
|
|
|
|
|
|
|
|
//Lobby Manager Singleton
|
|
|
|
|
public static LobbyManager Instance {
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Buttons
|
|
|
|
|
public GameObject connectButton;
|
|
|
|
|
public GameObject connectingButton;
|
|
|
|
|
public GameObject createRoomButton;
|
|
|
|
|
|
|
|
|
|
//Panels
|
|
|
|
|
public GameObject creatingRoomPanel;
|
|
|
|
|
|
|
|
|
|
//Menues
|
|
|
|
|
public GameObject nameInputMenu;
|
|
|
|
|
public GameObject roomSetupMenu;
|
|
|
|
|
public GameObject roomMenu;
|
|
|
|
|
|
|
|
|
|
//Inputs
|
|
|
|
|
public InputField playerNameInput;
|
|
|
|
|
public InputField roomNameInput;
|
|
|
|
|
|
|
|
|
|
//Scripts
|
|
|
|
|
public GUIController gui;
|
|
|
|
|
public RoomLayoutGroup roomLayoutGroup;
|
|
|
|
|
|
|
|
|
|
private RoomState state;
|
|
|
|
|
|
|
|
|
|
//Current List of available rooms in the default lobby
|
|
|
|
|
private readonly List<RoomInfo> roomList = new List<RoomInfo>();
|
|
|
|
|
|
|
|
|
|
private void Awake() {
|
|
|
|
|
Instance = this;
|
|
|
|
|
state = RoomState.NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnConnectButtonClicked() {
|
|
|
|
|
Debug.Log("Connect Button clicked");
|
|
|
|
|
|
|
|
|
|
PhotonNetwork.ConnectUsingSettings();
|
|
|
|
|
PhotonNetwork.SendRate = MultiplayerSettings.Instance.sendRate;
|
|
|
|
|
PhotonNetwork.SerializationRate = MultiplayerSettings.Instance.serializationRate;
|
|
|
|
|
|
|
|
|
|
connectButton.SetActive(false);
|
|
|
|
|
connectingButton.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCreateRoomButtonClicked() {
|
|
|
|
|
Debug.Log("Create Room Button clicked");
|
|
|
|
|
|
|
|
|
|
string roomName = roomNameInput.text;
|
|
|
|
|
TryCreateRoom(roomName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnJoinRoomButtonClicked(string roomName) {
|
|
|
|
|
Debug.Log("Trying to join room: " + roomName);
|
|
|
|
|
|
|
|
|
|
PhotonNetwork.JoinRoom(roomName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBackButtonClicked() {
|
|
|
|
|
Debug.Log("Back Button clicked");
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case RoomState.ROOM:
|
|
|
|
|
PhotonNetwork.LeaveRoom();
|
|
|
|
|
break;
|
|
|
|
|
case RoomState.ROOM_SETUP:
|
|
|
|
|
PhotonNetwork.Disconnect();
|
|
|
|
|
break;
|
|
|
|
|
case RoomState.NAME:
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
gui.OpenMenu();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnConnectedToMaster() {
|
|
|
|
|
PhotonNetwork.AutomaticallySyncScene = true;
|
|
|
|
|
PhotonNetwork.LocalPlayer.NickName = playerNameInput.text;
|
|
|
|
|
PhotonNetwork.JoinLobby(TypedLobby.Default);
|
|
|
|
|
|
|
|
|
|
nameInputMenu.SetActive(false);
|
|
|
|
|
roomSetupMenu.SetActive(true);
|
|
|
|
|
|
|
|
|
|
roomNameInput.text = PhotonNetwork.NickName + "'s Room";
|
|
|
|
|
|
|
|
|
|
state = RoomState.ROOM_SETUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnDisconnected(DisconnectCause cause) {
|
|
|
|
|
connectingButton.SetActive(false);
|
|
|
|
|
connectButton.SetActive(true);
|
|
|
|
|
|
|
|
|
|
roomSetupMenu.SetActive(false);
|
|
|
|
|
nameInputMenu.SetActive(true);
|
|
|
|
|
|
|
|
|
|
state = RoomState.NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnJoinedLobby() {
|
|
|
|
|
Debug.Log("Joined default lobby");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnJoinedRoom() {
|
|
|
|
|
Debug.Log("You successfully joined a room");
|
|
|
|
|
|
|
|
|
|
roomSetupMenu.SetActive(false);
|
|
|
|
|
roomMenu.SetActive(true);
|
|
|
|
|
RoomManager.Instance.OnJoinedRoom();
|
|
|
|
|
|
|
|
|
|
state = RoomState.ROOM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnLeftRoom() {
|
|
|
|
|
Debug.Log("You successfully left a room");
|
|
|
|
|
|
|
|
|
|
RoomManager.Instance.OnLeftRoom();
|
|
|
|
|
roomMenu.SetActive(false);
|
|
|
|
|
roomSetupMenu.SetActive(true);
|
|
|
|
|
|
|
|
|
|
state = RoomState.ROOM_SETUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnPlayerLeftRoom(Player otherPlayer) {
|
|
|
|
|
Debug.Log("Player " + otherPlayer.NickName + " (" + otherPlayer.ActorNumber + ") left the room");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnCreatedRoom() {
|
|
|
|
|
Debug.Log("You successfully created a room");
|
|
|
|
|
|
|
|
|
|
creatingRoomPanel.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnCreateRoomFailed(short returnCode, string message) {
|
|
|
|
|
Debug.Log("You failed to create a room");
|
|
|
|
|
|
|
|
|
|
creatingRoomPanel.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnRoomListUpdate(List<RoomInfo> roomList) {
|
|
|
|
|
Debug.Log("Room List Update");
|
|
|
|
|
|
|
|
|
|
ChacheRoomList(roomList);
|
|
|
|
|
roomLayoutGroup.OnReceivedRoomListUpdate(this.roomList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryCreateRoom(string name) {
|
|
|
|
|
Debug.Log("Trying to create a room");
|
|
|
|
|
|
|
|
|
|
creatingRoomPanel.SetActive(true);
|
|
|
|
|
RoomOptions roomOps = new RoomOptions() {
|
|
|
|
|
IsVisible = true,
|
|
|
|
|
IsOpen = true,
|
|
|
|
|
MaxPlayers = (byte)MultiplayerSettings.Instance.maxPlayers,
|
|
|
|
|
CleanupCacheOnLeave = false
|
|
|
|
|
};
|
|
|
|
|
PhotonNetwork.CreateRoom(name, roomOps, TypedLobby.Default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The updated List form Photon only contains changed rooms, so we
|
|
|
|
|
* must cache our actual list of available rooms
|
|
|
|
|
*/
|
|
|
|
|
private void ChacheRoomList(List<RoomInfo> updatedList) {
|
|
|
|
|
|
|
|
|
|
//First, see for changed and/or added roomInfo
|
|
|
|
|
//This information can also contain empty, closed and hidden rooms (removed later)
|
|
|
|
|
foreach (RoomInfo roomInfo in updatedList) {
|
|
|
|
|
|
|
|
|
|
//Remove outdated roomLists which are updated in the updatedList
|
|
|
|
|
roomList.RemoveAll(room => room.Name == roomInfo.Name);
|
|
|
|
|
|
|
|
|
|
//Add the updated roomInfo to my roomList
|
|
|
|
|
roomList.Add(roomInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Second, remove not needed (empty, closed, hidden) roomInfos
|
|
|
|
|
roomList.RemoveAll(room => !room.IsOpen || !room.IsVisible || room.PlayerCount == 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum RoomState {
|
|
|
|
|
NAME,
|
|
|
|
|
ROOM_SETUP,
|
|
|
|
|
ROOM
|
|
|
|
|
}
|