using ExitGames.Client.Photon; using Photon.Pun; using Photon.Realtime; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.SceneManagement; using UnityEngine.UI; public class NetworkManager : MonoBehaviourPunCallbacks, IOnEventCallback { [SerializeField] private GameEvent_RiftSettings onSpawnRiftRequest; [SerializeField] private StringSharedField charSelected; [SerializeField] private GameEvent_Player onPlayerJoinedParty; [SerializeField] private GameEvent_Player onPlayerLeftParty; [SerializeField] private GameEvent onLoadLevelStarting; [SerializeField] private GameEventListener onGameSceneLoaded; [SerializeField] private GameEventListener_PhotonView onPlayerSpawned; //[SerializeField] private TMP_Text nextRiftVoteText; private int count = 0; private bool changeLevelVoted = false; //private object inProgress = false; private Dictionary playerVoted = new Dictionary(); public List party = new List(); readonly string huntersInn = "4-RiftHuntersInn"; readonly string skellyard = "4-Skellyard"; private void Awake() { DontDestroyOnLoad(this); onGameSceneLoaded.Response.AddListener(OnGameSceneLoaded); onPlayerSpawned.Response.AddListener(OnPlayerSpawned); } // Start is called before the first frame update void Start() { PhotonNetwork.AutomaticallySyncScene = true; PhotonNetwork.ConnectUsingSettings(); Debug.Log("Connecting.."); } public override void OnConnectedToMaster() { Debug.Log("Connected."); PhotonNetwork.JoinLobby(); Debug.Log("Joining Lobby.."); } public override void OnJoinedLobby() { Debug.Log("Lobby joined."); //PhotonNetwork.JoinOrCreateRoom("Sprint_01", new RoomOptions { MaxPlayers = 0, EmptyRoomTtl = 0 }, TypedLobby.Default); //Debug.Log("Joining Game.."); } public override void OnJoinedRoom() { Debug.Log("Party \"" + PhotonNetwork.CurrentRoom.Name + "\" joined."); if (!SceneManager.GetActiveScene().name.Contains(huntersInn)) { if (PhotonNetwork.IsMasterClient) { PhotonNetwork.LoadLevel(huntersInn); } } } public void OnGameSceneLoaded() { StartCoroutine(SpawnCharacterWithDelay()); if (PhotonNetwork.LocalPlayer.IsMasterClient && SceneManager.GetActiveScene().name != huntersInn) StartCoroutine(SpawnEnemiesWithDelay()); } public IEnumerator SpawnCharacterWithDelay() { yield return new WaitUntil(() => PhotonNetwork.InRoom); PhotonNetwork.Instantiate("PlayableCharacters/" + charSelected.Value, Vector3.zero + new Vector3(3 * PhotonNetwork.LocalPlayer.ActorNumber, 0, 0), Quaternion.identity); } private void Update() { if (!PhotonNetwork.LocalPlayer.IsMasterClient) return; if (Input.GetKeyDown(KeyCode.S)) { onSpawnRiftRequest.Raise(new RiftSettings() { position = new Vector3(-6, 0, 6), riftIndex = Random.Range(0, 5), enemyIndexes_Quantity = new Dictionary() { { 0, Random.Range(2, 6) }, { 1, Random.Range(2, 6) } } }); //PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 10), Quaternion.identity); } if (Input.GetKeyDown(KeyCode.F1)) { PhotonNetwork.LoadLevel(huntersInn); } if (Input.GetKeyDown(KeyCode.F2)) { PhotonNetwork.LoadLevel(skellyard); } } public IEnumerator SpawnEnemiesWithDelay() { yield return new WaitForSeconds(1f);/* PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 10), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 8), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 6), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 4), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 2), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-10, 0, 0), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-8, 0, 10), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-8, 0, 8), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-8, 0, 6), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-8, 0, 4), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-8, 0, 2), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-6, 0, 10), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-6, 0, 8), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-6, 0, 6), Quaternion.identity); PhotonNetwork.InstantiateRoomObject("AngrySkellyPrefab", new Vector3(-6, 0, 4), Quaternion.identity);*/ onSpawnRiftRequest.Raise(new RiftSettings() { position = new Vector3(-6, 0, 6), riftIndex = Random.Range(0, 5), enemyIndexes_Quantity = new Dictionary() { { 0, Random.Range(2, 6) }, { 1, Random.Range(2, 6) } } }); } public override void OnPlayerEnteredRoom(Player newPlayer) { Debug.Log("New player joined: " + newPlayer.NickName); playerVoted.Add(newPlayer, false); //party.Add(newPlayer); //onPlayerJoinedParty.Raise(newPlayer); } public override void OnPlayerLeftRoom(Player otherPlayer) { if (playerVoted.ContainsKey(otherPlayer)) { playerVoted.Remove(otherPlayer); } if (party.Contains(otherPlayer)) { party.Remove(otherPlayer); onPlayerLeftParty.Raise(otherPlayer); } Debug.Log("Player left: " + otherPlayer.NickName); } public void OnPlayerSpawned(PhotonView photonView) { party.Add(photonView.Owner); onPlayerJoinedParty.Raise(photonView.Owner); } public void ChangeLevelVote(Button button) { if (!PhotonNetwork.IsConnected) return; if (changeLevelVoted) return; changeLevelVoted = true; button.interactable = false; if (PhotonNetwork.CurrentRoom.PlayerCount == 1) { CountVote(PhotonNetwork.LocalPlayer); Send_All_OnLoadLevelStarting(); } else { CountVote(PhotonNetwork.LocalPlayer); RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others }; PhotonNetwork.RaiseEvent(GameConstants.NetworkEventCodes.ChangeLevelVoted, PhotonNetwork.LocalPlayer, raiseEventOptions, SendOptions.SendReliable); if (PhotonNetwork.IsMasterClient) { if (count >= PhotonNetwork.CurrentRoom.PlayerCount) { Send_All_OnLoadLevelStarting(); } } } } private void Send_All_OnLoadLevelStarting() { RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.All }; PhotonNetwork.RaiseEvent(GameConstants.NetworkEventCodes.LoadLevelStarting, 0, raiseEventOptions, SendOptions.SendReliable); } public void OnEvent(EventData photonEvent) { if (photonEvent.Code == GameConstants.NetworkEventCodes.ChangeLevelVoted) { Player player = (Player)photonEvent.CustomData; Debug.Log(player); Debug.Log(player.ActorNumber); Debug.Log(player.NickName); if (PhotonNetwork.IsMasterClient) { CountVote(player); if (count >= PhotonNetwork.CurrentRoom.PlayerCount) { Send_All_OnLoadLevelStarting(); } } else { CountVote(player); } } if (photonEvent.Code == GameConstants.NetworkEventCodes.LoadLevelStarting) { Debug.Log("loadlevelStarting"); UpdateGameStateOnLoadLevelStarting(); if (PhotonNetwork.IsMasterClient) NetworkLoadVotedLevel(); } } private void CountVote(Player player) { playerVoted[player] = true; count++; //nextRiftVoteText.text = $"{count}/{PhotonNetwork.PlayerList.Length}"; } private void NetworkLoadVotedLevel() { UpdateRoomPropertiesInProgress(false); StartCoroutine(NetworkLoadRestartWithDelay()); } private IEnumerator NetworkLoadRestartWithDelay() { yield return new WaitForSeconds(1.5f); PhotonNetwork.LoadLevel(SceneManager.GetActiveScene().name == huntersInn ? skellyard : huntersInn); } private void UpdateGameStateOnLoadLevelStarting() { onLoadLevelStarting.Raise(); } private void CheckRoomProperties() { //if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue("inProgress", out inProgress)) //{ // Debug.Log("Got inProgress from properties: " + (bool)inProgress + " "); //} PhotonNetwork.Instantiate(charSelected.Value, Vector3.zero + new Vector3(3 * PhotonNetwork.LocalPlayer.ActorNumber, 0, 0), Quaternion.identity); } public override void OnRoomPropertiesUpdate(ExitGames.Client.Photon.Hashtable propertiesThatChanged) { } public void UpdateRoomPropertiesInProgress(bool inProgress) { ExitGames.Client.Photon.Hashtable ht = new ExitGames.Client.Photon.Hashtable { { "inProgress", inProgress } }; PhotonNetwork.CurrentRoom.SetCustomProperties(ht); } }