From 7fdbd57f589b548a2443aceec1e225ae270b0d13 Mon Sep 17 00:00:00 2001 From: JoshuaReason Date: Sat, 26 Jan 2019 18:15:44 +1100 Subject: [PATCH] Players query for already connected players --- Assets/Scenes/ClientScene.unity | 2 +- .../Multiplayer/Client/ClientManager.cs | 5 ++++ .../Multiplayer/Client/PlayersManager.cs | 1 + .../Scipts/Multiplayer/Core/PlayerProtocol.cs | 1 + .../Multiplayer/Server/PlayerServerManager.cs | 24 +++++++++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Assets/Scenes/ClientScene.unity b/Assets/Scenes/ClientScene.unity index 119338c..36fe09f 100644 --- a/Assets/Scenes/ClientScene.unity +++ b/Assets/Scenes/ClientScene.unity @@ -147,7 +147,7 @@ MonoBehaviour: ConnectOnStart: 0 LobbyScene: LobbyScene GameScene: TestScene - Name: The most nardish of players + Name: The Black Burrito IsConnected: 0 ID: 0 --- !u!4 &914867462 diff --git a/Assets/Scipts/Multiplayer/Client/ClientManager.cs b/Assets/Scipts/Multiplayer/Client/ClientManager.cs index 1258e06..31ebcc7 100644 --- a/Assets/Scipts/Multiplayer/Client/ClientManager.cs +++ b/Assets/Scipts/Multiplayer/Client/ClientManager.cs @@ -88,6 +88,11 @@ namespace Multiplayer Client.SendByChannel(msgType, msg, (int)channel); } + public void SendMessage(short msgType) + { + SendMessage(msgType, new PlayerMsg(ID)); + } + public void StartLobby(NetworkMessage msg) { UnityEngine.SceneManagement.SceneManager.LoadScene(LobbyScene); diff --git a/Assets/Scipts/Multiplayer/Client/PlayersManager.cs b/Assets/Scipts/Multiplayer/Client/PlayersManager.cs index 3a9970d..6851db1 100644 --- a/Assets/Scipts/Multiplayer/Client/PlayersManager.cs +++ b/Assets/Scipts/Multiplayer/Client/PlayersManager.cs @@ -33,6 +33,7 @@ namespace Multiplayer { LocalPlayer = GameObject.Instantiate(LocalPlayerPrefab,transform); LocalPlayer.transform.position = Vector3.zero; + ClientManager.Instance.SendMessage(PlayerMsgID.QueryPosition); } public void InstantiateRemotePlayer(byte playerID) diff --git a/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs b/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs index c24cfa4..e406ca0 100644 --- a/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs +++ b/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs @@ -15,6 +15,7 @@ namespace Multiplayer public const short GameWin = 203; public const short GameOver = 204; public const short GameStart = 205; + public const short QueryPosition = 206; } public class PlayerMsg : MessageBase diff --git a/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs b/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs index a48ef43..4992c26 100644 --- a/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs +++ b/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs @@ -46,5 +46,29 @@ namespace Multiplayer } } + private void QueryPosition(NetworkMessage msg) + { + PlayerMsg playerMsg; + if (!Utility.ReadMessage(msg, out playerMsg)) + return; + + if (!ServerManager.AllPlayers.ContainsKey(playerMsg.ID)) + { + Debug.Log("Recieved query from unknown player"); + return; + } + + Player player = ServerManager.AllPlayers[playerMsg.ID]; + + foreach (Player otherPlayer in ServerManager.AllPlayers.Values) + { + if (otherPlayer.Equals(player)) + continue; + + VectorMsg vectorMsg = new VectorMsg(otherPlayer.ID, otherPlayer.Position); + player.Connection.Send(PlayerMsgID.Position, vectorMsg); + } + } + } }