-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
60 lines (52 loc) · 1.78 KB
/
Copy pathPlayer.cs
File metadata and controls
60 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using ZamboniDedicated.Protocol;
using ZamboniDedicated.Protocol.Packets.SubPackets;
namespace ZamboniDedicated;
public class Player
{
private uint PlayerIdentifier { get; }
public IPEndPoint IpEndpoint { get; }
public ReliableConnection ReliableConnection { get; }
public bool Ready { get; set; }
private List<PlayerInputSubPacket> PendingInputs { get; } = new();
public bool HasEverSentInput { get; private set; }
public bool HasPendingInput => PendingInputs.Count > 0;
public byte InputsConsumedSinceLastTick { get; private set; }
public Player(uint playerIdentifier, IPEndPoint endpoint, UdpClient serverUdpClient, Stopwatch clock)
{
PlayerIdentifier = playerIdentifier;
ReliableConnection = new ReliableConnection(serverUdpClient, endpoint, clock);
IpEndpoint = endpoint;
}
public void ResetConsumedCount() => InputsConsumedSinceLastTick = 0;
public void EnqueueInput(PlayerInputSubPacket input)
{
HasEverSentInput = true;
if (PendingInputs.Count > 0 && PendingInputs[^1].InputKind == InputPayloadKind.Supersedable)
{
PendingInputs[^1] = input;
InputsConsumedSinceLastTick++;
}
else
{
PendingInputs.Add(input);
}
}
public PlayerInputSubPacket TakeInputForTick()
{
if (PendingInputs.Count == 0)
{
return new PlayerInputSubPacket(InputPayloadKind.Empty, Array.Empty<byte>());
}
var input = PendingInputs[0];
PendingInputs.RemoveAt(0);
InputsConsumedSinceLastTick++;
return input;
}
public override string ToString()
{
return $"[[{PlayerIdentifier}][{IpEndpoint}]]";
}
}