Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class QueuePlayer : MonoBehaviour {
- //public
- //list
- [Header("Scripts")]
- public QueueManager queueManager;
- public List<PhotonPlayer> playerListInQueue;
- public List<Room> roomsAvailable;
- [Space(6)]
- [Header("Variables")]
- public int countOfPlayersOnQueue;
- public int countOfRooms;
- public bool foundGame;
- //function
- void Start(){
- //set
- playerListInQueue = new List<PhotonPlayer>();
- roomsAvailable = new List<Room>();
- }
- void Update(){
- //set
- countOfPlayersOnQueue = playerListInQueue.Count;
- countOfRooms = roomsAvailable.Count;
- if(queueManager.onQueue){
- StartCoroutine(StartJoining());
- } else {
- StopCoroutine(StartJoining());
- }
- }
- //methods
- //queue
- public void AddToQueue(PhotonPlayer player){
- playerListInQueue.Add(player);
- }
- public void RemoveFromQueue(PhotonPlayer player){
- playerListInQueue.Remove(player);
- }
- public void AddRoom(Room room){
- roomsAvailable.Add(room);
- }
- //other
- IEnumerator StartJoining(){
- JoinGame();
- yield return null;
- }
- void JoinGame(){
- RoomOptions newRoomOptions = new RoomOptions();
- newRoomOptions.MaxPlayers = 2;
- if(roomsAvailable.Count <= 0){
- Debug.Log("MAKING A NEW ROOM");
- Room newRoom = new Room("Game" + roomsAvailable.Count, newRoomOptions);
- AddRoom(newRoom);
- } else {
- for (int r = 0; r < roomsAvailable.Count; r++){
- if(roomsAvailable[r].open){
- Debug.Log("AVAILABLE ROOMS!!!");
- if (PhotonNetwork.inRoom){
- Debug.Log("JOINING GAME!!!");
- PhotonNetwork.LeaveRoom();
- foundGame = true;
- queueManager.Queue(false);
- RemoveFromQueue(PhotonNetwork.player);
- //will return true after we leave the room
- if(!PhotonNetwork.inRoom){
- Debug.Log("IN GAME ROOM");
- PhotonNetwork.JoinRoom(roomsAvailable[r].name);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement