Make server send all player updates in one packet

This commit is contained in:
Sofia 2020-08-07 22:07:58 +03:00
parent 8edd5a81fd
commit dee0e1e3f1
9 changed files with 69 additions and 21 deletions

8
Assets/Scripts/Net.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5b2b07bcc084a8f449d46fad84fb9149
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,6 +20,7 @@ namespace NeonTea.Quakeball.Networking {
RegisterPacket(typeof(HelloPckt));
RegisterPacket(typeof(SpawnPckt));
RegisterPacket(typeof(SelfIdentPckt));
RegisterPacket(typeof(MultiplePlayerUpdatesPckt));
RegisterPacket(typeof(PlayerUpdatePckt));
}

View File

@ -31,7 +31,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
LocalPlayer = new NetPlayer(ulong.MaxValue - 1);
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player");
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
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<Player>();
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<RemotePlayer>().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<RemotePlayer>().QueuePacket(pckt);
}
}
}

View File

@ -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) {

View File

@ -17,6 +17,8 @@ namespace NeonTea.Quakeball.Networking.Instances {
private NetPlayer LocalPlayer = new NetPlayer(ulong.MaxValue);
private List<NetPlayer> PlayerList = new List<NetPlayer>();
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<NetChaperone>();
GameObject obj = GameObject.FindGameObjectWithTag("Player");
Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
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<Player>();
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);
}
}
}

View File

@ -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;
}

View File

@ -12,12 +12,26 @@ namespace NeonTea.Quakeball.Networking.Packets {
public MultiplePlayerUpdatesPckt() { }
public MultiplePlayerUpdatesPckt(List<NetPlayer> 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) {

View File

@ -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();
}
}
}

View File

@ -64,8 +64,8 @@ namespace NeonTea.Quakeball.Players {
private Vector3 FeetPosition;
/// <summary>Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers.</summary>
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);
}
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>