From 1fe20dbb5d59545d8750b0eca0d9b235511b8dde Mon Sep 17 00:00:00 2001 From: brausjonas <47791011+jonasbraus@users.noreply.github.com> Date: Fri, 13 May 2022 14:21:30 +0200 Subject: [PATCH] player sync --- .../Assets/Scripts/Game/Client.cs | 33 +++++++++++++++++++ .../Assets/Scripts/Game/Player.cs | 10 ++++++ Minecraft Client/Assets/Scripts/Game/World.cs | 9 +++++ Minecraft Server/Assets/Game/Server.cs | 29 ++++++++++++++-- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Minecraft Client/Assets/Scripts/Game/Client.cs b/Minecraft Client/Assets/Scripts/Game/Client.cs index 6e6876a..cef0a47 100644 --- a/Minecraft Client/Assets/Scripts/Game/Client.cs +++ b/Minecraft Client/Assets/Scripts/Game/Client.cs @@ -130,6 +130,24 @@ public class Client Send(new byte[]{6}); } + //1.12 == [1] = 1, [2] = 12 + public void SendPositionUpdate(Vector3 position) + { + string x = position.x.ToString("F"); + string y = position.y.ToString("F"); + string z = position.z.ToString("F"); + byte xV = byte.Parse(x.Split(',')[0]); + byte xN = byte.Parse(x.Split(',')[1]); + + byte yV = byte.Parse(y.Split(',')[0]); + byte yN = byte.Parse(y.Split(',')[1]); + + byte zV = byte.Parse(z.Split(',')[0]); + byte zN = byte.Parse(z.Split(',')[1]); + + Send(new byte[] { 7, xV, xN, yV, yN, zV, zN }); + } + private void Receive() { while(true) @@ -150,6 +168,21 @@ public class Client { world.AddPlayer(data[1]); } + + if (data[0] == 7) + { + string xS = data[1] + "," + data[2]; + string yS = data[3] + "," + data[4]; + string zS = data[5] + "," + data[6]; + + float x = float.Parse(xS); + float y = float.Parse(yS); + float z = float.Parse(zS); + + byte id = data[7]; + + world.UpdatePlayerPosition(new Vector3(x, y, z), id); + } } } diff --git a/Minecraft Client/Assets/Scripts/Game/Player.cs b/Minecraft Client/Assets/Scripts/Game/Player.cs index fbfa164..1a39405 100644 --- a/Minecraft Client/Assets/Scripts/Game/Player.cs +++ b/Minecraft Client/Assets/Scripts/Game/Player.cs @@ -12,6 +12,7 @@ public class Player : MonoBehaviour [SerializeField] private World world; private Camera camera = null; private Rigidbody rigidbody; + private Vector3 lastPosition = new Vector3(); //network private Client client = null; @@ -30,6 +31,15 @@ public class Player : MonoBehaviour { transform.Translate(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.fixedDeltaTime, 0, Input.GetAxisRaw("Vertical") * moveSpeed * Time.fixedDeltaTime); + + Vector3 position = transform.position; + + if (lastPosition.x != position.x || lastPosition.y != position.y || lastPosition.z != position.z) + { + client.SendPositionUpdate(transform.position); + } + + lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z); } private void Update() diff --git a/Minecraft Client/Assets/Scripts/Game/World.cs b/Minecraft Client/Assets/Scripts/Game/World.cs index 1e80ddb..d07fa91 100644 --- a/Minecraft Client/Assets/Scripts/Game/World.cs +++ b/Minecraft Client/Assets/Scripts/Game/World.cs @@ -52,6 +52,15 @@ public class World : MonoBehaviour } } + public void UpdatePlayerPosition(Vector3 position, byte id) + { + otherPlayers.TryGetValue(id, out GameObject g); + if(g != null) + { + g.transform.position = position; + } + } + public void AddPlayer(byte id) { playersToCreate.Enqueue(id); diff --git a/Minecraft Server/Assets/Game/Server.cs b/Minecraft Server/Assets/Game/Server.cs index 6176297..b4a165a 100644 --- a/Minecraft Server/Assets/Game/Server.cs +++ b/Minecraft Server/Assets/Game/Server.cs @@ -12,6 +12,7 @@ public class Server { //game private World world; + private List playerPositions = new List(); //network private UdpClient client; @@ -43,7 +44,7 @@ public class Server //[0] = 4: disconnect player //[0] = 5: create new player in client //[0] = 6: player ready - //[0] = 7: + //[0] = 7: player position update private void Receive() { @@ -75,7 +76,7 @@ public class Server players.Remove(tempEndPoint); Console.WriteLine(playerNames[i] + " left the game" + "\n"); playerNames.RemoveAt(i); - + playerPositions.RemoveAt(i); } } @@ -99,6 +100,7 @@ public class Server //add player players.Add(endPoint); playerNames.Add(name); + playerPositions.Add(new Vector3(0, 0, 0)); } if (data[0] == 3) @@ -133,6 +135,29 @@ public class Server } } } + + if (data[0] == 7) + { + string xS = data[1] + "," + data[2]; + string yS = data[3] + "," + data[4]; + string zS = data[5] + "," + data[6]; + + float x = float.Parse(xS); + float y = float.Parse(yS); + float z = float.Parse(zS); + + int id = GetPlayerID(endPoint); + + playerPositions[id] = new Vector3(x, y, z); + + foreach (IPEndPoint e in players) + { + if (id != GetPlayerID(e)) + { + Send(new byte[]{7, data[1], data[2], data[3], data[4], data[5], data[6], (byte)id}, e); + } + } + } } catch (Exception e) {