Make server send all player updates in one packet
This commit is contained in:
parent
8edd5a81fd
commit
dee0e1e3f1
8
Assets/Scripts/Net.meta
Normal file
8
Assets/Scripts/Net.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5b2b07bcc084a8f449d46fad84fb9149
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -20,6 +20,7 @@ namespace NeonTea.Quakeball.Networking {
|
|||||||
RegisterPacket(typeof(HelloPckt));
|
RegisterPacket(typeof(HelloPckt));
|
||||||
RegisterPacket(typeof(SpawnPckt));
|
RegisterPacket(typeof(SpawnPckt));
|
||||||
RegisterPacket(typeof(SelfIdentPckt));
|
RegisterPacket(typeof(SelfIdentPckt));
|
||||||
|
RegisterPacket(typeof(MultiplePlayerUpdatesPckt));
|
||||||
RegisterPacket(typeof(PlayerUpdatePckt));
|
RegisterPacket(typeof(PlayerUpdatePckt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
||||||
|
|
||||||
LocalPlayer = new NetPlayer(ulong.MaxValue - 1);
|
LocalPlayer = new NetPlayer(ulong.MaxValue - 1);
|
||||||
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player");
|
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Connected(Connection conn) {
|
public override void Connected(Connection conn) {
|
||||||
@ -65,22 +65,32 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
if (spawn.PlayerId == LocalPlayer.Id) {
|
if (spawn.PlayerId == LocalPlayer.Id) {
|
||||||
return; // Ignore, it's their own.
|
return; // Ignore, it's their own.
|
||||||
}
|
}
|
||||||
GameObject obj = Net.SpawnPlayer(spawn.Location);
|
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();
|
||||||
NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
|
NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
|
||||||
Players.Add(spawn.PlayerId, player);
|
Players.Add(spawn.PlayerId, player);
|
||||||
} else if (packet is PlayerUpdatePckt) {
|
} else if (packet is PlayerUpdatePckt) {
|
||||||
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
|
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
|
||||||
if (updatePckt.PlayerId == LocalPlayer.Id) {
|
HandleUpdatePlayer(updatePckt);
|
||||||
return; // Ignore, again.
|
} else if (packet is MultiplePlayerUpdatesPckt) {
|
||||||
|
MultiplePlayerUpdatesPckt multiple = (MultiplePlayerUpdatesPckt)packet;
|
||||||
|
foreach (PlayerUpdatePckt pckt in multiple.Updates) {
|
||||||
|
HandleUpdatePlayer(pckt);
|
||||||
}
|
}
|
||||||
Players[updatePckt.PlayerId].Controlled.GetComponent<RemotePlayer>().QueuePacket(updatePckt);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) {
|
public override void UpdateLocalPlayer() {
|
||||||
if (SelfIdentified) {
|
if (SelfIdentified && Server != null) {
|
||||||
|
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket();
|
||||||
Peer.SendUnreliable(Server.uid, pckt);
|
Peer.SendUnreliable(Server.uid, pckt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void HandleUpdatePlayer(PlayerUpdatePckt pckt) {
|
||||||
|
if (pckt.PlayerId == LocalPlayer.Id) {
|
||||||
|
return; // Ignore, again.
|
||||||
|
}
|
||||||
|
Players[pckt.PlayerId].Controlled.GetComponent<RemotePlayer>().QueuePacket(pckt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
public abstract void Connected(Connection conn);
|
public abstract void Connected(Connection conn);
|
||||||
public abstract void Disconnected(Connection conn);
|
public abstract void Disconnected(Connection conn);
|
||||||
public abstract void Handle(Connection conn, Packet packet);
|
public abstract void Handle(Connection conn, Packet packet);
|
||||||
public abstract void UpdateLocalPlayer(PlayerUpdatePckt pckt);
|
public abstract void UpdateLocalPlayer();
|
||||||
|
|
||||||
public void Stop() {
|
public void Stop() {
|
||||||
if (Peer != null) {
|
if (Peer != null) {
|
||||||
|
@ -17,6 +17,8 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
|
|
||||||
private NetPlayer LocalPlayer = new NetPlayer(ulong.MaxValue);
|
private NetPlayer LocalPlayer = new NetPlayer(ulong.MaxValue);
|
||||||
|
|
||||||
|
private List<NetPlayer> PlayerList = new List<NetPlayer>();
|
||||||
|
|
||||||
public override void Start(string address, int port, PeerMessageListener listener) {
|
public override void Start(string address, int port, PeerMessageListener listener) {
|
||||||
if (Peer != null) {
|
if (Peer != null) {
|
||||||
return;
|
return;
|
||||||
@ -29,9 +31,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
|
|
||||||
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
||||||
|
|
||||||
GameObject obj = GameObject.FindGameObjectWithTag("Player");
|
Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
|
||||||
LocalPlayer.Controlled = obj;
|
LocalPlayer.Controlled = obj;
|
||||||
Players.Add(LocalPlayer.Id, LocalPlayer);
|
AddPlayer(LocalPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Connected(Connection conn) {
|
public override void Connected(Connection conn) {
|
||||||
@ -47,7 +49,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
Peer.SendReliableLater(conn.uid, spawn);
|
Peer.SendReliableLater(conn.uid, spawn);
|
||||||
}
|
}
|
||||||
NetPlayer RemotePlayer = new NetPlayer(PlayerIdCounter++);
|
NetPlayer RemotePlayer = new NetPlayer(PlayerIdCounter++);
|
||||||
Players.Add(RemotePlayer.Id, RemotePlayer);
|
AddPlayer(RemotePlayer);
|
||||||
SelfIdentPckt ident = new SelfIdentPckt();
|
SelfIdentPckt ident = new SelfIdentPckt();
|
||||||
ident.PlayerId = RemotePlayer.Id;
|
ident.PlayerId = RemotePlayer.Id;
|
||||||
Peer.SendReliable(conn.uid, ident);
|
Peer.SendReliable(conn.uid, ident);
|
||||||
@ -55,13 +57,16 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
|
|
||||||
public override void Disconnected(Connection conn) {
|
public override void Disconnected(Connection conn) {
|
||||||
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
|
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) {
|
public override void Handle(Connection conn, Packet packet) {
|
||||||
if (packet is SpawnPckt) {
|
if (packet is SpawnPckt) {
|
||||||
SpawnPckt spawn = (SpawnPckt)packet;
|
SpawnPckt spawn = (SpawnPckt)packet;
|
||||||
if (Players[conn.uid].Controlled == null) {
|
if (Players[conn.uid].Controlled == null) {
|
||||||
GameObject obj = Net.SpawnPlayer(spawn.Location);
|
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();
|
||||||
Players[conn.uid].Controlled = obj;
|
Players[conn.uid].Controlled = obj;
|
||||||
|
|
||||||
spawn = new SpawnPckt();
|
spawn = new SpawnPckt();
|
||||||
@ -96,10 +101,19 @@ namespace NeonTea.Quakeball.Networking.Instances {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) {
|
public override void UpdateLocalPlayer() {
|
||||||
Debug.Log("Update local to everyone!");
|
MultiplePlayerUpdatesPckt pckt = new MultiplePlayerUpdatesPckt(PlayerList);
|
||||||
pckt.PlayerId = LocalPlayer.Id;
|
|
||||||
SendUnreliableToAll(pckt);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,9 +4,9 @@ using NeonTea.Quakeball.Players;
|
|||||||
namespace NeonTea.Quakeball.Networking {
|
namespace NeonTea.Quakeball.Networking {
|
||||||
public class NetPlayer {
|
public class NetPlayer {
|
||||||
public ulong Id;
|
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;
|
Id = id;
|
||||||
Controlled = obj;
|
Controlled = obj;
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,26 @@ namespace NeonTea.Quakeball.Networking.Packets {
|
|||||||
public MultiplePlayerUpdatesPckt() { }
|
public MultiplePlayerUpdatesPckt() { }
|
||||||
|
|
||||||
public MultiplePlayerUpdatesPckt(List<NetPlayer> players) {
|
public MultiplePlayerUpdatesPckt(List<NetPlayer> players) {
|
||||||
|
foreach (NetPlayer p in players) {
|
||||||
|
Updates.Add(p.Controlled.CreatePacket(p.Id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Read(ByteBuffer buffer) {
|
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) {
|
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() { }
|
||||||
|
|
||||||
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;
|
MoveDirection = moveDirection;
|
||||||
MoveStyle = moveStyle;
|
MoveStyle = moveStyle;
|
||||||
Jumping = jumping;
|
Jumping = jumping;
|
||||||
Pitch = pitch;
|
Pitch = pitch;
|
||||||
Yaw = yaw;
|
Yaw = yaw;
|
||||||
|
PlayerId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Read(ByteBuffer buffer) {
|
public override void Read(ByteBuffer buffer) {
|
||||||
|
@ -83,7 +83,7 @@ namespace NeonTea.Quakeball.Players {
|
|||||||
WantsToJump = false;
|
WantsToJump = false;
|
||||||
|
|
||||||
if (Networking.Net.Singleton.Instance != null) {
|
if (Networking.Net.Singleton.Instance != null) {
|
||||||
Networking.Net.Singleton.Instance.UpdateLocalPlayer(Player.CreatePacket());
|
Networking.Net.Singleton.Instance.UpdateLocalPlayer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,8 @@ namespace NeonTea.Quakeball.Players {
|
|||||||
private Vector3 FeetPosition;
|
private Vector3 FeetPosition;
|
||||||
|
|
||||||
/// <summary>Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers.</summary>
|
/// <summary>Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers.</summary>
|
||||||
public PlayerUpdatePckt CreatePacket() {
|
public PlayerUpdatePckt CreatePacket(ulong id = 0) {
|
||||||
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Jumping, Pitch, Yaw);
|
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Jumping, Pitch, Yaw, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>
|
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user