diff --git a/Assets/Scripts/Control&Input/Peer2PeerTransport.cs b/Assets/Scripts/Control&Input/Peer2PeerTransport.cs index a51cc7674..fe5cff47d 100644 --- a/Assets/Scripts/Control&Input/Peer2PeerTransport.cs +++ b/Assets/Scripts/Control&Input/Peer2PeerTransport.cs @@ -8,6 +8,7 @@ public struct PlayerDetails { public uint id; + public ulong steamID; public PlayerType type; public string name; @@ -76,7 +77,7 @@ public class Peer2PeerTransport : NetworkManager private SpawnHandlerDelegate onSpawnPlayer; private UnSpawnDelegate onUnSpawnPlayer; - private const int FPSPlayerIndex = 0; + private const int FPSPlayerPrefabIndex = 0; private readonly Dictionary players = new(); @@ -124,9 +125,7 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn PlayerInputManagerController.Singleton.NetworkClients.Add(connection); var index = SteamManager.Singleton.PlayerNames.Count - 1; var steamName = SteamManager.Singleton.PlayerNames[index]; - SteamManager.Singleton.PlayerDictionary.Add(connection.connectionId, - (steamName, index)); - + var steamID = SteamManager.Singleton.PlayerIDs[index]; var player = Instantiate(playerPrefab); var playerInput = player.GetComponent(); @@ -145,6 +144,7 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn var details = new PlayerDetails { id = (uint)index, + steamID = steamID, type = message.type, name = steamName, color = PlayerInputManagerController.Singleton.PlayerColors[index], @@ -158,8 +158,7 @@ private void OnReceivePlayerDetails(InitialPlayerDetailsMessage message) var details = message.details; if (players.ContainsKey(details.id)) return; - // TODO replace this check with sth else, find your netId somehow? Or use steam id? - details.type = SteamManager.Singleton.UserName == details.name ? PlayerType.Local : PlayerType.Remote; + details.type = SteamManager.Singleton.SteamID.m_SteamID == details.steamID ? PlayerType.Local : PlayerType.Remote; if (details.type is PlayerType.Local) myId = details.id; Debug.Log($"Received info for player {details.id}: name={details.name} type={details.type} color={details.color}"); players.Add(details.id, details); @@ -218,11 +217,9 @@ private void OnSpawnFPSPlayer(NetworkConnectionToClient connection, SpawnFPSPlay } var spawnPoint = spawnPoints[playerIndex]; - // TODO reset this playerIndex++; - // TODO check if this is kenough - var player = Instantiate(spawnPrefabs[FPSPlayerIndex], spawnPoint.position, spawnPoint.rotation); + var player = Instantiate(spawnPrefabs[FPSPlayerPrefabIndex], spawnPoint.position, spawnPoint.rotation); player.GetComponent().id = message.id; NetworkServer.AddPlayerForConnection(connection, player); NetworkServer.SendToAll(new InitializeFPSPlayerMessage(message.id, spawnPoint.position, spawnPoint.rotation)); @@ -271,7 +268,7 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag var input = PlayerInputManagerController.Singleton.LocalPlayerInputs[0]; // Make playerInput child of player it's attached to input.transform.parent = player.transform; - // Set recieved playerInput (and most importantly its camera) at an offset from player's position + // Set received playerInput (and most importantly its camera) at an offset from player's position input.transform.localPosition = cameraOffset.localPosition; input.transform.rotation = player.transform.rotation; @@ -307,7 +304,6 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag // TODO set based on playerdetails (and edit playerdetails) playerManager.identity.SetLoadout(StaticInfo.Singleton.StartingBody, StaticInfo.Singleton.StartingBarrel, StaticInfo.Singleton.StartingExtension); - Debug.Log($"{playerManager.identity.Body}"); // TODO do some other version of disabling HUD completely Destroy(playerManager.HUDController); @@ -316,11 +312,11 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag playerManager.GetComponent().isKinematic = true; // Create display gun structure - var gunHolderParent = new GameObject("GunHolderParent").transform; + var gunHolderParent = new GameObject("DisplayGunParent").transform; gunHolderParent.parent = player.transform; gunHolderParent.position = cameraOffset.position; gunHolderParent.rotation = player.transform.rotation; - var gunHolder = new GameObject("GunHolder").transform; + var gunHolder = new GameObject("DisplayGunHolder").transform; gunHolder.parent = gunHolderParent.transform; gunHolder.localPosition = Vector3.zero; gunHolder.localRotation = Quaternion.identity; diff --git a/Assets/Scripts/PlayerSelect/PlayerSelectManager.cs b/Assets/Scripts/PlayerSelect/PlayerSelectManager.cs index 1a114ab2e..c13d682ef 100644 --- a/Assets/Scripts/PlayerSelect/PlayerSelectManager.cs +++ b/Assets/Scripts/PlayerSelect/PlayerSelectManager.cs @@ -51,10 +51,10 @@ private void Start() animatorParameters.Add(playerAnimators[0].GetParameter(i).name); } if (SteamManager.IsSteamActive) - SteamManager.Singleton.LobbyPlayerUpdate += SetLobby; + SteamManager.Singleton.LobbyPlayerUpdate += UpdateLobby; } - public void SetLobby() + public void UpdateLobby() { for (int i = 0; i < SteamManager.Singleton.PlayerNames.Count; i++) SetupPlayerSelectModels(SteamManager.Singleton.PlayerNames[i], playerInputManagerController.PlayerColors[i], i); @@ -173,6 +173,6 @@ IEnumerator PlayRandomAnimation() private void OnDestroy() { if (SteamManager.IsSteamActive) - SteamManager.Singleton.LobbyPlayerUpdate -= SetLobby; + SteamManager.Singleton.LobbyPlayerUpdate -= UpdateLobby; } } diff --git a/Assets/Scripts/UI/MainMenu/MainMenuController.cs b/Assets/Scripts/UI/MainMenu/MainMenuController.cs index 72eb36f0e..4cbbe9644 100644 --- a/Assets/Scripts/UI/MainMenu/MainMenuController.cs +++ b/Assets/Scripts/UI/MainMenu/MainMenuController.cs @@ -337,7 +337,7 @@ public void HostLobby() if (!SteamManager.IsSteamActive) return; SteamManager.Singleton.HostLobby(); - playerSelectManager.SetLobby(); + playerSelectManager.UpdateLobby(); } public void LeaveLobby() diff --git a/Assets/Scripts/Utils/SteamManager.cs b/Assets/Scripts/Utils/SteamManager.cs index 853786856..941ac0cae 100644 --- a/Assets/Scripts/Utils/SteamManager.cs +++ b/Assets/Scripts/Utils/SteamManager.cs @@ -31,8 +31,9 @@ public class SteamManager : MonoBehaviour public static bool IsSteamActive => isSteamInitialized; public bool IsHosting = false; public string UserName; - public List PlayerNames = new List(); - public Dictionary PlayerDictionary = new Dictionary(); + public CSteamID SteamID; + public List PlayerNames = new(); + public List PlayerIDs = new(); public delegate void LobbyEvent(); public LobbyEvent LobbyPlayerUpdate; @@ -89,6 +90,7 @@ private void Start() RequestStats(); UserName = SteamFriends.GetPersonaName(); + SteamID = SteamUser.GetSteamID(); } private void RequestStats() @@ -152,20 +154,31 @@ private void OnJoinRequest(GameLobbyJoinRequested_t callback) SteamMatchmaking.JoinLobby(callback.m_steamIDLobby); } - private void OnLobbyEnter(LobbyEnter_t callback) + private void UpdateLobbyData(ulong lobbyID) { - // All users - PlayerInputManagerController.Singleton.RemoveJoinListener(); - // TODO: set steam names over players - var lobbyId = new CSteamID(callback.m_ulSteamIDLobby); - Debug.Log("onLobbyUp"); + var lobbyId = new CSteamID(lobbyID); + Debug.Log("Lobby entered"); for (int i = 0; i < SteamMatchmaking.GetNumLobbyMembers(lobbyId); i++) { - string name = SteamFriends.GetFriendPersonaName(SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i)); - if (!PlayerNames.Contains(name)) - PlayerNames.Add(name); + var id = SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i); + var name = SteamFriends.GetFriendPersonaName(id); + + if (PlayerIDs.Contains(id.m_SteamID)) + continue; + Debug.Log($"Steam user {name} (id={id.m_SteamID}) entered lobby"); + + // TODO replace these separate lists with *one* + PlayerNames.Add(name); + PlayerIDs.Add(id.m_SteamID); } LobbyPlayerUpdate?.Invoke(); + } + + private void OnLobbyEnter(LobbyEnter_t callback) + { + // All users + PlayerInputManagerController.Singleton.RemoveJoinListener(); + UpdateLobbyData(callback.m_ulSteamIDLobby); if (NetworkServer.active) return; // Only clients from here! @@ -175,15 +188,8 @@ private void OnLobbyEnter(LobbyEnter_t callback) private void OnLobbyUpdate(LobbyChatUpdate_t callback) { - var lobbyId = new CSteamID(callback.m_ulSteamIDLobby); - Debug.Log("onLobbyUp"); - for (int i = 0; i < SteamMatchmaking.GetNumLobbyMembers(lobbyId); i++) - { - string name = SteamFriends.GetFriendPersonaName(SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i)); - if (!PlayerNames.Contains(name)) - PlayerNames.Add(name); - } - LobbyPlayerUpdate?.Invoke(); + Debug.Log("Lobby updated"); + UpdateLobbyData(callback.m_ulSteamIDLobby); } public void HostLobby() @@ -194,6 +200,7 @@ public void HostLobby() SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypeFriendsOnly, transportProtocol.maxConnections); // Disable local joining PlayerInputManagerController.Singleton.RemoveJoinListener(); + // TODO make it possible to have multiple local players in online match, then remove this for (int i = 0; i < PlayerInputManagerController.Singleton.LocalPlayerInputs.Count; i++) { if (i == 0)