diff --git a/Assets/Scripts/Environment/Pitfall.cs b/Assets/Scripts/Environment/Pitfall.cs index 43f0341..f518807 100644 --- a/Assets/Scripts/Environment/Pitfall.cs +++ b/Assets/Scripts/Environment/Pitfall.cs @@ -3,6 +3,7 @@ using MLAPI.Exceptions; using MLAPI.Messaging; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; public class Pitfall : NetworkBehaviour @@ -60,14 +61,14 @@ public class Pitfall : NetworkBehaviour { // Spawn as player _runner = Instantiate(runnerPrefab, RespawnPoint, Quaternion.Euler(0, -90, 0)).gameObject; - //Recreate Inventory - // FOLLOWING LINE WILL NEED TO BE UPDATED SINCE THIS WILL ONLY WORK FOR THE HOST AND NOT THE CONNECTED CLIENTS - _runner.GetComponentInChildren().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent().ItemDict); _runner.GetComponent().SpawnAsPlayerObject(clientID, null, true); // Turn on camera if the player is the host if (NetworkManager.Singleton.LocalClientId == clientID) { + // Recreate Inventory (host) + _runner.GetComponentInChildren().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent().ItemDict); + GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; @@ -77,7 +78,8 @@ public class Pitfall : NetworkBehaviour else { // Spawn via RPC on the server - StartCoroutine(SpawnClient(clientID)); + string itemsAsString = string.Join(",", playerData.pInv.NetworkItemList); + StartCoroutine(SpawnClient(clientID, itemsAsString)); } } } @@ -86,7 +88,7 @@ public class Pitfall : NetworkBehaviour } // The client respawning needs a slight delay to allow for the spawn to properly sync up - IEnumerator SpawnClient(ulong clientID) + IEnumerator SpawnClient(ulong clientID, string itemsAsString) { yield return new WaitForSecondsRealtime(1f); @@ -98,12 +100,12 @@ public class Pitfall : NetworkBehaviour } }; - SpawnPlayerClientRpc(clientID, clientRpcParams); + SpawnPlayerClientRpc(clientID, itemsAsString, clientRpcParams); } // Spawn in each player [ClientRpc] - public void SpawnPlayerClientRpc(ulong clientId, ClientRpcParams clientRpcParams = default) + public void SpawnPlayerClientRpc(ulong clientId, string itemsAsString, ClientRpcParams clientRpcParams = default) { GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player"); @@ -112,6 +114,10 @@ public class Pitfall : NetworkBehaviour { if (character.GetComponent().OwnerClientId == clientId) { + // Rebuild inventory + List itemList = itemsAsString.Split(',').ToList(); + character.GetComponentInChildren().UpdateEquips(itemList, this.gameObject.GetComponent().ItemDict); + GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; diff --git a/Assets/Scripts/Network/Game/SpawnManager.cs b/Assets/Scripts/Network/Game/SpawnManager.cs index 06839a9..03ae245 100644 --- a/Assets/Scripts/Network/Game/SpawnManager.cs +++ b/Assets/Scripts/Network/Game/SpawnManager.cs @@ -1,3 +1,6 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; using MLAPI; using MLAPI.Messaging; using UnityEngine; @@ -70,14 +73,49 @@ public class SpawnManager : NetworkBehaviour { } else { // Spawn as player _runner = Instantiate(runnerPrefab, runnersSpawnPoints[runnersSpawned], Quaternion.Euler(0, -90, 0)).gameObject; - //Recreate Inventory - _runner.GetComponentInChildren().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent().ItemDict); _runner.GetComponent().SpawnAsPlayerObject(clientId, null, true); // Increment runners runnersSpawned++; + + // Tell the client to apply the inventory to its player + string itemsAsString = string.Join(",", playerData.pInv.NetworkItemList); + StartCoroutine(ApplyInventoryToClient(clientId, itemsAsString)); } } } } + + // Give a slight delay to allow spawning to complete + IEnumerator ApplyInventoryToClient(ulong clientID, string itemsAsString) + { + yield return new WaitForSecondsRealtime(1f); + + ClientRpcParams clientRpcParams = new ClientRpcParams + { + Send = new ClientRpcSendParams + { + TargetClientIds = new ulong[] { clientID } + } + }; + + ApplyInventoryClientRPC(clientID, itemsAsString, clientRpcParams); + } + + [ClientRpc] + private void ApplyInventoryClientRPC(ulong clientID, string itemsAsString, ClientRpcParams clientRpcParams = default) { + + + GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player"); + + // Loop over the characters + foreach (GameObject character in playableCharacters) { + // Find the local player + if (character.GetComponent().OwnerClientId == clientID) { + List itemList = itemsAsString.Split(',').ToList(); + + character.GetComponentInChildren().UpdateEquips(itemList, this.gameObject.GetComponent().ItemDict); + } + } + } } diff --git a/Assets/Scripts/Network/PlayerData.cs b/Assets/Scripts/Network/PlayerData.cs index 94b3940..4382284 100644 --- a/Assets/Scripts/Network/PlayerData.cs +++ b/Assets/Scripts/Network/PlayerData.cs @@ -3,7 +3,7 @@ public struct PlayerData { public string PlayerName { get; private set; } public ulong ClientId { get; private set; } public bool IsKing { get; set; } - public PlayerInventory pInv{get; set;} + public PlayerInventory pInv { get; set; } public bool Finished { get; set; } diff --git a/Assets/Scripts/PlayerScripts/PlayerInventory.cs b/Assets/Scripts/PlayerScripts/PlayerInventory.cs index 7e3ec80..67e3a5f 100644 --- a/Assets/Scripts/PlayerScripts/PlayerInventory.cs +++ b/Assets/Scripts/PlayerScripts/PlayerInventory.cs @@ -72,6 +72,4 @@ public class PlayerInventory : NetworkBehaviour AddItem(allItems[itemName]); } } - - }