You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.9 KiB
114 lines
2.9 KiB
using ExitGames.Client.Photon;
|
|
using Photon.Pun;
|
|
using Photon.Realtime;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class RoomManager : MonoBehaviourPunCallbacks {
|
|
|
|
//GameObjects
|
|
public GameObject countdownPanel;
|
|
|
|
//Room Manager Singleton
|
|
public static RoomManager Instance {
|
|
get; set;
|
|
}
|
|
|
|
//Buttons
|
|
public Button startButton;
|
|
|
|
//Texts
|
|
public Text roomTitle;
|
|
|
|
//Scripts
|
|
public PlayerSlotManager playerSlotManager;
|
|
public StartCountdown startCountdown;
|
|
|
|
private PhotonView pv;
|
|
|
|
private void Awake() {
|
|
Instance = this;
|
|
pv = GetComponent<PhotonView>();
|
|
}
|
|
|
|
public void OnStartButtonClicked() {
|
|
Debug.Log("Start Button clicked");
|
|
|
|
PhotonNetwork.CurrentRoom.IsVisible = false;
|
|
PhotonNetwork.CurrentRoom.IsOpen = false;
|
|
pv.RPC("RpcStartCountdown", RpcTarget.All);
|
|
}
|
|
|
|
//Called by PhotonLobby
|
|
new public void OnJoinedRoom() {
|
|
playerSlotManager.OnJoinedRoom();
|
|
CheckInterface();
|
|
}
|
|
|
|
//Called by PhotonLobby
|
|
new public void OnLeftRoom() {
|
|
playerSlotManager.Clear();
|
|
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable());
|
|
}
|
|
|
|
public override void OnPlayerEnteredRoom(Player newPlayer) {
|
|
Debug.Log("Player " + newPlayer.NickName + " entered the room");
|
|
|
|
playerSlotManager.OnPlayerEnteredRoom(newPlayer);
|
|
CheckInterface();
|
|
}
|
|
|
|
public override void OnPlayerLeftRoom(Player otherPlayer) {
|
|
Debug.Log("Player " + otherPlayer.NickName + " left the room");
|
|
|
|
playerSlotManager.OnPlayerLeftRoom(otherPlayer);
|
|
CheckInterface();
|
|
}
|
|
|
|
public override void OnMasterClientSwitched(Player newMasterClient) {
|
|
Debug.Log("Master client switched to: " + newMasterClient.ToStringFull());
|
|
|
|
playerSlotManager.OnMasterClientSwitched(newMasterClient);
|
|
CheckInterface();
|
|
}
|
|
|
|
public override void OnPlayerPropertiesUpdate(Player target, Hashtable changedProps) {
|
|
playerSlotManager.OnPlayerPropertiesUpdate(target, changedProps);
|
|
}
|
|
|
|
[PunRPC]
|
|
private void RpcStartCountdown() {
|
|
Debug.Log("Starting countdown...");
|
|
|
|
countdownPanel.SetActive(true);
|
|
startCountdown.StartCount(MultiplayerSettings.Instance.startTime);
|
|
}
|
|
|
|
public void StartSelection() {
|
|
Debug.Log("Started loading selection scene...");
|
|
|
|
//Reset IsReady property for later usage
|
|
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable() { ["IsReady"] = false });
|
|
|
|
//Every client loads Selection Scene itself
|
|
PhotonNetwork.LoadLevel(1);
|
|
}
|
|
|
|
private void CheckInterface() {
|
|
bool master = PhotonNetwork.IsMasterClient;
|
|
int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
|
|
int minPlayers = MultiplayerSettings.Instance.minPlayers;
|
|
int maxPlayers = MultiplayerSettings.Instance.maxPlayers;
|
|
bool interactable = playerSlotManager.IsEveryoneReady() &&
|
|
playerCount >= minPlayers && playerCount <= maxPlayers && master;
|
|
|
|
startButton.interactable = interactable;
|
|
roomTitle.text = PhotonNetwork.CurrentRoom.Name;
|
|
}
|
|
|
|
private void Update() {
|
|
CheckInterface();
|
|
}
|
|
|
|
}
|
|
|