From c37b0b22f01b54e61f3f782e3e9ea77e95043cf6 Mon Sep 17 00:00:00 2001 From: teascade Date: Sat, 8 Aug 2020 00:57:11 +0300 Subject: [PATCH] Add SyncPckt part 1 --- Assets/Scripts/Networking/Instances/Client.cs | 10 ++++++ .../Networking/Instances/NetInstance.cs | 1 + Assets/Scripts/Networking/Instances/Server.cs | 23 ++++++++----- .../Packets/MultiplePlayerUpdatesPckt.cs | 33 +++++++++++++++++++ 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index 27a8fbb..b60e51a 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -79,6 +79,9 @@ namespace NeonTea.Quakeball.Networking.Instances { } else if (packet is PlayerJumpPckt) { PlayerJumpPckt jump = (PlayerJumpPckt)packet; Players[jump.PlayerId].Controlled.Jump(); + } else if (packet is PlayerSyncPacket) { + PlayerSyncPacket sync = (PlayerSyncPacket)packet; + } } @@ -102,5 +105,12 @@ namespace NeonTea.Quakeball.Networking.Instances { Peer.SendReliable(Server.uid, jump); } } + + public override void SendPlayerSync() { + if (SelfIdentified && Server != null) { + PlayerSyncPacket packet; // Get the sync packet for LocalPlayer + // SendUnreliable(Server.uid, packet); + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Instances/NetInstance.cs b/Assets/Scripts/Networking/Instances/NetInstance.cs index 70c6cca..997dd09 100644 --- a/Assets/Scripts/Networking/Instances/NetInstance.cs +++ b/Assets/Scripts/Networking/Instances/NetInstance.cs @@ -19,6 +19,7 @@ namespace NeonTea.Quakeball.Networking.Instances { public abstract void Handle(Connection conn, Packet packet); public abstract void UpdateLocalPlayer(); + public abstract void SendPlayerSync(); public abstract void LocalPlayerJump(); public void Stop() { diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index 1d385a0..d7974c6 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -88,9 +88,21 @@ namespace NeonTea.Quakeball.Networking.Instances { SendReliableToAll(jump); } } + } else if (packet is PlayerSyncPacket) { + PlayerSyncPacket sync = (PlayerSyncPacket)packet; } } + private void AddPlayer(NetPlayer player) { + Players.Add(player.Id, player); + PlayerList.Add(player); + } + + private void RemovePlayer(NetPlayer player) { + Players.Remove(player.Id); + PlayerList.Remove(player); + } + public void SendReliableToAll(Packet packet, ulong except = ulong.MaxValue) { foreach (NetPlayer p in Players.Values) { if (p.Id == ulong.MaxValue || p.Id == except) { @@ -119,14 +131,9 @@ namespace NeonTea.Quakeball.Networking.Instances { SendReliableToAll(jump); } - private void AddPlayer(NetPlayer player) { - Players.Add(player.Id, player); - PlayerList.Add(player); - } - - private void RemovePlayer(NetPlayer player) { - Players.Remove(player.Id); - PlayerList.Remove(player); + public override void SendPlayerSync() { + MultipleSyncsPckt pckt = new MultipleSyncsPckt(PlayerList); + SendUnreliableToAll(pckt); } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs b/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs index c548352..cda131d 100644 --- a/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs +++ b/Assets/Scripts/Networking/Packets/MultiplePlayerUpdatesPckt.cs @@ -36,4 +36,37 @@ namespace NeonTea.Quakeball.Networking.Packets { } } } + + public class MultipleSyncsPckt : Packet { + + public List Updates = new List(); + + public MultipleSyncsPckt() { } + + public MultipleSyncsPckt(List players) { + foreach (NetPlayer p in players) { + if (p.Controlled == null) { + continue; + } + //Updates.Add(p.Controlled.CreatePacket(p.Id)); + } + } + + public override void Read(ByteBuffer buffer) { + Updates.Clear(); + int count = buffer.ReadInt(); + for (int i = 0; i < count; i++) { + PlayerSyncPacket pckt = new PlayerSyncPacket(); + pckt.Read(buffer); + Updates.Add(pckt); + } + } + + public override void Write(ByteBuffer buffer) { + buffer.Write(Updates.Count); + foreach (PlayerSyncPacket p in Updates) { + p.Write(buffer); + } + } + } }