Move PlayerUpdatePckt creation and consumption to Player.cs

This commit is contained in:
Jens Pitkänen 2020-08-07 21:12:53 +03:00
parent f313f8aaf4
commit b34edcab2d
3 changed files with 23 additions and 15 deletions

View File

@ -83,8 +83,7 @@ namespace NeonTea.Quakeball.Player {
WantsToJump = false; WantsToJump = false;
if (Net.Net.Singleton.Instance != null) { if (Net.Net.Singleton.Instance != null) {
PlayerUpdatePckt pckt = new PlayerUpdatePckt(Player.MoveDirection, Player.CurrentMoveStyle, Player.Jumping, Player.Pitch, Player.Yaw); Net.Net.Singleton.Instance.UpdateLocalPlayer(Player.CreatePacket());
Net.Net.Singleton.Instance.UpdateLocalPlayer(pckt);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using UnityEngine; using UnityEngine;
using NeonTea.Quakeball.Net.Packets;
namespace NeonTea.Quakeball.Player { namespace NeonTea.Quakeball.Player {
/// <summary>The central glue class for players (both local and remote).</summary> /// <summary>The central glue class for players (both local and remote).</summary>
@ -62,6 +63,21 @@ namespace NeonTea.Quakeball.Player {
private CharacterController CharacterController; private CharacterController CharacterController;
private Vector3 FeetPosition; 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);
}
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>
public void ProcessPacket(ref PlayerUpdatePckt packet) {
Pitch = packet.Pitch;
Yaw = packet.Yaw;
MoveDirection = packet.MoveDirection;
CurrentMoveStyle = packet.MoveStyle;
Jumping = packet.Jumping;
packet = null;
}
/// <summary>The normal of the ground below the player. If there is no ground, it's <c>Vector3.up</c> by default.</summary> /// <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() { public Vector3 GroundCast() {
RaycastHit Hit; RaycastHit Hit;

View File

@ -12,11 +12,13 @@ namespace NeonTea.Quakeball.Player {
public void QueuePacket(PlayerUpdatePckt packet) { public void QueuePacket(PlayerUpdatePckt packet) {
if (QueuedPckt != null) { if (QueuedPckt != null) {
// Re-sync?
ProcessPacket(ref QueuedPckt);
string Warning = "Can't keep up! Got another packet while one was still in queue, fast-forwarding (and probably desyncing)!"; string Warning = "Can't keep up! Got another packet while one was still in queue, fast-forwarding (and probably desyncing)!";
Debug.LogWarning(Warning); Debug.LogWarning(Warning);
Terminal.Singleton.Println($"<color={Terminal.ERROR_COLOR}>{Warning}</color>"); Terminal.Singleton.Println($"<color={Terminal.ERROR_COLOR}>{Warning}</color>");
// Re-sync?
Player.ProcessPacket(ref QueuedPckt);
LastUpdateTime = Time.time;
} }
QueuedPckt = packet; QueuedPckt = packet;
} }
@ -27,18 +29,9 @@ namespace NeonTea.Quakeball.Player {
private void Update() { private void Update() {
if (Time.time - LastUpdateTime >= 1f / Player.UpdateFrequency && QueuedPckt != null) { if (Time.time - LastUpdateTime >= 1f / Player.UpdateFrequency && QueuedPckt != null) {
ProcessPacket(ref QueuedPckt); Player.ProcessPacket(ref QueuedPckt);
LastUpdateTime = Time.time;
} }
} }
private void ProcessPacket(ref PlayerUpdatePckt packet) {
Player.Pitch = packet.Pitch;
Player.Yaw = packet.Yaw;
Player.MoveDirection = packet.MoveDirection;
Player.CurrentMoveStyle = packet.MoveStyle;
Player.Jumping = packet.Jumping;
LastUpdateTime = Time.time;
packet = null;
}
} }
} }