PRDarkSide

PhotonRoomCustomMatch

Sep 3rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.02 KB | None | 0 0
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9.  
  10. public class PhotonRoomCustomMatch : MonoBehaviourPunCallbacks, IInRoomCallbacks
  11. {
  12.     public static PhotonRoomCustomMatch room;
  13.     private PhotonView PV;
  14.  
  15.     public bool isGameLoaded;
  16.     public int currentScene;
  17.  
  18.     Player[] photonPlayers;
  19.     public int playersInRoom;
  20.     public int myNumberInRoom;
  21.  
  22.     public int enemyAmount;
  23.  
  24.     public int playerInGame;
  25.  
  26.     private bool readyToCount;
  27.     private bool readyToStart;
  28.     public float startingTime;
  29.     private float lessThanMaxPlayers;
  30.     private float atMaxPlayer;
  31.     private float timeToStart;
  32.  
  33.     public GameObject lobbyGO;
  34.     public GameObject roomGO;
  35.     public Transform playersPanel;
  36.     public GameObject playerListingPrefab;
  37.     public GameObject startButton;
  38.  
  39.     private void Awake()
  40.     {
  41.         if (PhotonRoomCustomMatch.room == null)
  42.         {
  43.             PhotonRoomCustomMatch.room = this;
  44.         }
  45.         else
  46.         {
  47.             if (PhotonRoomCustomMatch.room != this)
  48.             {
  49.                 Destroy(PhotonRoomCustomMatch.room.gameObject);
  50.                 PhotonRoomCustomMatch.room = this;
  51.             }
  52.         }
  53.         DontDestroyOnLoad(this.gameObject);
  54.     }
  55.  
  56.     public override void OnEnable()
  57.     {
  58.         base.OnEnable();
  59.         PhotonNetwork.AddCallbackTarget(this);
  60.         SceneManager.sceneLoaded += OnSceneFinishedLoading;
  61.     }
  62.  
  63.     public override void OnDisable()
  64.     {
  65.         base.OnDisable();
  66.         PhotonNetwork.RemoveCallbackTarget(this);
  67.         SceneManager.sceneLoaded -= OnSceneFinishedLoading;
  68.     }
  69.  
  70.     // Start is called before the first frame update
  71.     void Start()
  72.     {
  73.         PV = GetComponent<PhotonView>();
  74.         GameObject enemy = GameObject.FindWithTag("Enemy");
  75.         readyToCount = false;
  76.         readyToStart = false;
  77.         lessThanMaxPlayers = startingTime;
  78.         atMaxPlayer = 6;
  79.         timeToStart = startingTime;
  80.  
  81.     }
  82.  
  83.     // Update is called once per frame
  84.     void Update()
  85.     {
  86.         if (MultiplayerSetting.multiplayerSetting.delayStart)
  87.         {
  88.             if(playersInRoom == 1)
  89.             {
  90.                 RestartTimer();
  91.             }
  92.             if (!isGameLoaded)
  93.             {
  94.                 if (readyToStart)
  95.                 {
  96.                     atMaxPlayer -= Time.deltaTime;
  97.                     lessThanMaxPlayers = atMaxPlayer;
  98.                     timeToStart = atMaxPlayer;
  99.                 }
  100.                 else if (readyToCount)
  101.                 {
  102.                     lessThanMaxPlayers -= Time.deltaTime;
  103.                     timeToStart = lessThanMaxPlayers;
  104.                 }
  105.                 Debug.Log("Display time to start to the players" + timeToStart);
  106.                 if (timeToStart <= 0)
  107.                 {
  108.                     StartGame();
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     public override void OnJoinedRoom()
  115.     {
  116.         base.OnJoinedRoom();
  117.         Debug.Log("We are now in a room");
  118.  
  119.         lobbyGO.SetActive(false);
  120.         roomGO.SetActive(true);
  121.         if (PhotonNetwork.IsMasterClient)
  122.         {
  123.             startButton.SetActive(true);
  124.         }
  125.         ClearPlayerListings();
  126.         ListPlayers();
  127.  
  128.         photonPlayers = PhotonNetwork.PlayerList;
  129.         playersInRoom = photonPlayers.Length;
  130.         myNumberInRoom = playersInRoom;
  131.        
  132.         if (MultiplayerSetting.multiplayerSetting.delayStart)
  133.         {
  134.             Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiplayerSetting.multiplayerSetting.maxPlayers + ")");
  135.             if (playersInRoom > 1)
  136.             {
  137.                 readyToCount = true;
  138.             }
  139.             if (playersInRoom == MultiplayerSetting.multiplayerSetting.maxPlayers)
  140.             {
  141.                 readyToStart = true;
  142.                 if (!PhotonNetwork.IsMasterClient)
  143.                     return;
  144.                 PhotonNetwork.CurrentRoom.IsOpen = false;
  145.             }
  146.         }
  147.     }
  148.  
  149.     void ClearPlayerListings()
  150.     {
  151.         for(int i = playersPanel.childCount -1; i >= 0; i--)
  152.         {
  153.             Destroy(playersPanel.GetChild(i).gameObject);
  154.         }
  155.     }
  156.  
  157.     void ListPlayers()
  158.     {
  159.         if (PhotonNetwork.InRoom)
  160.         {
  161.             foreach(Player player in PhotonNetwork.PlayerList)
  162.             {
  163.                 GameObject tempListing = Instantiate(playerListingPrefab, playersPanel);
  164.                 Text tempText = tempListing.transform.GetChild(0).GetComponent<Text>();
  165.                 tempText.text = player.NickName;
  166.             }
  167.         }
  168.     }
  169.  
  170.     public override void OnPlayerEnteredRoom(Player newPlayer)
  171.     {
  172.         base.OnPlayerEnteredRoom(newPlayer);
  173.         Debug.Log("A new player has joined the room");
  174.         ClearPlayerListings();
  175.         ListPlayers();
  176.         photonPlayers = PhotonNetwork.PlayerList;
  177.         playersInRoom++;
  178.  
  179.         if (MultiplayerSetting.multiplayerSetting.delayStart)
  180.         {
  181.             Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiplayerSetting.multiplayerSetting.maxPlayers + ")");
  182.             if (playersInRoom > 1)
  183.             {
  184.                 readyToCount = true;
  185.             }
  186.             if (playersInRoom == MultiplayerSetting.multiplayerSetting.maxPlayers)
  187.             {
  188.                 readyToStart = true;
  189.                 if (!PhotonNetwork.IsMasterClient)
  190.                     return;
  191.                 PhotonNetwork.CurrentRoom.IsOpen = false;
  192.             }
  193.         }
  194.     }
  195.  
  196.     public void StartGame()
  197.     {
  198.         isGameLoaded = true;
  199.         if (!PhotonNetwork.IsMasterClient)
  200.             return;
  201.         if (MultiplayerSetting.multiplayerSetting.delayStart)
  202.         {
  203.             PhotonNetwork.CurrentRoom.IsOpen = false;
  204.         }
  205.         PhotonNetwork.LoadLevel(MultiplayerSetting.multiplayerSetting.gameScene);
  206.  
  207.        
  208.     }
  209.  
  210.     void RestartTimer()
  211.     {
  212.         lessThanMaxPlayers = startingTime;
  213.         timeToStart = startingTime;
  214.         atMaxPlayer = 6;
  215.         readyToCount = false;
  216.         readyToStart = false;
  217.     }
  218.  
  219.     void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
  220.     {
  221.         currentScene = scene.buildIndex;
  222.         if (currentScene == MultiplayerSetting.multiplayerSetting.gameScene)
  223.         {
  224.             isGameLoaded = true;
  225.  
  226.             if (MultiplayerSetting.multiplayerSetting.delayStart)
  227.             {
  228.                 PV.RPC("RPC_LoadedGameScene", RpcTarget.MasterClient);
  229.             }
  230.             else
  231.             {
  232.                 RPC_CreatePlayer();
  233.  
  234.                 if (PhotonNetwork.IsMasterClient)
  235.                 {
  236.                     RPC_CreateEnemies();
  237.                 }
  238.             }
  239.         }
  240.     }
  241.  
  242.     [PunRPC]
  243.     private void RPC_LoadedGameScene()
  244.     {
  245.         playerInGame++;
  246.         if (playersInRoom == PhotonNetwork.PlayerList.Length)
  247.         {
  248.             PV.RPC("RPC_CreatePlayer", RpcTarget.All);
  249.             if (!PV.IsMine)
  250.             {
  251.                 Destroy(gameObject.GetComponent<PlayerMovement>());
  252.             }
  253.         }
  254.     }
  255.  
  256.     [PunRPC]
  257.     private void RPC_CreatePlayer()
  258.     {
  259.         PhotonNetwork.Instantiate(Path.Combine("Prefabs", "PhotonNetworkPlayer"), transform.position, Quaternion.identity, 0);
  260.     }
  261.  
  262.     [PunRPC]
  263.     private void RPC_CreateEnemies()
  264.     {
  265.         for (int i = 0; i < enemyAmount; i++)
  266.         {
  267.             PhotonNetwork.Instantiate(Path.Combine("Prefabs", "PhotonNetworkEnemy"), transform.position, Quaternion.identity, 0);
  268.         }
  269.        
  270.     }
  271.  
  272.     public override void OnPlayerLeftRoom(Player otherPlayer)
  273.     {
  274.         base.OnPlayerLeftRoom(otherPlayer);
  275.         PhotonNetwork.DestroyPlayerObjects(otherPlayer);
  276.         Debug.Log(otherPlayer.NickName + "has left the game");
  277.         playersInRoom--;
  278.         ClearPlayerListings();
  279.         ListPlayers();
  280.     }
  281. }
Add Comment
Please, Sign In to add comment