-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.cs
More file actions
297 lines (238 loc) · 9.24 KB
/
Copy pathInputHandler.cs
File metadata and controls
297 lines (238 loc) · 9.24 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using Godot;
using System;
using System.Collections;
using System.Collections.Generic;
public class InputHandler : Node
{
Label statusLabel;
string status;
const int INPUT_DELAY = 10;
const int ROLLBACK = 10;
//Number of packets to send when sending. In order to mitigate UDP loses.
const int PACKET_AMOUNT = 1;
byte frameNum;
PacketPeerUDP udpPeer;
Thread networkThread;
bool inputRecieved;
Fobble.Inputs currInput;
Mutex inputRecievedMutex;
string currIcon;
Fobble.CardSlots? currSlot;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
statusLabel = GetParent().GetNode<Label>("NetStatusLabel");
currInput = new Fobble.Inputs();
udpPeer = new PacketPeerUDP();
inputRecieved = false;
inputRecievedMutex = new Mutex();
}
public void StartUDPPeer(string address, int port = 42069)
{
GD.Print(udpPeer.Listen(port, "*"));
GD.Print(udpPeer.SetDestAddress(address, port));
networkThread = new Thread();
networkThread.Start(this, "NetworkThreadInputs");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(float delta)
{
inputRecievedMutex.Lock();
if (Fobble.Instance.gameStatus == Fobble.GameStatus.Playing)
{
inputRecievedMutex.Unlock();
HandleInput();
status = "";
}
else if (Fobble.Instance.gameStatus == Fobble.GameStatus.Waiting)
{
SendHandshake();
inputRecievedMutex.Unlock();
status = "Waiting to connect";
}
else if (Fobble.Instance.gameStatus == Fobble.GameStatus.End)
{
inputRecievedMutex.Unlock();
HandleInput();
status = "GG";
}
statusLabel.Text = status;
}
private void SendHandshake()
{
byte[] deckOrder = Fobble.Instance.deckOrder;
if (deckOrder == null)
{
deckOrder = Fobble.CreateDeck();
Fobble.Instance.deckOrder = deckOrder;
}
byte[] packet = new byte[2 + Fobble.BASE_DECK.Length];
Array.Copy(deckOrder, 0, packet, 2, Fobble.BASE_DECK.Length);
packet[0] = 2;
packet[1] = 0;
//Make a handshake
for (int i = 0; i < PACKET_AMOUNT; i++)
udpPeer.PutPacket(packet);
}
public void InputMade(Fobble.CardSlots slot, string icon)
{
currSlot = slot;
currIcon = icon;
}
private void HandleInput()
{
//Inputs made this frame
currInput.localIcon = currIcon;
currInput.localSlot = currSlot;
SendInput();
if (Fobble.Instance.gameStatus == Fobble.GameStatus.End)
{
if (Input.IsKeyPressed((int)KeyList.Space))
{
GetTree().Quit();
}
return;
}
if (Fobble.Instance.gameStatus != Fobble.GameStatus.Playing)
return;
Fobble.Instance.UpdateAll(currInput);
Fobble.Instance.UpdateUI();
frameNum++;
currIcon = null;
currSlot = null;
currInput.netIcon = null;
currInput.netSlot = null;
}
private void SendInput()
{
//Craft input recieved packet to send to other player
if (currInput.LocalActive)
{
byte[] packet = new byte[5];
packet[0] = 0;
packet[1] = (byte)(frameNum);
packet[2] = currInput.Encoded[0];//Slot
packet[3] = currInput.Encoded[1];//Icon
packet[4] = (byte)Fobble.Instance.deckIndex;//Deck
//Send inputs
for (int i = 0; i < PACKET_AMOUNT; i++)
udpPeer.PutPacket(packet);
}
}
private void NetworkThreadInputs(object data)
{
bool newInput = false;
byte[] result = null;
while (true)
{
inputRecievedMutex.Lock();
if (Fobble.Instance.gameStatus == Fobble.GameStatus.End)
{
inputRecievedMutex.Unlock();
continue;
}
inputRecievedMutex.Unlock();
result = udpPeer.GetPacket();
if (result != null && result.Length >= 1)
{
byte status = result[0];
switch (status)
{
//Input recieved
case 0:
{
//0FSID
inputRecievedMutex.Lock();
//Decode byte inputs
byte frame = result[1];
byte icon = result[3];
byte deckIndex = result[4];
Fobble.CardSlots? slot = null;
if (result[2] != 255)
slot = (Fobble.CardSlots)result[2];
if (deckIndex == Fobble.Instance.deckIndex || deckIndex == 255)
{
currInput.netIcon = icon == 255 ? null : Fobble.SYMBOLS[icon];
currInput.netSlot = slot;
}
newInput = true;
inputRecievedMutex.Unlock();
if (newInput)
{
inputRecievedMutex.Lock();
inputRecieved = true;
if (Fobble.Instance.gameStatus == Fobble.GameStatus.Waiting)
Fobble.Instance.gameStatus = Fobble.GameStatus.Playing;
inputRecievedMutex.Unlock();
}
newInput = false;
break;
}
//Game start / handshake
case 2:
{
inputRecievedMutex.Lock();
if (result[1] == 0)
{
inputRecievedMutex.Unlock();
byte[] deckOrder = new byte[Fobble.BASE_DECK.Length];
byte[] packet = new byte[2 + Fobble.BASE_DECK.Length];
Array.Copy(result, 2, deckOrder, 0, Fobble.BASE_DECK.Length);
Array.Copy(result, 2, packet, 2, Fobble.BASE_DECK.Length);
packet[0] = 2;
packet[1] = 1;
Fobble.Instance.deckOrder = deckOrder;
//Send reply handshake
for (int i = 0; i < PACKET_AMOUNT; i++)
udpPeer.PutPacket(packet);
}
else if (result[1] == 1)
{
inputRecievedMutex.Unlock();
byte[] deckOrder = new byte[Fobble.BASE_DECK.Length];
Array.Copy(result, 2, deckOrder, 0, Fobble.BASE_DECK.Length);
IStructuralEquatable se = deckOrder;
if (se.Equals(Fobble.Instance.deckOrder, StructuralComparisons.StructuralEqualityComparer))
{
Fobble.Instance.deckOrder = deckOrder;
byte[] packet = new byte[2];
packet[0] = 2;
packet[1] = 2;
Fobble.Instance.deckOrder = deckOrder;
//Send reply handshake
for (int i = 0; i < PACKET_AMOUNT; i++)
udpPeer.PutPacket(packet);
}
}
else if (result[1] == 2)
{
if (Fobble.Instance.gameStatus == Fobble.GameStatus.Waiting)
{
frameNum = 0;
Fobble.Instance.gameStatus = Fobble.GameStatus.Playing;
inputRecieved = true;
inputRecievedMutex.Unlock();
byte[] packet = new byte[2];
packet[0] = 2;
packet[1] = 2;
//Send reply handshake
for (int i = 0; i < PACKET_AMOUNT; i++)
udpPeer.PutPacket(packet);
}
inputRecievedMutex.Unlock();
}
break;
}
//Game end
case 3:
{
inputRecievedMutex.Lock();
Fobble.Instance.gameStatus = Fobble.GameStatus.End;
inputRecievedMutex.Unlock();
break;
}
}
}
}
}
}