diff --git a/Assets/Scripts/Networking/CanvasInput.cs b/Assets/Scripts/Networking/CanvasInput.cs index 6a4d447..a4bd8ec 100644 --- a/Assets/Scripts/Networking/CanvasInput.cs +++ b/Assets/Scripts/Networking/CanvasInput.cs @@ -23,12 +23,12 @@ namespace NeonTea.Quakeball.Networking { string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "0.0.0.0" : HostAddr.text; string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text; Debug.Log(addr); - Net.Singleton.StartServer(addr, Int32.Parse(port), this); + Net.Singleton.StartServer(addr, Int32.Parse(port), "", this); }); Join.onClick.AddListener(() => { string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "127.0.0.1" : HostAddr.text; string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text; - Net.Singleton.StartClient(addr, Int32.Parse(port), this); + Net.Singleton.StartClient(addr, Int32.Parse(port), "", this); }); Stop.onClick.AddListener(() => { Net.Quit(); diff --git a/Assets/Scripts/Networking/GameProtocol.cs b/Assets/Scripts/Networking/GameProtocol.cs index 1254891..1b1d2f4 100644 --- a/Assets/Scripts/Networking/GameProtocol.cs +++ b/Assets/Scripts/Networking/GameProtocol.cs @@ -21,6 +21,7 @@ namespace NeonTea.Quakeball.Networking { RegisterPacket(typeof(PingPckt)); RegisterPacket(typeof(SpawnPckt)); RegisterPacket(typeof(SelfIdentPckt)); + RegisterPacket(typeof(NicknamePckt)); RegisterPacket(typeof(PlayerUpdatePckt)); RegisterPacket(typeof(PlayerActionPckt)); RegisterPacket(typeof(PlayerSyncPacket)); diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index 4728e47..3cac4f2 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -28,7 +28,7 @@ namespace NeonTea.Quakeball.Networking.Instances { LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent(); } - public override void Start(string address, int port, PeerMessageListener listener) { + public override void Start(string address, int port, string nick, PeerMessageListener listener) { if (Peer.Running) { return; } @@ -37,6 +37,8 @@ namespace NeonTea.Quakeball.Networking.Instances { Peer.Start(0); byte ident = Peer.RegisterProtocol(new GameProtocol(this)); Peer.Connect(address, port, ident); + + LocalPlayer.Nick = nick; } public override void OnStop() { @@ -84,8 +86,16 @@ namespace NeonTea.Quakeball.Networking.Instances { Players.Add(LocalPlayer.Id, LocalPlayer); SelfIdentified = true; + NicknamePckt nickpckt = new NicknamePckt(LocalPlayer.Nick); + Peer.SendReliable(Server.uid, nickpckt); + SpawnPckt spawn = new SpawnPckt(LocalPlayer.Controlled.transform.position); - Peer.SendReliable(Server.uid, spawn); + Peer.SendReliableLater(Server.uid, spawn); + } else if (packet is NicknamePckt) { + NicknamePckt nick = (NicknamePckt)packet; + if (nick.PlayerId != LocalPlayer.Id) { + Players[nick.PlayerId].Nick = nick.Nick; + } } else if (packet is SpawnPckt) { SpawnPckt spawn = (SpawnPckt)packet; if (spawn.PlayerId == LocalPlayer.Id && spawn.IsInitial) { diff --git a/Assets/Scripts/Networking/Instances/NetInstance.cs b/Assets/Scripts/Networking/Instances/NetInstance.cs index 01ec7e7..2b038bb 100644 --- a/Assets/Scripts/Networking/Instances/NetInstance.cs +++ b/Assets/Scripts/Networking/Instances/NetInstance.cs @@ -19,7 +19,7 @@ namespace NeonTea.Quakeball.Networking.Instances { Peer = new Peer(Fingerprint); } - public abstract void Start(string address, int port, PeerMessageListener listener); + public abstract void Start(string address, int port, string nick, PeerMessageListener listener); public abstract void OnStop(); public abstract void Connected(Connection conn); public abstract void Disconnected(Connection conn); diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index a96b870..7de81c5 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -24,7 +24,7 @@ namespace NeonTea.Quakeball.Networking.Instances { LocalPlayer = new NetPlayer(ulong.MaxValue); } - public override void Start(string address, int port, PeerMessageListener listener) { + public override void Start(string address, int port, string nick, PeerMessageListener listener) { if (Peer.Running) { return; } @@ -38,6 +38,7 @@ namespace NeonTea.Quakeball.Networking.Instances { Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent(); LocalPlayer.Controlled = obj; + LocalPlayer.Nick = nick; AddPlayer(LocalPlayer); } @@ -59,6 +60,9 @@ namespace NeonTea.Quakeball.Networking.Instances { if (p.Controlled == null) { // Not yet initialized, sending later. continue; } + NicknamePckt nick = new NicknamePckt(p.Nick, p.Id); + Peer.SendReliableLater(conn.uid, nick); + SpawnPckt spawn = new SpawnPckt(p.Controlled.transform.position, p.Id); Peer.SendReliableLater(conn.uid, spawn); } @@ -78,7 +82,12 @@ namespace NeonTea.Quakeball.Networking.Instances { } public override void Handle(Connection conn, Packet packet) { - if (packet is SpawnPckt) { + if (packet is NicknamePckt) { + NicknamePckt nick = (NicknamePckt)packet; + Players[conn.uid].Nick = nick.Nick; + nick.PlayerId = conn.uid; + SendReliableToAll(nick); + } else if (packet is SpawnPckt) { SpawnPckt spawn = (SpawnPckt)packet; if (Players[conn.uid].Controlled == null && spawn.IsInitial) { Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); diff --git a/Assets/Scripts/Networking/Net.cs b/Assets/Scripts/Networking/Net.cs index e1f16a1..2237b16 100644 --- a/Assets/Scripts/Networking/Net.cs +++ b/Assets/Scripts/Networking/Net.cs @@ -11,14 +11,14 @@ namespace NeonTea.Quakeball.Networking { public NetInstance Instance; - public void StartClient(string address, int port, PeerMessageListener listener) { + public void StartClient(string address, int port, string nick, PeerMessageListener listener) { Instance = new Client(); - Instance.Start(address, port, listener); + Instance.Start(address, port, nick, listener); } - public void StartServer(string address, int port, PeerMessageListener listener) { + public void StartServer(string address, int port, string nick, PeerMessageListener listener) { Instance = new Server(); - Instance.Start(address, port, listener); + Instance.Start(address, port, nick, listener); } public static void Quit() { diff --git a/Assets/Scripts/Networking/NetChaperone.cs b/Assets/Scripts/Networking/NetChaperone.cs index 25602c8..732d17d 100644 --- a/Assets/Scripts/Networking/NetChaperone.cs +++ b/Assets/Scripts/Networking/NetChaperone.cs @@ -23,8 +23,8 @@ namespace NeonTea.Quakeball.Networking { private void Start() { if (Terminal.Singleton != null) { Terminal = Terminal.Singleton; - Terminal.RegisterCommand("host", Host, "host [port] [address] - Hosts server at given address and port."); - Terminal.RegisterCommand("join", Join, "join [address] [port] - Tries to join a server at given address and port."); + Terminal.RegisterCommand("host", Host, "host [port] [address] - Hosts server at given address and port."); + Terminal.RegisterCommand("join", Join, "join [address] [port] - Tries to join a server at given address and port."); Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s)."); Terminal.RegisterCommand("disconnect", Disconnect, "disconnect - Stop current server, or close current client abruptly."); } @@ -43,23 +43,24 @@ namespace NeonTea.Quakeball.Networking { } private bool Host(string[] args) { - if (args.Length > 2) { + if (args.Length > 3) { Terminal.Println($"Can't accept more than 2 arguments."); return false; } string addr = "0.0.0.0"; string portstr = "8080"; - if (args.Length > 0) { - portstr = args[0]; - } + string nick = args[0]; if (args.Length > 1) { - addr = args[1]; + portstr = args[1]; + } + if (args.Length > 2) { + addr = args[2]; } int port; if (!Int32.TryParse(portstr, out port)) { return false; } - Net.Singleton.StartServer(addr, port, this); + Net.Singleton.StartServer(addr, port, nick, this); return true; } @@ -70,17 +71,18 @@ namespace NeonTea.Quakeball.Networking { } string addr = "127.0.0.1"; string portstr = "8080"; - if (args.Length > 0) { - addr = args[0]; - } + string nick = args[0]; if (args.Length > 1) { - portstr = args[1]; + addr = args[1]; + } + if (args.Length > 2) { + portstr = args[2]; } int port; if (!Int32.TryParse(portstr, out port)) { return false; } - Net.Singleton.StartClient(addr, port, this); + Net.Singleton.StartClient(addr, port, nick, this); return true; } diff --git a/Assets/Scripts/Networking/NetPlayer.cs b/Assets/Scripts/Networking/NetPlayer.cs index 6c3a599..2cacd27 100644 --- a/Assets/Scripts/Networking/NetPlayer.cs +++ b/Assets/Scripts/Networking/NetPlayer.cs @@ -24,12 +24,14 @@ namespace NeonTea.Quakeball.Networking { } } } + public string Nick; public bool Unsynced = false; public float Ping = 0; public NetPlayer(ulong id, Player obj = null) { Id = id; Controlled = obj; + Nick = id.ToString(); } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Packets/IdentityPackets.cs b/Assets/Scripts/Networking/Packets/IdentityPackets.cs new file mode 100644 index 0000000..2cee4dd --- /dev/null +++ b/Assets/Scripts/Networking/Packets/IdentityPackets.cs @@ -0,0 +1,39 @@ +using UnityEngine; +using NeonTea.Quakeball.TeaNet.Packets; + +namespace NeonTea.Quakeball.Networking.Packets { + + public class SelfIdentPckt : Packet { + public ulong PlayerId; + + public override void Read(ByteBuffer buffer) { + PlayerId = buffer.ReadULong(); + } + + public override void Write(ByteBuffer buffer) { + buffer.Write(PlayerId); + } + + } + + public class NicknamePckt : Packet { + public ulong PlayerId; + public string Nick; + + public NicknamePckt() { } + public NicknamePckt(string nick, ulong id = 0) { + Nick = nick; + PlayerId = id; + } + + public override void Read(ByteBuffer buffer) { + PlayerId = buffer.ReadULong(); + Nick = buffer.ReadString(); + } + + public override void Write(ByteBuffer buffer) { + buffer.Write(PlayerId); + buffer.Write(Nick); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Networking/Packets/IdentityPackets.cs.meta b/Assets/Scripts/Networking/Packets/IdentityPackets.cs.meta new file mode 100644 index 0000000..58b1b08 --- /dev/null +++ b/Assets/Scripts/Networking/Packets/IdentityPackets.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e3459e9d04acc344e82eb16118106a00 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/Packets/SpawnPckt.cs b/Assets/Scripts/Networking/Packets/SpawnPckt.cs index f1cdb5c..dc5e350 100644 --- a/Assets/Scripts/Networking/Packets/SpawnPckt.cs +++ b/Assets/Scripts/Networking/Packets/SpawnPckt.cs @@ -50,18 +50,4 @@ namespace NeonTea.Quakeball.Networking.Packets { buffer.Write(KillerPlayerId); } } - - public class SelfIdentPckt : Packet { - - public ulong PlayerId; - - public override void Read(ByteBuffer buffer) { - PlayerId = buffer.ReadULong(); - } - - public override void Write(ByteBuffer buffer) { - buffer.Write(PlayerId); - } - - } } \ No newline at end of file