Implement player sync packet

This commit is contained in:
Jens Pitkänen 2020-08-08 00:59:51 +03:00
parent c37b0b22f0
commit 6f0ff60952
6 changed files with 29 additions and 6 deletions

View File

@ -89,12 +89,12 @@ namespace NeonTea.Quakeball.Networking.Instances {
if (pckt.PlayerId == LocalPlayer.Id) {
return; // Ignore, again.
}
Players[pckt.PlayerId].Controlled.ProcessPacket(ref pckt);
Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(ref pckt);
}
public override void UpdateLocalPlayer() {
if (SelfIdentified && Server != null) {
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket();
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreateUpdatePacket();
Peer.SendUnreliable(Server.uid, pckt);
}
}

View File

@ -78,7 +78,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
if (Players[conn.uid].Controlled != null) {
updatePckt.PlayerId = conn.uid;
Players[conn.uid].Controlled.ProcessPacket(ref updatePckt);
Players[conn.uid].Controlled.ProcessUpdatePacket(ref updatePckt);
}
} else if (packet is PlayerJumpPckt) {
PlayerJumpPckt jump = (PlayerJumpPckt)packet;

View File

@ -15,7 +15,7 @@ namespace NeonTea.Quakeball.Networking.Packets {
if (p.Controlled == null) {
continue;
}
Updates.Add(p.Controlled.CreatePacket(p.Id));
Updates.Add(p.Controlled.CreateUpdatePacket(p.Id));
}
}

View File

@ -59,6 +59,13 @@ namespace NeonTea.Quakeball.Networking.Packets {
public Vector3 Location;
public Vector3 GroundVelocity;
public PlayerSyncPacket() { }
public PlayerSyncPacket(ulong id, Vector3 location, Vector3 groundVelocity) {
PlayerId = id;
Location = location;
GroundVelocity = groundVelocity;
}
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
Location = buffer.ReadVector3();

View File

@ -9,6 +9,7 @@ namespace NeonTea.Quakeball.Players {
public class LocalPlayer : MonoBehaviour {
public Transform Camera;
public bool DisableInput = false;
public float FullSyncFrequency = 1;
private float Lean = 0;
@ -19,6 +20,7 @@ namespace NeonTea.Quakeball.Players {
private InputAction JumpAction;
private float PreviousPlayerUpdate = -1;
private float PreviousPlayerFullSync = -1;
private bool WantsToJump = false;
private void Awake() {
@ -87,6 +89,11 @@ namespace NeonTea.Quakeball.Players {
}
WantsToJump = false;
}
if (Time.time - PreviousPlayerFullSync >= 1f / FullSyncFrequency) {
FullSyncFrequency = Time.time;
// TODO: Create and send the packet
}
}
}
}

View File

@ -61,12 +61,12 @@ 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(ulong id = 0) {
public PlayerUpdatePckt CreateUpdatePacket(ulong id = 0) {
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Pitch, Yaw, id);
}
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>
public void ProcessPacket(ref PlayerUpdatePckt packet) {
public void ProcessUpdatePacket(ref PlayerUpdatePckt packet) {
Pitch = packet.Pitch;
Yaw = packet.Yaw;
MoveDirection = packet.MoveDirection;
@ -74,6 +74,15 @@ namespace NeonTea.Quakeball.Players {
packet = null;
}
/// <summary>Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server</summary>
public PlayerSyncPacket CreateSyncPacket(ulong id = 0) {
return new PlayerSyncPacket(id, transform.position, GroundVelocity);
}
/// <summary>Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server</summary>
public void ProcessSyncPacket(PlayerSyncPacket syncPckt) {
}
/// <summary>The normal of the ground below the player. If there is no ground, it's <c>Vector3.up</c> by default.</summary>
public Vector3 GroundCast() {
RaycastHit Hit;