-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.cs
More file actions
588 lines (490 loc) · 18.4 KB
/
Copy pathClient.cs
File metadata and controls
588 lines (490 loc) · 18.4 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class Client : TcpEngine
{
// Connection
internal string host = "127.0.0.1";
public int port = 8001;
private Transform player;
internal Transform playerCamera;
internal GameObject statsCanvas;
private Vector3 playerPos;
private Vector3 playerRot;
private Vector3 playerCamRot;
public GameObject localPlayerClone;
public NetworkPlayer playerClone;
public Transform fakeLocalPlayer;
const int SERIAL_X = 12345;
const int SERIAL_Y = 67890;
private static bool isHandshaking = true;
public int id;
public string localName = "No name";
public int localWeaponId;
public bool isAlive = false;
// stats
public float kills = 0;
public float deaths = 0;
public bool hasTheLead = false;
public readonly static Queue<Action> RunOnMainThread = new Queue<Action>();
private Thread thread;
public Hashtable playerDatas = new Hashtable();
GlobalResources globalResources;
internal LobbyLog log;
Transform[] spawnPoints;
// init
void Start()
{
// set name
if (PlayerPrefs.HasKey("username")) localName = PlayerPrefs.GetString("username");
statsCanvas = GameObject.Find("Lobby Stats UI");
globalResources = GameObject.Find("/Global Resources").GetComponent<GlobalResources>();
log = GameObject.Find("/MenuSystem/Game Log UI/1_log_container").GetComponent<LobbyLog>();
spawnPoints = GameObject.Find("/Environment/spawns").GetComponentsInChildren<Transform>();
AssignLocalPlayer(null);
ConnectNotBlocking(host, port);
}
// play sound when player gets the lead or loses it
private void CheckTheLead()
{
float maxValue = 0;
foreach (object o in playerDatas.Values)
{
PlayerData p = (PlayerData)o;
if (maxValue < p.kills) maxValue = p.kills;
}
bool lead = kills>maxValue;
if (hasTheLead != lead && GameObject.Find("/Player"))
{
GameObject.Find("/Player").SendMessage("HasTheLead", lead);
hasTheLead = lead;
}
}
// Handle player transform when dead/alive
public void AssignLocalPlayer(Transform newLocalPlayer)
{
if (newLocalPlayer != null) player = newLocalPlayer;
else player = fakeLocalPlayer;
playerCamera = player.GetChild(0);
}
// Send chat message to everyone
void SendChatMessage(string message)
{
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(4); // put int
buffer.Put(message);
Send(buffer.Trim().Get());
}
// Send information when local player dies
internal void LocalPlayerDied(int killerID, int criticalCode)
{
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(3); // put int
buffer.Put(killerID);
buffer.Put(criticalCode);
Send(buffer.Trim().Get());
}
// Send damage caused to other players
internal void DamageDealedTo(int receiverID, float amount, int criticalCode, Vector3 sourcePos)
{
//send to network
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(2); // put int
buffer.Put(receiverID);
buffer.Put(amount);
buffer.Put(criticalCode);
buffer.Put(sourcePos.x);
buffer.Put(sourcePos.y);
buffer.Put(sourcePos.z);
Send(buffer.Trim().Get());
}
// send weapon swap to network
void LocalPlayerChangedToWeapon(int weaponId)
{
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(0); // put int
buffer.Put(weaponId);
Send(buffer.Trim().Get());
localWeaponId = weaponId;
}
void LocalPlayerCrouch(bool value)
{
//TODO
}
void DetonateFinalWord()
{
//detonate id is weapons lenght + 1
int wid = globalResources.weapons.Count+1;
LocalPlayerChangedToWeapon(wid);
}
// Send primary fire to network
void LocalPlayerFiredWeapon()
{
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(1); // put int
Send(buffer.Trim().Get());
}
// Spawn client to the game
public void LocalPlayerSpawn()
{
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(2); // put int
Send(buffer.Trim().Get());
// player obj
if (GameObject.Find("/Dead Player")) Destroy(GameObject.Find("/Dead Player"));
int randomSpawn = UnityEngine.Random.Range(1,spawnPoints.Length);//dont include [0]
GameObject pClone = Instantiate(localPlayerClone,spawnPoints[randomSpawn].position,spawnPoints[randomSpawn].rotation);
pClone.name = "Player";
isAlive = true;
SendAppereances();
}
// Send equipped appereances to network
void SendAppereances()
{
List<GameObject> a = globalResources.appereances;
int holo = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_holo"), typeof(GameObject)) as GameObject);
int head = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_head"), typeof(GameObject)) as GameObject);
int face = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_face"), typeof(GameObject)) as GameObject);
int gloves = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_gloves"), typeof(GameObject)) as GameObject);
int upperBody = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_upperbody"), typeof(GameObject)) as GameObject);
int lowerBody = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_lowerbody"), typeof(GameObject)) as GameObject);
int boots = a.IndexOf(Resources.Load(PlayerPrefs.GetString("equipped_boots"), typeof(GameObject)) as GameObject);
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)2);
buffer.Put(5); // put int
buffer.Put(holo);
buffer.Put(head);
buffer.Put(face);
buffer.Put(gloves);
buffer.Put(upperBody);
buffer.Put(lowerBody);
buffer.Put(boots);
Send(buffer.Trim().Get());
}
public override void ConnectionResolve(bool success)
{
print("Connecting to " + host + ":" + port + " ...");
if (success == false)
{
Debug.LogWarning("Connection failed: " + host);
}
else
{
print("Connected: " + host);
// Start server thread
thread = new Thread(new ThreadStart(Refresh));
thread.IsBackground = true;
thread.Start();
}
}
// Update is called once per frame
void Update()
{
if (!isHandshaking)
{
playerPos = player.position;
playerRot = player.rotation.eulerAngles;
playerCamRot = playerCamera.localRotation.eulerAngles;
ByteBuffer buffer = new ByteBuffer();
buffer.Put((byte)1);
buffer.Put(playerPos.x);
buffer.Put(playerPos.y);
buffer.Put(playerPos.z);
buffer.Put(playerRot.x);
buffer.Put(playerRot.y);
buffer.Put(playerRot.z);
buffer.Put(playerCamRot.x);
buffer.Put(playerCamRot.y);
buffer.Put(playerCamRot.z);
Send(buffer.Trim().Get());
}
while (RunOnMainThread.Count > 0)
{
RunOnMainThread.Dequeue().Invoke();
}
}
public override void Packet(byte[] data)
{
ByteBuffer buffer = new ByteBuffer(data);
if (isHandshaking)
{
// Check if handshake is valid
if (data.Length >= 16)
{
int x = buffer.GetInt();
int y = buffer.GetInt();
if (x == SERIAL_X && y == SERIAL_Y)
{
id = buffer.GetInt();
// Send response back
ByteBuffer sendBuffer = new ByteBuffer();
sendBuffer.Put((byte)0);
sendBuffer.Put(SERIAL_Y);
sendBuffer.Put(SERIAL_X);
sendBuffer.Put(localName); //NAME STRING
RunOnMainThread.Enqueue(() => {
SendAppereances(); //APPEREANCES
});
ByteBuffer z = sendBuffer.Trim();
Send(z.Get());
int playerCount = buffer.GetInt();
for (int i = 0; i < playerCount; i++)
{
int playerId = buffer.GetInt();
PlayerJoined(playerId, buffer.GetString());
ReceiveAppereances(playerId, buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt());
InitPlayerStats(playerId, buffer.GetInt(), buffer.GetInt());
}
isHandshaking = false;
}
}
}
else
{
while (buffer.GetPointer() != data.Length)
{
byte protocol = buffer.GetByte();
if (protocol == 1)
{
UpdatePlayerPositions(buffer);
}
if (protocol == 2)
{
byte argument = buffer.GetByte();
if (argument == 0)
{
PlayerJoined(buffer.GetInt(), buffer.GetString());
}
if (argument == 1)
{
PlayerLeft(buffer.GetInt());
}
if (argument == 2)
{
PlayerChangeWeapon(buffer.GetInt(), buffer.GetInt());
}
if (argument == 3)
{
PlayerFireWeapon(buffer.GetInt());
}
if (argument == 4)
{
DamageReceived(buffer.GetInt(),buffer.GetFloat(),buffer.GetInt(),
new Vector3(buffer.GetFloat(), buffer.GetFloat(), buffer.GetFloat()));
}
if (argument == 5)
{
PlayerDied(buffer.GetInt(), buffer.GetInt(), buffer.GetInt());
}
if (argument == 6)
{
ReceiveChatMessage(buffer.GetInt(),buffer.GetString());
}
if (argument == 7)
{
ReceiveAppereances(buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt(), buffer.GetInt());
}
}
}
}
}
internal void UpdatePlayerPositions(ByteBuffer buffer)
{
int playerCount = buffer.GetInt();
for (int i = 0; i < playerCount; i++)
{
int pid = buffer.GetInt();
if (pid != id)
{
PlayerData pData = (PlayerData)playerDatas[pid];
if (pData != null) {
pData.position = new Vector3(buffer.GetFloat(), buffer.GetFloat(), buffer.GetFloat());
pData.rotation = new Vector3(buffer.GetFloat(), buffer.GetFloat(), buffer.GetFloat());
pData.subRotation = new Vector3(buffer.GetFloat(), buffer.GetFloat(), buffer.GetFloat());
}
else
{
buffer.GetFloat(); // x
buffer.GetFloat(); // y
buffer.GetFloat(); // z
buffer.GetFloat(); // rx
buffer.GetFloat(); // ry
buffer.GetFloat(); // rz
buffer.GetFloat(); // rcx
buffer.GetFloat(); // rcy
buffer.GetFloat(); // rcz
}
}
else
{
// self
buffer.GetFloat(); // x
buffer.GetFloat(); // y
buffer.GetFloat(); // z
buffer.GetFloat(); // rx
buffer.GetFloat(); // ry
buffer.GetFloat(); // rz
buffer.GetFloat(); // rcx
buffer.GetFloat(); // rcy
buffer.GetFloat(); // rcz
}
}
}
// Network player joined
internal void PlayerJoined(int playerID, string playerName)
{
playerDatas.Add(playerID, new PlayerData(playerName));
RunOnMainThread.Enqueue(() =>{
NetworkPlayer clone = Instantiate(playerClone);
clone.networkId = playerID;
clone.networkName = playerName;
});
}
// Network player left
internal void PlayerLeft(int playerID)
{
//Debug.Log("leaved player: " + playerID);
((PlayerData)playerDatas[playerID]).destroy = true;
playerDatas.Remove(playerID);
}
// Network player changes weapon
internal void PlayerChangeWeapon(int playerID, int weaponID)
{
//final w detonate
if (weaponID == globalResources.weapons.Count + 1 && playerID != id) PlayerDetonatedFinalWord(playerID);
else
{
((PlayerData)playerDatas[playerID]).weapon = weaponID;
((PlayerData)playerDatas[playerID]).weaponChanged = true;
}
// lead
RunOnMainThread.Enqueue(() => {
CheckTheLead();
});
}
// Network player fires weapon
internal void PlayerFireWeapon(int playerID)
{
((PlayerData)playerDatas[playerID]).pendingPrimaryFire = true;
}
// Receive chat messages
void ReceiveChatMessage(int senderID, string message)
{
string senderName = host;
string color = "silver";
if (senderID <= 0) color = "red";
else senderName = (senderID == id) ? localName : GetLatestDatas(senderID).name;
RunOnMainThread.Enqueue(() => {
log.AddChatMessage("<color=" + color + ">" + senderName + ": " + "</color>" + message, (senderID == id));
});
}
void PlayerCrouch(int playerId, byte value)
{
if (playerId == id) return; // self
((PlayerData)playerDatas[playerId]).crouch = Convert.ToBoolean(value);
}
void PlayerDetonatedFinalWord(int playerId)
{
if (playerId == id) return; // self
((PlayerData)playerDatas[playerId]).pendingFinalWord = true;
}
// Network player dies
internal void PlayerDied(int killedID, int killerID, int criticalCode)
{
// kill network players
if (killedID != id) ((PlayerData)playerDatas[killedID]).die = true;
RunOnMainThread.Enqueue(() => {
// Get critical type
string criticalType = "killed";
if (criticalCode == 1) criticalType = "headshot";
if (criticalCode == 2) criticalType = "nutshot";
if (criticalCode == 3) criticalType = "smackdown";
// Log Message
if (killerID < 0) // suicides
{
// log
log.LogMessage((killedID == id) ? localName : GetLatestDatas(killedID).name, " suicided","", false, false);
// stats
if (killedID == id) { deaths++; kills--; }
else { ((PlayerData)playerDatas[killedID]).deaths++; ((PlayerData)playerDatas[killedID]).kills--; }
}
else // kills
{
//data
if (killedID != id) ((PlayerData)playerDatas[killedID]).lastKiller=killerID;
// log
log.LogMessage((killerID == id) ? localName : GetLatestDatas(killerID).name, " " + criticalType + " ",(killedID == id) ? localName : GetLatestDatas(killedID).name, false, false);
// stats kills
if (killerID == id) kills++;
else ((PlayerData)playerDatas[killerID]).kills++;
//stats deaths
if (killedID == id) deaths++;
else ((PlayerData)playerDatas[killedID]).deaths++;
}
// Add my kills
if (killerID == id && isAlive) GameObject.Find("/Player").GetComponent<PlayerManager>().GotKill(killedID, criticalCode);
});
}
// Local player receives damage
internal void DamageReceived(int dealerID, float amount, int criticalCode, Vector3 sourcePos)
{
if (isAlive)
{
//Debug.Log("client "+dealerID+" dealed you "+amount+" damage");
RunOnMainThread.Enqueue(() => {
GameObject.Find("/Player").GetComponent<PlayerManager>().TakeDamage(amount, sourcePos, criticalCode, dealerID);
});
}
}
// Receive network player appereances and put them to playerDatas
internal void ReceiveAppereances(int playerID, int holo, int head, int face, int gloves, int upperbody, int lowerbody, int boots)
{
if (playerID == id) return; // skip self
//print(playerID+", "+ holo + ", " + head + ", " + face + ", " + gloves + ", " + upperbody + ", " + lowerbody + ", " + boots);
// save to playerdata
PlayerData pd = ((PlayerData) playerDatas[playerID]);
pd.holo = holo;
pd.head = head;
pd.face = face;
pd.gloves = gloves;
pd.upperbody = upperbody;
pd.lowerbody = lowerbody;
pd.boots = boots;
pd.appereancesChanged = true;
}
// init player stats if join during the game
void InitPlayerStats(int pID, int kills, int deaths)
{
((PlayerData)playerDatas[pID]).kills = kills;
((PlayerData)playerDatas[pID]).deaths = deaths;
}
// Close connection and disable this client gameObject
internal void Quit()
{
Disconnect();
gameObject.SetActive(false);
isHandshaking = true;
}
// This is called every frame by every NetworkPlayer
public PlayerData GetLatestDatas(int networkId)
{
foreach (object i in playerDatas.Keys)
{
if (((int)i) == networkId)
{
return (PlayerData)playerDatas[i];
}
}
return null;
}
}