diff --git a/Minecraft Client/Assets/Scripts/Game/Client.cs b/Minecraft Client/Assets/Scripts/Game/Client.cs index f991c90..ea221b4 100644 --- a/Minecraft Client/Assets/Scripts/Game/Client.cs +++ b/Minecraft Client/Assets/Scripts/Game/Client.cs @@ -148,6 +148,25 @@ public class Client Send(send); } + + public void SendRotationUpdate(Quaternion rotation) + { + string x = rotation.x.ToString("n4"); + string y = rotation.y.ToString("n4"); + string z = rotation.z.ToString("n4"); + string w = rotation.w.ToString("n4"); + + string sendRotation = x + " " + y + " " + z + " " + w; + byte[] sendRotationASCII = Encoding.ASCII.GetBytes(sendRotation); + byte[] send = new byte[sendRotationASCII.Length + 1]; + send[0] = 8; + for (int i = 1; i < send.Length; i++) + { + send[i] = sendRotationASCII[i - 1]; + } + + Send(send); + } private void Receive() { @@ -192,7 +211,27 @@ public class Client { world.RemovePlayer(data[1]); } + + if (data[0] == 8) + { + byte[] rotationASCII = new byte[data.Length - 2]; + for (int i = 0; i < data.Length - 2; i++) + { + rotationASCII[i] = data[i + 2]; + } + + byte id = data[1]; + + string[] rotationString = Encoding.ASCII.GetString(rotationASCII).Split(' '); + float x = float.Parse(rotationString[0]); + float y = float.Parse(rotationString[1]); + float z = float.Parse(rotationString[2]); + float w = float.Parse(rotationString[3]); + + world.UpdatePlayerRotation(new Quaternion(x, y, z, w), id); + } } + } public void EditBlock(Vector3 positionInWorld, byte blockID) diff --git a/Minecraft Client/Assets/Scripts/Game/Player.cs b/Minecraft Client/Assets/Scripts/Game/Player.cs index c313177..5769be8 100644 --- a/Minecraft Client/Assets/Scripts/Game/Player.cs +++ b/Minecraft Client/Assets/Scripts/Game/Player.cs @@ -25,6 +25,7 @@ public class Player : MonoBehaviour private float gravity = -20f; private Camera camera = null; private Vector3 lastPosition = new Vector3(); + private Quaternion lastRotation = new Quaternion(); //network private Client client = null; @@ -56,13 +57,21 @@ public class Player : MonoBehaviour transform.Translate(velocity, Space.World); Vector3 position = transform.position; + Quaternion rotation = transform.rotation; if (lastPosition.x != position.x || lastPosition.y != position.y || lastPosition.z != position.z) { client.SendPositionUpdate(transform.position); } + + if (rotation.w != lastRotation.w || rotation.x != lastRotation.x || rotation.y != lastRotation.y || + rotation.z != lastRotation.z) + { + client.SendRotationUpdate(transform.rotation); + } lastPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z); + lastRotation = transform.rotation; } private void Update() @@ -233,9 +242,9 @@ public class Player : MonoBehaviour } } - public class PlayerUpdateData + public class PlayerPositionUpdateData { - public PlayerUpdateData(byte id, Vector3 position, bool destroy) + public PlayerPositionUpdateData(byte id, Vector3 position, bool destroy) { this.id = id; this.position = position; @@ -246,5 +255,17 @@ public class Player : MonoBehaviour public Vector3 position; public bool destroy; } + + public class PlayerRotationsUpdateData + { + public byte id; + public Quaternion rotation; + + public PlayerRotationsUpdateData(byte id, Quaternion rotation) + { + this.id = id; + this.rotation = rotation; + } + } } diff --git a/Minecraft Client/Assets/Scripts/Game/World.cs b/Minecraft Client/Assets/Scripts/Game/World.cs index 0bf8cc6..a4dedbf 100644 --- a/Minecraft Client/Assets/Scripts/Game/World.cs +++ b/Minecraft Client/Assets/Scripts/Game/World.cs @@ -12,7 +12,10 @@ public class World : MonoBehaviour private string name; private Dictionary otherPlayers = new Dictionary(); private Queue playersToCreate = new Queue(); - private Queue playersToUpdate = new Queue(); + private Queue playersPositionsToUpdate = new Queue(); + + private Queue playersRotationsToUpdate = + new Queue(); //chunks private Chunk[,] chunks; @@ -36,7 +39,7 @@ public class World : MonoBehaviour public void RemovePlayer(byte id) { - playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, Vector3.zero, true)); + playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, Vector3.zero, true)); } private void Update() @@ -58,9 +61,9 @@ public class World : MonoBehaviour otherPlayers.Add(id, player); } - if (playersToUpdate.Count > 0) + if (playersPositionsToUpdate.Count > 0) { - Player.PlayerUpdateData data = playersToUpdate.Dequeue(); + Player.PlayerPositionUpdateData data = playersPositionsToUpdate.Dequeue(); otherPlayers.TryGetValue(data.id, out GameObject g); if (g != null) { @@ -72,11 +75,26 @@ public class World : MonoBehaviour } } } + + if (playersRotationsToUpdate.Count > 0) + { + Player.PlayerRotationsUpdateData data = playersRotationsToUpdate.Dequeue(); + otherPlayers.TryGetValue(data.id, out GameObject g); + if (g != null) + { + g.transform.rotation = data.rotation; + } + } } public void UpdatePlayerPosition(Vector3 position, byte id) { - playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, position, false)); + playersPositionsToUpdate.Enqueue(new Player.PlayerPositionUpdateData(id, position, false)); + } + + public void UpdatePlayerRotation(Quaternion rotation, byte id) + { + playersRotationsToUpdate.Enqueue(new Player.PlayerRotationsUpdateData(id, rotation)); } public void AddPlayer(byte id) diff --git a/Minecraft Server/Assets/Game/Server.cs b/Minecraft Server/Assets/Game/Server.cs index e353762..fb297d1 100644 --- a/Minecraft Server/Assets/Game/Server.cs +++ b/Minecraft Server/Assets/Game/Server.cs @@ -45,6 +45,7 @@ public class Server //[0] = 5: create new player in client //[0] = 6: player ready //[0] = 7: player position update + //[0] = 8: player rotation update private void Receive() { @@ -179,6 +180,41 @@ public class Server } } } + + if (data[0] == 8) + { + byte[] rotationASCII = new byte[data.Length - 1]; + for (int i = 0; i < data.Length - 1; i++) + { + rotationASCII[i] = data[i + 1]; + } + + string[] rotationString = Encoding.ASCII.GetString(rotationASCII).Split(' '); + string x = rotationString[0]; + string y = rotationString[1]; + string z = rotationString[2]; + string w = rotationString[3]; + + byte id = (byte)GetPlayerID(endPoint); + string sendRotation = x + " " + y + " " + z + " " + w; + byte[] sendRotationASCII = Encoding.ASCII.GetBytes(sendRotation); + byte[] send = new byte[sendRotationASCII.Length + 2]; + send[0] = 8; + send[1] = id; + + for (int i = 2; i < send.Length; i++) + { + send[i] = sendRotationASCII[i - 2]; + } + + foreach (IPEndPoint e in players) + { + if (id != GetPlayerID(e)) + { + Send(send, e); + } + } + } } catch (Exception e) { diff --git a/Minecraft Server/Assets/save.world b/Minecraft Server/Assets/save.world index 5a6fb39..e69de29 100644 Binary files a/Minecraft Server/Assets/save.world and b/Minecraft Server/Assets/save.world differ