Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class QueuePlayer : MonoBehaviour {
  6.  
  7.     //public
  8.     //list
  9.     [Header("Scripts")]
  10.     public QueueManager queueManager;
  11.  
  12.     public List<PhotonPlayer> playerListInQueue;
  13.     public List<Room> roomsAvailable;
  14.     [Space(6)]
  15.     [Header("Variables")]
  16.     public int countOfPlayersOnQueue;
  17.     public int countOfRooms;
  18.     public bool foundGame;
  19.     //function
  20.     void Start(){
  21.         //set
  22.         playerListInQueue = new List<PhotonPlayer>();
  23.         roomsAvailable = new List<Room>();
  24.     }
  25.     void Update(){
  26.         //set
  27.         countOfPlayersOnQueue = playerListInQueue.Count;
  28.         countOfRooms = roomsAvailable.Count;
  29.  
  30.         if(queueManager.onQueue){
  31.             StartCoroutine(StartJoining());
  32.         } else {
  33.             StopCoroutine(StartJoining());
  34.         }
  35.     }
  36.     //methods
  37.     //queue
  38.     public void AddToQueue(PhotonPlayer player){
  39.         playerListInQueue.Add(player);
  40.     }
  41.     public void RemoveFromQueue(PhotonPlayer player){
  42.         playerListInQueue.Remove(player);
  43.     }
  44.     public void AddRoom(Room room){
  45.         roomsAvailable.Add(room);
  46.     }
  47.     //other
  48.     IEnumerator StartJoining(){
  49.         JoinGame();
  50.         yield return null;
  51.     }
  52.     void JoinGame(){
  53.         RoomOptions newRoomOptions = new RoomOptions();
  54.         newRoomOptions.MaxPlayers = 2;
  55.         if(roomsAvailable.Count <= 0){
  56.             Debug.Log("MAKING A NEW ROOM");
  57.             Room newRoom = new Room("Game" + roomsAvailable.Count, newRoomOptions);
  58.             AddRoom(newRoom);
  59.         } else {
  60.             for (int r = 0; r < roomsAvailable.Count; r++){
  61.                 if(roomsAvailable[r].open){
  62.                     Debug.Log("AVAILABLE ROOMS!!!");
  63.                     if (PhotonNetwork.inRoom){
  64.                         Debug.Log("JOINING GAME!!!");
  65.                         PhotonNetwork.LeaveRoom();
  66.                         foundGame = true;
  67.                         queueManager.Queue(false);
  68.                         RemoveFromQueue(PhotonNetwork.player);
  69.                         //will return true after we leave the room
  70.                         if(!PhotonNetwork.inRoom){
  71.                             Debug.Log("IN GAME ROOM");
  72.                             PhotonNetwork.JoinRoom(roomsAvailable[r].name);
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement