using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.SceneManagement; using UnityEngine.UI; public class NetworkManager : MonoBehaviour { [SerializeField] private GameEvent_RiftSettings onSpawnRiftRequest; [SerializeField] private StringSharedField charSelected; [SerializeField] private GameEvent onLoadLevelStarting; [SerializeField] private GameEvent onJoinedRoom; [SerializeField] private GameEvent onLeftRoom; [SerializeField] private GameEventListener onCharacterConfirmed; [SerializeField] private GameEventListener_ZoneData onGameSceneLoaded; [SerializeField] private GameEventListener_JobInstance onJobSelected; [SerializeField] private GameEventListener_RiftPlayer onPlayerSpawned; [SerializeField] private GameEventListener onInfoPanelClosed; [SerializeField] private GameEventListener onMapReady; [SerializeField] private TMP_Text changeLevelVoteText; private int count = 0; private bool changeLevelVoted = false; private Button voteButton; //private object inProgress = false; ZoneData currentZone; private string votedZone; private void Awake() { DontDestroyOnLoad(this); onCharacterConfirmed.Response.AddListener(OnJoinedRoom); onGameSceneLoaded.Response.AddListener(OnGameSceneLoaded); onMapReady.Response.AddListener(OnMapReady); onInfoPanelClosed.Response.AddListener(() => { CancelChangeLevelVote(); }); onJobSelected.Response.AddListener((x) => votedZone = x.zoneName); } // Start is called before the first frame update void Start() { //Debug.Log("Connecting.."); } public void OnConnectedToMaster() { Debug.Log("Connected."); StartCoroutine(BackToLobby()); Debug.Log("Joining Lobby.."); } public void OnJoinedLobby() { Debug.Log("Lobby joined."); OnJoinedRoom(); //PhotonNetwork.JoinOrCreateRoom("Sprint_01", new RoomOptions { MaxPlayers = 0, EmptyRoomTtl = 0 }, TypedLobby.Default); //Debug.Log("Joining Game.."); } public void OnJoinedRoom() { if (!SceneManager.GetActiveScene().name.Contains(GameConstants.Scenes.HuntersInn)) { SceneManager.LoadScene(GameConstants.Scenes.HuntersInn); } onJoinedRoom.Raise(); } public void OnGameSceneLoaded(ZoneData zoneData) { currentZone = zoneData; if (zoneData.isProceduralMap) { //wait for map ready } else { StartCoroutine(SpawnCharacterWithDelay(zoneData)); } } public IEnumerator SpawnCharacterWithDelay(ZoneData zoneData) { yield return new WaitForSeconds(0.5f); //Instantiate(charSelected.Value, GetSpiralPosition(zoneData.playerBaseSpawnPoint.position, PhotonNetwork.LocalPlayer.ActorNumber - 1), Quaternion.identity); Instantiate(Resources.Load("PlayableCharacters/" + charSelected.Value), zoneData.playerBaseSpawnPoint.position, Quaternion.identity); } public void OnMapReady() { Instantiate(Resources.Load("PlayableCharacters/" + charSelected.Value), currentZone.playerBaseSpawnPoint.position, Quaternion.identity); } public Vector3 GetSpiralPosition(Vector3 centerPoint, int playerIndex) { if (playerIndex == 0) return centerPoint; // Base radius and angle increment settings float baseRadius = 1.5f; // Base distance between players float angleIncrement = 30f; // Degrees between each position float radiusGrowthFactor = 0.25f; // How much to increase radius per rotation // Calculate current angle and radius float angle = angleIncrement * playerIndex; float rotations = angle / 360f; float currentRadius = baseRadius * (1f + rotations * radiusGrowthFactor); // Convert angle to radians float rad = angle * Mathf.Deg2Rad; // Calculate position float x = centerPoint.x + currentRadius * Mathf.Cos(rad); float z = centerPoint.z + currentRadius * Mathf.Sin(rad); return new Vector3(x, centerPoint.y, z); } /* private void Update() { if (!PhotonNetwork.LocalPlayer.IsMasterClient) return; if (Input.GetKeyDown(KeyCode.S)) { onSpawnRiftRequest.Raise(new RiftSettings() { zoneData = rift, 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(ZoneData zoneData) { yield return new WaitForSeconds(1f); onSpawnRiftRequest.Raise(new RiftSettings() { zoneData = zoneData, numberOfWaves = Random.Range(GameConstants.GameBalancing.MinimumNumberOfWavesPerRift, GameConstants.GameBalancing.MaximumNumberOfWavesPerRift), riftIndex = Random.Range(0, 5), enemyIndexes_Quantity = new Dictionary() { { (int)GameConstants.EnemySpawning.EnemyID.AngrySkelly, Random.Range(GameConstants.GameBalancing.MinimumQuantityAngrySkelly, GameConstants.GameBalancing.MaximumQuantityAngrySkelly) }, { (int)GameConstants.EnemySpawning.EnemyID.SkellyMage, Random.Range(GameConstants.GameBalancing.MinimumQuantitySkellyMage, GameConstants.GameBalancing.MaximumQuantitySkellyMage) }, { (int)GameConstants.EnemySpawning.EnemyID.VineGolem, Random.Range(GameConstants.GameBalancing.MinimumQuantityVineGolem, GameConstants.GameBalancing.MaximumQuantityVineGolem) }, { (int)GameConstants.EnemySpawning.EnemyID.PolygonGolem, Random.Range(GameConstants.GameBalancing.MinimumQuantityPolygonGolem, GameConstants.GameBalancing.MaximumQuantityPolygonGolem) }, { (int)GameConstants.EnemySpawning.EnemyID.Lich, Random.Range(GameConstants.GameBalancing.MinimumQuantityLich, GameConstants.GameBalancing.MaximumQuantityLich) }, }, bossIndex = Random.Range(0, (int)GameConstants.EnemySpawning.EnemyID.Count) }); ; }*/ public void OnPlayerEnteredRoom(RiftPlayer newPlayer) { Debug.Log("New player joined: " + newPlayer); changeLevelVoteText.text = $"Votes: {count}/1"; //onPlayerJoinedParty.Raise(newPlayer); } public void OnPlayerLeftRoom(RiftPlayer otherPlayer) { changeLevelVoteText.text = $"Votes: {count}/1"; Debug.Log("Player left: " + otherPlayer); } public void LeaveBackToCharacterSelection() { OnLeftRoom(); } public void OnLeftRoom() { onLeftRoom.Raise(); SceneManager.LoadScene(GameConstants.Scenes.CharacterList); } IEnumerator BackToLobby() { yield return null; OnJoinedLobby(); } public void ChangeLevelVote(Button button) { if (changeLevelVoted) return; voteButton = button; changeLevelVoted = true; //voteButton.interactable = false; UpdateGameStateOnLoadLevelStarting(); NetworkLoadVotedLevel(); } public void CancelChangeLevelVote() { changeLevelVoted = false; if (voteButton != null) voteButton.interactable = true; } private void NetworkLoadVotedLevel() { StartCoroutine(NetworkLoadRestartWithDelay()); } private IEnumerator NetworkLoadRestartWithDelay() { yield return new WaitForSeconds(1f); SceneManager.LoadScene(GameConstants.Scenes.GetSceneNameByZoneName(votedZone)); changeLevelVoted = false; } private void UpdateGameStateOnLoadLevelStarting() { onLoadLevelStarting.Raise(); } }