From c0d9c685ca3491cfe290254f9b0a98ab40ab744d Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Wed, 14 Feb 2024 05:37:52 +0100 Subject: [PATCH] Progress on gmae setup script --- .../Scripts/Multiplayer/NetworkedGameSetup.cs | 51 ++++++++++++++----- Assets/Scripts/Rope/RopeSimulator.cs | 2 +- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs b/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs index dada16d..b93e489 100644 --- a/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs +++ b/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using Unity.Netcode; using UnityEngine; using UnityEngine.SceneManagement; @@ -9,8 +10,6 @@ public class NetworkedGameSetup : NetworkBehaviour { [SerializeField] private GameObject PlayerPrefab; - private RopeSimulator RopeSim; - private void Start() { DontDestroyOnLoad(this); @@ -19,26 +18,54 @@ public class NetworkedGameSetup : NetworkBehaviour private void SceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List clientsCompleted, List clientsTimedOut) { + NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded; // Only run once StartSetupProcedure(clientsCompleted); } private void StartSetupProcedure(List playerIds) { - SpawnPlayers(playerIds); + GameObject[] players; + if (IsHost || IsServer) + players = SpawnPlayers(playerIds); + else + players = GetPlayers(playerIds); + InitRope(players); } - private void SpawnPlayers(List playerIds) + private GameObject[] GetPlayers(List playerIds) { - NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded; + GameObject[] players = new GameObject[playerIds.Count]; - // Assuming only 2 palyers - if (IsHost) + for (int i = 0; i < playerIds.Count; i++) { - for (int i = 0; i < playerIds.Count; i++) - { - GameObject player = Instantiate(PlayerPrefab, Vector2.up * 3, Quaternion.identity); - player.GetComponent().SpawnAsPlayerObject(playerIds[i], true); - } + ulong playerId = playerIds[i]; + players[i] = NetworkManager.SpawnManager.SpawnedObjects[playerId].gameObject; } + + 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) + { + RopeSimulator ropeSim = GetComponentInChildren(); + + //// Assuming 2 players + //ropeSim.start = new RopeJoint(players[0].transform.position, true); + //ropeSim.start = players[1]; + } + } diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 2202a0c..cb3fca0 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -16,7 +16,7 @@ public class RopeSimulator : MonoBehaviour private bool constrainStickMinLength; [SerializeField] - RopeJoint start, end; + public RopeJoint start, end; [SerializeField] float subDivision = 50f;