quakeball/Assets/Scripts/Player/RemotePlayer.cs

45 lines
1.6 KiB
C#

using UnityEngine;
using NeonTea.Quakeball.Net.Packets;
using NeonTea.Quakeball.Interface;
namespace NeonTea.Quakeball.Player {
/// <summary>A controller class for a remote player. Gets updates from the network and touches the relevant components.</summary>
[RequireComponent(typeof(Player))]
public class RemotePlayer : MonoBehaviour {
private Player Player;
private PlayerUpdatePckt QueuedPckt = null;
private float LastUpdateTime = -1;
public void QueuePacket(PlayerUpdatePckt packet) {
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)!";
Debug.LogWarning(Warning);
Terminal.Singleton.Println($"<color={Terminal.ERROR_COLOR}>{Warning}</color>");
}
QueuedPckt = packet;
}
private void Awake() {
Player = GetComponent<Player>();
}
private void Update() {
if (Time.time - LastUpdateTime >= 1f / Player.UpdateFrequency && QueuedPckt != null) {
ProcessPacket(ref QueuedPckt);
}
}
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;
}
}
}