Add nicknames
This commit is contained in:
parent
d2e74fce02
commit
2777f85d61
@ -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();
|
||||
|
@ -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));
|
||||
|
@ -28,7 +28,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
||||
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -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);
|
||||
|
@ -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<Player>();
|
||||
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<Player>();
|
||||
|
@ -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() {
|
||||
|
@ -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 <nickname> [port] [address] - Hosts server at given address and port.");
|
||||
Terminal.RegisterCommand("join", Join, "join <nickname> [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($"<color={Terminal.ERROR_COLOR}>Can't accept more than 2 arguments.</color>");
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
39
Assets/Scripts/Networking/Packets/IdentityPackets.cs
Normal file
39
Assets/Scripts/Networking/Packets/IdentityPackets.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Networking/Packets/IdentityPackets.cs.meta
Normal file
11
Assets/Scripts/Networking/Packets/IdentityPackets.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3459e9d04acc344e82eb16118106a00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user