From 7a866191bb0d8e581a67aa92840c6b51b4297eed Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Mon, 25 Oct 2021 13:38:16 -0500 Subject: [PATCH] Continued work on spawning players - Modified PlayerData to hold IsKing - Added spawning code - Need to merge in player prefab with networkobject --- Assets/Scenes/Preloader.unity | 2 +- Assets/Scripts/Network/PlayerData.cs | 4 ++- Assets/Scripts/Network/ServerGameNetPortal.cs | 32 ++++++++++++++++--- Assets/Scripts/UI/Lobby/LobbyUI.cs | 17 +++++++--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Assets/Scenes/Preloader.unity b/Assets/Scenes/Preloader.unity index f2bb8a7..0d25e83 100644 --- a/Assets/Scenes/Preloader.unity +++ b/Assets/Scenes/Preloader.unity @@ -212,7 +212,7 @@ MonoBehaviour: runnerSpawnPoints: - {x: 0, y: 0, z: 0} - {x: 2, y: 0, z: 2} - kingSpawnPoint: {x: 0, y: 0, z: 0} + kingSpawnPoint: {x: 5, y: 5, z: 5} --- !u!1 &1494071116 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Network/PlayerData.cs b/Assets/Scripts/Network/PlayerData.cs index ac2a888..aa2f5dd 100644 --- a/Assets/Scripts/Network/PlayerData.cs +++ b/Assets/Scripts/Network/PlayerData.cs @@ -2,10 +2,12 @@ public struct PlayerData { public string PlayerName { get; private set; } public ulong ClientId { get; private set; } + public bool IsKing { get; set; } - public PlayerData(string playerName, ulong clientId) + public PlayerData(string playerName, ulong clientId, bool isKing = false) { PlayerName = playerName; ClientId = clientId; + IsKing = isKing; } } diff --git a/Assets/Scripts/Network/ServerGameNetPortal.cs b/Assets/Scripts/Network/ServerGameNetPortal.cs index 0cb1865..bab87de 100644 --- a/Assets/Scripts/Network/ServerGameNetPortal.cs +++ b/Assets/Scripts/Network/ServerGameNetPortal.cs @@ -13,11 +13,15 @@ public class ServerGameNetPortal : MonoBehaviour { [Header("Settings")] [SerializeField] private int maxPlayers = 3; + [Header("Prefabs")] + [SerializeField] private GameObject runnerPrefab; + [SerializeField] private GameObject kingPrefab; + public static ServerGameNetPortal Instance => instance; private static ServerGameNetPortal instance; - private Dictionary clientData; - private Dictionary clientIdToGuid; + public Dictionary clientData; + public Dictionary clientIdToGuid; private Dictionary clientSceneMap; private bool gameInProgress; @@ -102,6 +106,7 @@ public class ServerGameNetPortal : MonoBehaviour { Vector3[] runnersSpawnPoints; Vector3 kingSpawnPoint; + // Get the spawn points for the level switch (gameLevelLoaded) { default: case 0: @@ -109,14 +114,33 @@ public class ServerGameNetPortal : MonoBehaviour { kingSpawnPoint = SpawnPoints.Instance.getKingSpawnPoint(gameLevelLoaded); break; } - + // Spawn in the players, but make sure the character controllers are diabled + int runnerSpawnIndex = 0; + foreach (PlayerData pData in clientData.Values) { + // Spawn in the prefab for the player based on king or runner + GameObject go = null; + if (pData.IsKing) { + go = Instantiate(runnerPrefab, kingSpawnPoint, Quaternion.identity); + + } else { + go = Instantiate(runnerPrefab, runnersSpawnPoints[runnerSpawnIndex], Quaternion.identity); + } + + // Disable the character controller + go.GetComponent().enabled = false; + go.GetComponent().enabled = true; + + // Spawn the player on network and assign the owner + go.GetComponent().Spawn(); + go.GetComponent().ChangeOwnership(pData.ClientId); + } // Perform the intro cutscene camera movement // Do a 3.2.1 countdown or ask team what we want to do here - // Re-enable the character controllers + // Re-enable the character controllers and disable the capsulecollider // Start the game timer } diff --git a/Assets/Scripts/UI/Lobby/LobbyUI.cs b/Assets/Scripts/UI/Lobby/LobbyUI.cs index 92f6559..4728afc 100644 --- a/Assets/Scripts/UI/Lobby/LobbyUI.cs +++ b/Assets/Scripts/UI/Lobby/LobbyUI.cs @@ -57,8 +57,6 @@ public class LobbyUI : NetworkBehaviour { return false; } - // TODO: Need to add a check to make sure that 2 are runners and 1 is king - foreach (var player in lobbyPlayers) { if (!player.IsReady) @@ -136,14 +134,25 @@ public class LobbyUI : NetworkBehaviour { [ServerRpc(RequireOwnership = false)] private void SwapTeamsServerRpc(ServerRpcParams serverRpcParams = default) { - for (int i = 0; i < lobbyPlayers.Count; i++) { - if (lobbyPlayers[i].ClientId == serverRpcParams.Receive.SenderClientId) { + for (int i = 0; i < lobbyPlayers.Count; i++) { + ulong senderClientID = serverRpcParams.Receive.SenderClientId; + // Update the lobby state that controls that player's data + if (lobbyPlayers[i].ClientId == senderClientID) { lobbyPlayers[i] = new LobbyPlayerState( lobbyPlayers[i].ClientId, lobbyPlayers[i].PlayerName, lobbyPlayers[i].IsReady, !lobbyPlayers[i].IsKing ); + + // Update the server's player data as well + if (ServerGameNetPortal.Instance.clientIdToGuid.TryGetValue(senderClientID, out string clientGuid)) { + ServerGameNetPortal.Instance.clientData[clientGuid] = new PlayerData( + ServerGameNetPortal.Instance.clientData[clientGuid].PlayerName, + ServerGameNetPortal.Instance.clientData[clientGuid].ClientId, + lobbyPlayers[i].IsKing + ); + } } } }