From 6f0ff60952557c70873c330c32d058262b1a019c Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sat, 8 Aug 2020 00:59:51 +0300 Subject: [PATCH] Implement player sync packet --- Assets/Scripts/Networking/Instances/Client.cs | 4 ++-- Assets/Scripts/Networking/Instances/Server.cs | 2 +- .../Networking/Packets/MultiplePlayerUpdatesPckt.cs | 2 +- .../Scripts/Networking/Packets/PlayerUpdatePckt.cs | 7 +++++++ Assets/Scripts/Players/LocalPlayer.cs | 7 +++++++ Assets/Scripts/Players/Player.cs | 13 +++++++++++-- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index b60e51a..d809eeb 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -89,12 +89,12 @@ namespace NeonTea.Quakeball.Networking.Instances { if (pckt.PlayerId == LocalPlayer.Id) { return; // Ignore, again. } - Players[pckt.PlayerId].Controlled.ProcessPacket(ref pckt); + Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(ref pckt); } public override void UpdateLocalPlayer() { if (SelfIdentified && Server != null) { - PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket(); + PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreateUpdatePacket(); Peer.SendUnreliable(Server.uid, pckt); } } diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index d7974c6..14b5f75 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -78,7 +78,7 @@ namespace NeonTea.Quakeball.Networking.Instances { PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet; if (Players[conn.uid].Controlled != null) { updatePckt.PlayerId = conn.uid; - Players[conn.uid].Controlled.ProcessPacket(ref updatePckt); + Players[conn.uid].Controlled.ProcessUpdatePacket(ref updatePckt); } } else if (packet is PlayerJumpPckt) { PlayerJumpPckt jump = (PlayerJumpPckt)packet; diff --git a/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs b/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs index cda131d..c65a506 100644 --- a/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs +++ b/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs @@ -15,7 +15,7 @@ namespace NeonTea.Quakeball.Networking.Packets { if (p.Controlled == null) { continue; } - Updates.Add(p.Controlled.CreatePacket(p.Id)); + Updates.Add(p.Controlled.CreateUpdatePacket(p.Id)); } } diff --git a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs index 2e8d834..df892a2 100644 --- a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs +++ b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs @@ -59,6 +59,13 @@ namespace NeonTea.Quakeball.Networking.Packets { public Vector3 Location; public Vector3 GroundVelocity; + public PlayerSyncPacket() { } + public PlayerSyncPacket(ulong id, Vector3 location, Vector3 groundVelocity) { + PlayerId = id; + Location = location; + GroundVelocity = groundVelocity; + } + public override void Read(ByteBuffer buffer) { PlayerId = buffer.ReadULong(); Location = buffer.ReadVector3(); diff --git a/Assets/Scripts/Players/LocalPlayer.cs b/Assets/Scripts/Players/LocalPlayer.cs index 5f0867a..a4fc0c0 100644 --- a/Assets/Scripts/Players/LocalPlayer.cs +++ b/Assets/Scripts/Players/LocalPlayer.cs @@ -9,6 +9,7 @@ namespace NeonTea.Quakeball.Players { public class LocalPlayer : MonoBehaviour { public Transform Camera; public bool DisableInput = false; + public float FullSyncFrequency = 1; private float Lean = 0; @@ -19,6 +20,7 @@ namespace NeonTea.Quakeball.Players { private InputAction JumpAction; private float PreviousPlayerUpdate = -1; + private float PreviousPlayerFullSync = -1; private bool WantsToJump = false; private void Awake() { @@ -87,6 +89,11 @@ namespace NeonTea.Quakeball.Players { } WantsToJump = false; } + + if (Time.time - PreviousPlayerFullSync >= 1f / FullSyncFrequency) { + FullSyncFrequency = Time.time; + // TODO: Create and send the packet + } } } } diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index 63ffcbd..0199749 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -61,12 +61,12 @@ namespace NeonTea.Quakeball.Players { private Vector3 FeetPosition; /// Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers. - public PlayerUpdatePckt CreatePacket(ulong id = 0) { + public PlayerUpdatePckt CreateUpdatePacket(ulong id = 0) { return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Pitch, Yaw, id); } /// Updates this Player with the given packet, and sets it to null. No reusing packets. - public void ProcessPacket(ref PlayerUpdatePckt packet) { + public void ProcessUpdatePacket(ref PlayerUpdatePckt packet) { Pitch = packet.Pitch; Yaw = packet.Yaw; MoveDirection = packet.MoveDirection; @@ -74,6 +74,15 @@ namespace NeonTea.Quakeball.Players { packet = null; } + /// Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server + public PlayerSyncPacket CreateSyncPacket(ulong id = 0) { + return new PlayerSyncPacket(id, transform.position, GroundVelocity); + } + + /// Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server + public void ProcessSyncPacket(PlayerSyncPacket syncPckt) { + } + /// The normal of the ground below the player. If there is no ground, it's Vector3.up by default. public Vector3 GroundCast() { RaycastHit Hit;