diff --git a/Assets/Scripts/Net.meta b/Assets/Scripts/Net.meta new file mode 100644 index 0000000..34d5379 --- /dev/null +++ b/Assets/Scripts/Net.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5b2b07bcc084a8f449d46fad84fb9149 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/GameProtocol.cs b/Assets/Scripts/Networking/GameProtocol.cs index 1b6c4fb..2fbde4a 100644 --- a/Assets/Scripts/Networking/GameProtocol.cs +++ b/Assets/Scripts/Networking/GameProtocol.cs @@ -20,6 +20,7 @@ namespace NeonTea.Quakeball.Networking { RegisterPacket(typeof(HelloPckt)); RegisterPacket(typeof(SpawnPckt)); RegisterPacket(typeof(SelfIdentPckt)); + RegisterPacket(typeof(MultiplePlayerUpdatesPckt)); RegisterPacket(typeof(PlayerUpdatePckt)); } diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index 8d72908..f89fb2a 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -31,7 +31,7 @@ namespace NeonTea.Quakeball.Networking.Instances { Net = GameObject.FindGameObjectWithTag("Net").GetComponent(); LocalPlayer = new NetPlayer(ulong.MaxValue - 1); - LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player"); + LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent(); } public override void Connected(Connection conn) { @@ -65,22 +65,32 @@ namespace NeonTea.Quakeball.Networking.Instances { if (spawn.PlayerId == LocalPlayer.Id) { return; // Ignore, it's their own. } - GameObject obj = Net.SpawnPlayer(spawn.Location); + Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); NetPlayer player = new NetPlayer(spawn.PlayerId, obj); Players.Add(spawn.PlayerId, player); } else if (packet is PlayerUpdatePckt) { PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet; - if (updatePckt.PlayerId == LocalPlayer.Id) { - return; // Ignore, again. + HandleUpdatePlayer(updatePckt); + } else if (packet is MultiplePlayerUpdatesPckt) { + MultiplePlayerUpdatesPckt multiple = (MultiplePlayerUpdatesPckt)packet; + foreach (PlayerUpdatePckt pckt in multiple.Updates) { + HandleUpdatePlayer(pckt); } - Players[updatePckt.PlayerId].Controlled.GetComponent().QueuePacket(updatePckt); } } - public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) { - if (SelfIdentified) { + public override void UpdateLocalPlayer() { + if (SelfIdentified && Server != null) { + PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket(); Peer.SendUnreliable(Server.uid, pckt); } } + + private void HandleUpdatePlayer(PlayerUpdatePckt pckt) { + if (pckt.PlayerId == LocalPlayer.Id) { + return; // Ignore, again. + } + Players[pckt.PlayerId].Controlled.GetComponent().QueuePacket(pckt); + } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Instances/NetInstance.cs b/Assets/Scripts/Networking/Instances/NetInstance.cs index a9fb0c0..0a7fc38 100644 --- a/Assets/Scripts/Networking/Instances/NetInstance.cs +++ b/Assets/Scripts/Networking/Instances/NetInstance.cs @@ -17,7 +17,7 @@ namespace NeonTea.Quakeball.Networking.Instances { public abstract void Connected(Connection conn); public abstract void Disconnected(Connection conn); public abstract void Handle(Connection conn, Packet packet); - public abstract void UpdateLocalPlayer(PlayerUpdatePckt pckt); + public abstract void UpdateLocalPlayer(); public void Stop() { if (Peer != null) { diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index 2431022..6377ff3 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -17,6 +17,8 @@ namespace NeonTea.Quakeball.Networking.Instances { private NetPlayer LocalPlayer = new NetPlayer(ulong.MaxValue); + private List PlayerList = new List(); + public override void Start(string address, int port, PeerMessageListener listener) { if (Peer != null) { return; @@ -29,9 +31,9 @@ namespace NeonTea.Quakeball.Networking.Instances { Net = GameObject.FindGameObjectWithTag("Net").GetComponent(); - GameObject obj = GameObject.FindGameObjectWithTag("Player"); + Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent(); LocalPlayer.Controlled = obj; - Players.Add(LocalPlayer.Id, LocalPlayer); + AddPlayer(LocalPlayer); } public override void Connected(Connection conn) { @@ -47,7 +49,7 @@ namespace NeonTea.Quakeball.Networking.Instances { Peer.SendReliableLater(conn.uid, spawn); } NetPlayer RemotePlayer = new NetPlayer(PlayerIdCounter++); - Players.Add(RemotePlayer.Id, RemotePlayer); + AddPlayer(RemotePlayer); SelfIdentPckt ident = new SelfIdentPckt(); ident.PlayerId = RemotePlayer.Id; Peer.SendReliable(conn.uid, ident); @@ -55,13 +57,16 @@ namespace NeonTea.Quakeball.Networking.Instances { public override void Disconnected(Connection conn) { Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}"); + if (Players.ContainsKey(conn.uid)) { + RemovePlayer(Players[conn.uid]); + } } public override void Handle(Connection conn, Packet packet) { if (packet is SpawnPckt) { SpawnPckt spawn = (SpawnPckt)packet; if (Players[conn.uid].Controlled == null) { - GameObject obj = Net.SpawnPlayer(spawn.Location); + Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); Players[conn.uid].Controlled = obj; spawn = new SpawnPckt(); @@ -96,10 +101,19 @@ namespace NeonTea.Quakeball.Networking.Instances { } } - public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) { - Debug.Log("Update local to everyone!"); - pckt.PlayerId = LocalPlayer.Id; + public override void UpdateLocalPlayer() { + MultiplePlayerUpdatesPckt pckt = new MultiplePlayerUpdatesPckt(PlayerList); SendUnreliableToAll(pckt); } + + 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); + } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/NetPlayer.cs b/Assets/Scripts/Networking/NetPlayer.cs index 674b6ab..3955812 100644 --- a/Assets/Scripts/Networking/NetPlayer.cs +++ b/Assets/Scripts/Networking/NetPlayer.cs @@ -4,9 +4,9 @@ using NeonTea.Quakeball.Players; namespace NeonTea.Quakeball.Networking { public class NetPlayer { public ulong Id; - public GameObject Controlled; + public Player Controlled; - public NetPlayer(ulong id, GameObject obj = null) { + public NetPlayer(ulong id, Player obj = null) { Id = id; Controlled = obj; } diff --git a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs index 32d9b62..352dbc0 100644 --- a/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs +++ b/Assets/Scripts/Networking/Packets/PlayerUpdatePckt.cs @@ -12,12 +12,26 @@ namespace NeonTea.Quakeball.Networking.Packets { public MultiplePlayerUpdatesPckt() { } public MultiplePlayerUpdatesPckt(List players) { + foreach (NetPlayer p in players) { + 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++) { + PlayerUpdatePckt pckt = new PlayerUpdatePckt(); + pckt.Read(buffer); + Updates.Add(pckt); + } } public override void Write(ByteBuffer buffer) { + buffer.Write(Updates.Count); + foreach (PlayerUpdatePckt p in Updates) { + p.Write(buffer); + } } } @@ -33,12 +47,13 @@ namespace NeonTea.Quakeball.Networking.Packets { public PlayerUpdatePckt() { } - public PlayerUpdatePckt(Vector3 moveDirection, byte moveStyle, bool jumping, float pitch, float yaw) { + public PlayerUpdatePckt(Vector3 moveDirection, byte moveStyle, bool jumping, float pitch, float yaw, ulong id = 0) { MoveDirection = moveDirection; MoveStyle = moveStyle; Jumping = jumping; Pitch = pitch; Yaw = yaw; + PlayerId = id; } public override void Read(ByteBuffer buffer) { diff --git a/Assets/Scripts/Players/LocalPlayer.cs b/Assets/Scripts/Players/LocalPlayer.cs index 8fced21..cf11211 100644 --- a/Assets/Scripts/Players/LocalPlayer.cs +++ b/Assets/Scripts/Players/LocalPlayer.cs @@ -83,7 +83,7 @@ namespace NeonTea.Quakeball.Players { WantsToJump = false; if (Networking.Net.Singleton.Instance != null) { - Networking.Net.Singleton.Instance.UpdateLocalPlayer(Player.CreatePacket()); + Networking.Net.Singleton.Instance.UpdateLocalPlayer(); } } } diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index c3a9107..3761538 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -64,8 +64,8 @@ namespace NeonTea.Quakeball.Players { private Vector3 FeetPosition; /// Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers. - public PlayerUpdatePckt CreatePacket() { - return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Jumping, Pitch, Yaw); + public PlayerUpdatePckt CreatePacket(ulong id = 0) { + return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Jumping, Pitch, Yaw, id); } /// Updates this Player with the given packet, and sets it to null. No reusing packets.