diff --git a/Assets/Scripts/Networking/GameProtocol.cs b/Assets/Scripts/Networking/GameProtocol.cs index c25e90f..9327f92 100644 --- a/Assets/Scripts/Networking/GameProtocol.cs +++ b/Assets/Scripts/Networking/GameProtocol.cs @@ -23,6 +23,7 @@ namespace NeonTea.Quakeball.Networking { RegisterPacket(typeof(MultiplePlayerUpdatesPckt)); RegisterPacket(typeof(PlayerUpdatePckt)); RegisterPacket(typeof(PlayerJumpPckt)); + RegisterPacket(typeof(PlayerSyncPacket)); } public override void ConnectionStatusChanged(ConnectionStatus oldStatus, ConnectionStatus newStatus, Connection conn) { diff --git a/Assets/Scripts/Networking/Net.cs b/Assets/Scripts/Networking/Net.cs index 185d184..e1f16a1 100644 --- a/Assets/Scripts/Networking/Net.cs +++ b/Assets/Scripts/Networking/Net.cs @@ -1,6 +1,7 @@ using UnityEngine; using NeonTea.Quakeball.Networking.Instances; using NeonTea.Quakeball.TeaNet.Peers; +using NeonTea.Quakeball.TeaNet.Packets; using System.Collections.Generic; namespace NeonTea.Quakeball.Networking { @@ -31,4 +32,22 @@ namespace NeonTea.Quakeball.Networking { Application.quitting += Quit; } } + + + public static class ByteBufferExtensions { + /// Extension: Reads Vector3 + public static Vector3 ReadVector3(this ByteBuffer buffer) { + float x = buffer.ReadFloat(); + float y = buffer.ReadFloat(); + float z = buffer.ReadFloat(); + return new Vector3(x, y, z); + } + + /// Extension: Writes Vector3 + public static void Write(this ByteBuffer buffer, Vector3 vector) { + buffer.Write(vector.x); + buffer.Write(vector.y); + buffer.Write(vector.z); + } + } } diff --git a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs index 7b939f2..2e8d834 100644 --- a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs +++ b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs @@ -23,10 +23,7 @@ namespace NeonTea.Quakeball.Networking.Packets { public override void Read(ByteBuffer buffer) { PlayerId = buffer.ReadULong(); - float x = buffer.ReadFloat(); - float y = buffer.ReadFloat(); - float z = buffer.ReadFloat(); - MoveDirection = new Vector3(x, y, z); + MoveDirection = buffer.ReadVector3(); MoveStyle = buffer.Read(); Pitch = buffer.ReadFloat(); Yaw = buffer.ReadFloat(); @@ -34,9 +31,7 @@ namespace NeonTea.Quakeball.Networking.Packets { public override void Write(ByteBuffer buffer) { buffer.Write(PlayerId); - buffer.Write(MoveDirection.x); - buffer.Write(MoveDirection.y); - buffer.Write(MoveDirection.z); + buffer.Write(MoveDirection); buffer.Write(MoveStyle); buffer.Write(Pitch); buffer.Write(Yaw); @@ -57,4 +52,23 @@ namespace NeonTea.Quakeball.Networking.Packets { buffer.Write(PlayerId); } } + + public class PlayerSyncPacket : Packet { + public ulong PlayerId; + + public Vector3 Location; + public Vector3 GroundVelocity; + + public override void Read(ByteBuffer buffer) { + PlayerId = buffer.ReadULong(); + Location = buffer.ReadVector3(); + GroundVelocity = buffer.ReadVector3(); + } + + public override void Write(ByteBuffer buffer) { + buffer.ReadULong(); + buffer.Write(Location); + buffer.Write(GroundVelocity); + } + } }