using Assets.Scripts.Multiplayer; using Steamworks.ServerList; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.Netcode; using Unity.Netcode.Transports.UTP; using UnityEngine; using UnityEngine.SceneManagement; using static UnityEditor.Experimental.GraphView.GraphView; public class NetworkedGameSetup : NetworkBehaviour { [SerializeField] private GameObject PlayerPrefab; private void Awake() { if (NetworkManager.Singleton == null || NetworkManager.Singleton.SceneManager == null) return; DontDestroyOnLoad(this); NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneLoaded; } private void SceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List clientsCompleted, List clientsTimedOut) { if (!sceneName.ToLower().Contains("game")) return; NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded; // Only run once StartSetupProcedure(clientsCompleted); } private void StartSetupProcedure(List playerIds) { bool isServer = SteamManager.IsServer || LocalManager.IsServer; GameObject[] players; if (isServer) players = SpawnPlayers(playerIds); else players = GetPlayers(playerIds); StartCoroutine(LateSetupProcedue(players)); } private IEnumerator LateSetupProcedue(GameObject[] players) { yield return new WaitForSecondsRealtime(0.01f); InitRope(players); InitUpgrader(players); InitBlood(players); InitSpawner(players); } private GameObject[] GetPlayers(List playerIds) { GameObject[] players = NetworkManager.SpawnManager.SpawnedObjects.Where(x => playerIds.Contains(x.Value.NetworkObjectId)).Select(x => x.Value.gameObject).ToArray(); return players; } private GameObject[] SpawnPlayers(List playerIds) { GameObject[] players = new GameObject[playerIds.Count]; for (int i = 0; i < playerIds.Count; i++) { GameObject player = Instantiate(PlayerPrefab, Vector2.up * 3, Quaternion.identity); player.GetComponent().SpawnAsPlayerObject(playerIds[i], true); players[i] = player; } return players; } private void InitRope(GameObject[] players) { if (players.Length == 1) { RopeSimulator.instance.BuildRope(players[0].GetComponent(), players[0].GetComponent()); } else // 2 players { RopeSimulator.instance.BuildRope(players[0].GetComponent(), players[1].GetComponent()); } } private void InitUpgrader(GameObject[] players) { return; // fuck this upgrade system Upgrader upgradeSystem = Upgrader.instance; if (players.Length == 1) { upgradeSystem.player1 = players[0]; upgradeSystem.player2 = players[0]; } else // 2 players { upgradeSystem.player1 = players[0]; upgradeSystem.player2 = players[1]; } } private void InitBlood(GameObject[] players) { BloodComputeShader bloodScript = BloodComputeShader.Instance; if (players.Length == 1) { bloodScript.mop1 = players[0].GetComponent(); bloodScript.mop2 = players[0].GetComponent(); } else // 2 players { bloodScript.mop1 = players[0].GetComponent(); bloodScript.mop2 = players[1].GetComponent(); } } private void InitSpawner(GameObject[] players) { EnemySpawner spawner = EnemySpawner.instance; spawner.players = players; } }