Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Photon.Pun;
- using Photon.Realtime;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class PhotonRoomCustomMatch : MonoBehaviourPunCallbacks, IInRoomCallbacks
- {
- public static PhotonRoomCustomMatch room;
- private PhotonView PV;
- public bool isGameLoaded;
- public int currentScene;
- Player[] photonPlayers;
- public int playersInRoom;
- public int myNumberInRoom;
- public int enemyAmount;
- public int playerInGame;
- private bool readyToCount;
- private bool readyToStart;
- public float startingTime;
- private float lessThanMaxPlayers;
- private float atMaxPlayer;
- private float timeToStart;
- public GameObject lobbyGO;
- public GameObject roomGO;
- public Transform playersPanel;
- public GameObject playerListingPrefab;
- public GameObject startButton;
- private void Awake()
- {
- if (PhotonRoomCustomMatch.room == null)
- {
- PhotonRoomCustomMatch.room = this;
- }
- else
- {
- if (PhotonRoomCustomMatch.room != this)
- {
- Destroy(PhotonRoomCustomMatch.room.gameObject);
- PhotonRoomCustomMatch.room = this;
- }
- }
- DontDestroyOnLoad(this.gameObject);
- }
- public override void OnEnable()
- {
- base.OnEnable();
- PhotonNetwork.AddCallbackTarget(this);
- SceneManager.sceneLoaded += OnSceneFinishedLoading;
- }
- public override void OnDisable()
- {
- base.OnDisable();
- PhotonNetwork.RemoveCallbackTarget(this);
- SceneManager.sceneLoaded -= OnSceneFinishedLoading;
- }
- // Start is called before the first frame update
- void Start()
- {
- PV = GetComponent<PhotonView>();
- GameObject enemy = GameObject.FindWithTag("Enemy");
- readyToCount = false;
- readyToStart = false;
- lessThanMaxPlayers = startingTime;
- atMaxPlayer = 6;
- timeToStart = startingTime;
- }
- // Update is called once per frame
- void Update()
- {
- if (MultiplayerSetting.multiplayerSetting.delayStart)
- {
- if(playersInRoom == 1)
- {
- RestartTimer();
- }
- if (!isGameLoaded)
- {
- if (readyToStart)
- {
- atMaxPlayer -= Time.deltaTime;
- lessThanMaxPlayers = atMaxPlayer;
- timeToStart = atMaxPlayer;
- }
- else if (readyToCount)
- {
- lessThanMaxPlayers -= Time.deltaTime;
- timeToStart = lessThanMaxPlayers;
- }
- Debug.Log("Display time to start to the players" + timeToStart);
- if (timeToStart <= 0)
- {
- StartGame();
- }
- }
- }
- }
- public override void OnJoinedRoom()
- {
- base.OnJoinedRoom();
- Debug.Log("We are now in a room");
- lobbyGO.SetActive(false);
- roomGO.SetActive(true);
- if (PhotonNetwork.IsMasterClient)
- {
- startButton.SetActive(true);
- }
- ClearPlayerListings();
- ListPlayers();
- photonPlayers = PhotonNetwork.PlayerList;
- playersInRoom = photonPlayers.Length;
- myNumberInRoom = playersInRoom;
- if (MultiplayerSetting.multiplayerSetting.delayStart)
- {
- Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiplayerSetting.multiplayerSetting.maxPlayers + ")");
- if (playersInRoom > 1)
- {
- readyToCount = true;
- }
- if (playersInRoom == MultiplayerSetting.multiplayerSetting.maxPlayers)
- {
- readyToStart = true;
- if (!PhotonNetwork.IsMasterClient)
- return;
- PhotonNetwork.CurrentRoom.IsOpen = false;
- }
- }
- }
- void ClearPlayerListings()
- {
- for(int i = playersPanel.childCount -1; i >= 0; i--)
- {
- Destroy(playersPanel.GetChild(i).gameObject);
- }
- }
- void ListPlayers()
- {
- if (PhotonNetwork.InRoom)
- {
- foreach(Player player in PhotonNetwork.PlayerList)
- {
- GameObject tempListing = Instantiate(playerListingPrefab, playersPanel);
- Text tempText = tempListing.transform.GetChild(0).GetComponent<Text>();
- tempText.text = player.NickName;
- }
- }
- }
- public override void OnPlayerEnteredRoom(Player newPlayer)
- {
- base.OnPlayerEnteredRoom(newPlayer);
- Debug.Log("A new player has joined the room");
- ClearPlayerListings();
- ListPlayers();
- photonPlayers = PhotonNetwork.PlayerList;
- playersInRoom++;
- if (MultiplayerSetting.multiplayerSetting.delayStart)
- {
- Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiplayerSetting.multiplayerSetting.maxPlayers + ")");
- if (playersInRoom > 1)
- {
- readyToCount = true;
- }
- if (playersInRoom == MultiplayerSetting.multiplayerSetting.maxPlayers)
- {
- readyToStart = true;
- if (!PhotonNetwork.IsMasterClient)
- return;
- PhotonNetwork.CurrentRoom.IsOpen = false;
- }
- }
- }
- public void StartGame()
- {
- isGameLoaded = true;
- if (!PhotonNetwork.IsMasterClient)
- return;
- if (MultiplayerSetting.multiplayerSetting.delayStart)
- {
- PhotonNetwork.CurrentRoom.IsOpen = false;
- }
- PhotonNetwork.LoadLevel(MultiplayerSetting.multiplayerSetting.gameScene);
- }
- void RestartTimer()
- {
- lessThanMaxPlayers = startingTime;
- timeToStart = startingTime;
- atMaxPlayer = 6;
- readyToCount = false;
- readyToStart = false;
- }
- void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
- {
- currentScene = scene.buildIndex;
- if (currentScene == MultiplayerSetting.multiplayerSetting.gameScene)
- {
- isGameLoaded = true;
- if (MultiplayerSetting.multiplayerSetting.delayStart)
- {
- PV.RPC("RPC_LoadedGameScene", RpcTarget.MasterClient);
- }
- else
- {
- RPC_CreatePlayer();
- if (PhotonNetwork.IsMasterClient)
- {
- RPC_CreateEnemies();
- }
- }
- }
- }
- [PunRPC]
- private void RPC_LoadedGameScene()
- {
- playerInGame++;
- if (playersInRoom == PhotonNetwork.PlayerList.Length)
- {
- PV.RPC("RPC_CreatePlayer", RpcTarget.All);
- if (!PV.IsMine)
- {
- Destroy(gameObject.GetComponent<PlayerMovement>());
- }
- }
- }
- [PunRPC]
- private void RPC_CreatePlayer()
- {
- PhotonNetwork.Instantiate(Path.Combine("Prefabs", "PhotonNetworkPlayer"), transform.position, Quaternion.identity, 0);
- }
- [PunRPC]
- private void RPC_CreateEnemies()
- {
- for (int i = 0; i < enemyAmount; i++)
- {
- PhotonNetwork.Instantiate(Path.Combine("Prefabs", "PhotonNetworkEnemy"), transform.position, Quaternion.identity, 0);
- }
- }
- public override void OnPlayerLeftRoom(Player otherPlayer)
- {
- base.OnPlayerLeftRoom(otherPlayer);
- PhotonNetwork.DestroyPlayerObjects(otherPlayer);
- Debug.Log(otherPlayer.NickName + "has left the game");
- playersInRoom--;
- ClearPlayerListings();
- ListPlayers();
- }
- }
Add Comment
Please, Sign In to add comment