forked from kroyee/SpeedBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameFieldDrawer.cpp
653 lines (611 loc) · 16 KB
/
gameFieldDrawer.cpp
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include "gui.h"
#include "optionSet.h"
#include "gamePlay.h"
#include "network.h"
#include <iostream>
using namespace std;
void GameFieldDrawer::addField(obsField& field) {
fields.push_back(field);
calFieldPos();
drawOppField(fields.back());
}
void GameFieldDrawer::removeField(sf::Uint16 id) {
for (auto it = fields.begin(); it != fields.end(); it++)
if (it->id == id) {
it = fields.erase(it);
break;
}
calFieldPos();
}
void GameFieldDrawer::removeAllFields() {
while (fields.size())
fields.pop_front();
}
void GameFieldDrawer::updateField(obsField& field) {
for (auto it = fields.begin(); it != fields.end(); it++)
if (it->id == field.id) {
for (int y=0; y<22; y++)
for (int x=0; x<10; x++)
it->square[y][x] = field.square[y][x];
it->nextpiece = field.nextpiece;
it->nprot = field.nprot;
it->npcol = field.npcol;
drawOppField(*it);
}
}
void GameFieldDrawer::calFieldPos() {
float r = 600/440.0;
short width = 490, height = 555, startx = 465, starty = 40;
short total = fields.size(), placed = 0;
short field_w = width;
short field_h = r*field_w;
if (field_h>height) {
field_h=height;
field_w=field_h/r;
}
bool done = false;
short x, y, rows;
while (!done) {
x=0; y=0; rows=1;
for (placed = 0; placed < total; placed++) {
if (x+field_w > width) { x=0; y+=field_h; rows++; }
if (y+field_h > height)
break;
x+=field_w;
}
if (placed >= total)
done=true;
else {
field_w--;
field_h=field_w*r;
}
}
short perrow = (float)total/rows + 0.999999;
r = field_w/440.0;
short onrow=0, currow=0;
short(*origin)[2] = new short[total][2];
short c;
for (c=0; c<total; c++) {
origin[c][0] = startx + (onrow+1)*((width-field_w*perrow) / (perrow+1)) + onrow * field_w;
origin[c][1] = starty + (currow+1)*((height-field_h*rows) / (rows+1)) + field_h*currow;
onrow++;
if (onrow==perrow) {
onrow=0;
currow++;
}
}
c=0;
for (auto &&it : fields) { //start 480, 0
short originX = (480-origin[c][0]) / (r-1);
short originY = (origin[c][1] / (r-1))*-1;
it.sprite.setOrigin(originX, originY);
it.sprite.setPosition(480+originX, originY);
it.sprite.setScale(r, r);
c++;
}
delete[] origin;
origin = nullptr;
currentR = r;
}
void GameFieldDrawer::resetOppFields() {
for (auto&& field : fields) {
field.position=0;
field.datacount=250;
field.posX=0;
field.posY=0;
field.clear();
drawOppField(field);
}
}
void GameFieldDrawer::drawOppField(obsField& field) {
field.drawField();
for (int rot=0; rot < field.nprot; rot++)
options->basepiece[field.nextpiece].rcw();
for (int y=0; y<4; y++)
for (int x=0; x<4; x++)
if (options->basepiece[field.nextpiece].grid[y][x] != 0) {
field.tile[field.npcol-1].setPosition(sf::Vector2f(-15*options->basepiece[field.nextpiece].lpiece+330+x*30, 45+y*30));
field.texture.draw(field.tile[field.npcol-1]);
}
for (int rot=0; rot < field.nprot; rot++)
options->basepiece[field.nextpiece].rccw();
}
void GameFieldDrawer::drawFields() {
if (!gui.get("GameFields")->isVisible())
return;
for (auto &&it : fields)
window->draw(it.sprite);
if (scaleup) {
scaleup->scale += sclock.restart().asMilliseconds() / 5;
if (scaleup->scale > 100)
scaleup->scale = 100;
float maxRat = 0.85, add;
add = (maxRat-currentR)/100;
if (add>0) {
scaleup->sprite.setScale(currentR+add*scaleup->scale, currentR+add*scaleup->scale);
window->draw(scaleup->sprite);
}
}
}
sf::String GameFieldDrawer::getName(sf::Uint16 id) {
if (id == myId)
return game->field.name;
for (auto&& field : fields)
if (field.id == id)
return field.name;
return "Unknown";
}
void GameFieldDrawer::goAway() {
away=true;
net->packet.clear();
sf::Uint8 packetid = 8; //8-Packet
net->packet << packetid;
net->sendTCP();
game->gameover=true;
game->sendgameover=true;
game->awayText.setString("Away");
game->drawGameOver();
}
void GameFieldDrawer::unAway() {
away=false;
game->autoaway=false;
net->packet.clear();
sf::Uint8 packetid = 9; //9-Packet
net->packet << packetid;
net->sendTCP();
game->awayText.setString("");
game->drawGameOver();
}
void GameFieldDrawer::handleEvent(sf::Event event) {
bool selectchat=false;
if (setkey)
putKey(event);
if (gui.get("GameFields")->isVisible()) {
if (event.type == sf::Event::MouseMoved) {
sf::Vector2f pos = window->mapPixelToCoords(sf::Mouse::getPosition(*window));
sf::FloatRect box;
if (scaleup) {
box = scaleup->sprite.getGlobalBounds();
if (!box.contains(pos)) {
scaleup->scale=0;
scaleup->sprite.setScale(currentR, currentR);
scaleup=0;
}
}
for (auto &&it : fields) {
box = it.sprite.getGlobalBounds();
if (box.contains(pos)) {
if (&it != scaleup) {
if (scaleup) {
scaleup->scale=0;
scaleup->sprite.setScale(currentR, currentR);
}
scaleup=⁢
scaleup->scale=0;
sclock.restart();
break;
}
}
}
}
else if (event.type == sf::Event::MouseLeft && scaleup) {
scaleup->scale=0;
scaleup->sprite.setScale(currentR, currentR);
scaleup=0;
}
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
if (chatFocused) {
gui.get("ChatBox", 1)->unfocus();
gui.get("slChatBox", 1)->unfocus();
}
else if (gui.get("Login")->isVisible()) {
closeLogin();
}
else if (gui.get("MainMenu")->isVisible()) {
if (gui.get("AUS")->isVisible()) {
gui.get("AUS")->hide();
gui.get("MainMenu")->enable();
}
else {
gui.get<tgui::Label>("AUSL", 1)->setText("Do you want to quit?");
gui.get("AUS")->show();
gui.get("MainMenu")->disable();
}
}
else if (gui.get("OptTab")->isVisible())
gui.get<tgui::Tab>("OptTab")->select("Back");
else if (gui.get("opTab")->isVisible())
gui.get<tgui::Tab>("opTab")->select("Back");
else if (gui.get("InGameTab")->isVisible()) {
if (gui.get("AUS")->isVisible())
gui.get("AUS")->hide();
else {
gui.get<tgui::Label>("AUSL", 1)->setText("Leave this game?");
gui.get("AUS")->show();
}
}
}
else if (event.key.code == sf::Keyboard::Return) {
if (playonline) {
if (!chatFocused) {
if (inroom) {
gui.get("GameFields")->hide();
gui.get("Score")->hide();
gui.get("Chat")->show();
selectchat=true;
}
}
}
}
}
if (gui.get("Rooms")->isVisible())
if (event.type == sf::Event::MouseWheelScrolled)
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
if (!gui.get("RoomScroll", 1)->isFocused()) {
short cur = gui.get<tgui::Scrollbar>("RoomScroll", 1)->getValue();
cur-=event.mouseWheelScroll.delta;
if (cur<0)
cur=0;
gui.get<tgui::Scrollbar>("RoomScroll", 1)->setValue(cur);
}
if (gui.get("QuickMsg")->isVisible())
if (quickMsgClock.getElapsedTime() > sf::seconds(5))
gui.get("QuickMsg")->hide();
gui.handleEvent(event);
if (selectchat)
gui.get("ChatBox", 1)->focus();
}
void GameFieldDrawer::sendGameState() { //UDP Packet outgoing
compressor.compress();
net->packet.clear();
sf::Uint8 packetid = 100;
net->packet << packetid << myId << gamedatacount;
gamedatacount++;
for (int i=0; i<compressor.tmpcount; i++)
net->packet << compressor.tmp[i];
if (compressor.bitcount>0)
net->packet << compressor.tmp[compressor.tmpcount];
net->sendUDP();
}
void GameFieldDrawer::sendGameData() {
sf::Time tmp = game->keyclock.getElapsedTime();
if (tmp>gamedata) {
gamedata=tmp+sf::milliseconds(100);
sendGameState();
}
if (game->linesSent > linesSent) { //5-Packet
sf::Uint8 packetid=5;
sf::Uint8 amount = game->linesSent-linesSent;
net->packet.clear();
net->packet << packetid << amount;
net->sendTCP();
linesSent = game->linesSent;
}
if (game->garbageCleared > garbageCleared) { //6-Packet
sf::Uint8 packetid=6;
sf::Uint8 amount = game->garbageCleared-garbageCleared;
net->packet.clear();
net->packet << packetid << amount;
net->sendTCP();
garbageCleared = game->garbageCleared;
}
if (game->linesBlocked > linesBlocked) { //7-Packet
sf::Uint8 packetid=7;
sf::Uint8 amount = game->linesBlocked-linesBlocked;
net->packet.clear();
net->packet << packetid << amount;
net->sendTCP();
linesBlocked = game->linesBlocked;
}
}
void GameFieldDrawer::sendGameOver() { //3-Packet
net->packet.clear();
sf::Uint8 packetid = 3;
net->packet << packetid << game->maxCombo << game->linesSent << game->linesRecieved << game->linesBlocked << game->bpm << game->linesPerMinute;
net->sendTCP();
game->sendgameover=false;
sendGameState();
}
void GameFieldDrawer::sendGameWinner() { //4-Packet
net->packet.clear();
sf::Uint8 packetid = 4;
net->packet << packetid << game->maxCombo << game->linesSent << game->linesRecieved << game->linesBlocked << game->bpm << game->linesPerMinute;
net->sendTCP();
game->winner=false;
}
void GameFieldDrawer::handlePacket() {
if (net->packetid !=100)
cout << "Packet id: " << (int)net->packetid << endl;
switch ((int)net->packetid) {
case 255: //Disconnect
gui.get("opTab")->hide();
gui.get("Rooms")->hide();
gui.get("ServerLobby")->hide();
gui.get("AUS")->hide();
gui.get("Connecting")->hide();
gui.get("MainMenu")->enable();
mainMenu();
disconnect=true;
break;
case 100: //Game data
{
sf::Uint16 clientid;
sf::Uint8 datacount;
net->packet >> clientid >> datacount;
for (auto&& field : fields)
if (field.id==clientid) {
if (datacount>field.datacount || (datacount<50 && field.datacount>200)) {
field.datacount=datacount;
for (int c=0; net->packet >> compressor.tmp[c]; c++) {}
compressor.extract();
if (compressor.validate()) {
compressor.field = &field;
compressor.copy();
drawOppField(field);
}
}
break;
}
}
break;
case 0: //Welcome packet
{
sf::String name;
sf::Uint8 roomCount, maxPlayers, currentPlayers;
sf::Uint16 id;
net->packet >> myId >> name >> roomCount;
lobbyMsg("Server", name);
removeAllRooms();
for (int i=0; i<roomCount; i++) {
net->packet >> id >> name >> currentPlayers >> maxPlayers;
addRoom(name, currentPlayers, maxPlayers, id);
}
}
break;
case 1://Start countdown
{
sf::Uint8 countdown;
sf::Uint16 seed1, seed2;
net->packet >> countdown >> seed1 >> seed2;
game->rander.seedPiece(seed1);
game->rander.seedHole(seed2);
game->rander.reset();
game->startCountdown();
game->countDown(countdown);
resetOppFields();
startcount=true;
gamedatacount=251;
sendGameState();
}
break;
case 2://Countdown
{
if (!game->rander.total)
game->startCountdown();
sf::Uint8 countdown;
net->packet >> countdown;
game->countDown(countdown);
if (!countdown) {
startgame=true;
gamedatacount=0;
gamedata=sf::seconds(0);
}
else {
gamedatacount=255-countdown;
sendGameState();
}
}
break;
case 3://Join room ok/no
{
sf::Uint8 joinok;
net->packet >> joinok;
if (joinok) {
sf::Uint8 playersinroom;
sf::Uint16 playerid, seed1, seed2;
net->packet >> seed1 >> seed2 >> playersinroom;
game->rander.seedPiece(seed1);
game->rander.seedHole(seed2);
game->rander.reset();
obsField newfield(textureBase->tile, &textureBase->fieldBackground);
newfield.clear();
sf::String name;
for (int c=0; c<playersinroom; c++) {
net->packet >> playerid >> name;
newfield.id = playerid;
newfield.setName(name, *printFont);
addField(newfield);
}
inroom=true;
gui.get("InGameTab")->show();
gui.get<tgui::Tab>("InGameTab")->select(0);
gui.get("GameFields")->show();
gui.get("opTab")->hide();
gui.get("Rooms")->hide();
game->makeNewPiece();
game->field.clear();
game->countdownText.setString("");
game->preDraw();
}
else {} //Show "Couldn't join room" message
}
break;
case 4: //Another player joined room
{
sf::String name;
obsField newfield(textureBase->tile, &textureBase->fieldBackground);
newfield.clear();
net->packet >> newfield.id >> name;
newfield.setName(name, *printFont);
addField(newfield);
}
break;
case 5: //Another player left the room
{
sf::Uint16 removeid;
net->packet >> removeid;
removeField(removeid);
}
break;
case 6: //Round ended
game->gameover=true;
break;
case 7: //Round ended and you where the winner
game->gameover=true;
game->winner=true;
game->autoaway=false;
break;
case 8: // Round score data
{
sf::Uint8 count, maxcombo, rank, position;
sf::Uint16 id, linesSent, linesRecieved, linesBlocked, bpm, spm, score;
float linesAdjusted;
net->packet >> count;
clearScore();
for (int i=0; i<count; i++) {
net->packet >> id >> maxcombo >> linesSent >> linesRecieved >> linesBlocked >> bpm >> spm >> rank >> position >> score >> linesAdjusted;
scoreRow(getName(id), score, rank, bpm, maxcombo, linesSent, linesAdjusted, spm, linesRecieved, linesBlocked);
}
}
break;
case 9: // Auth result
{
sf::Uint8 success;
net->packet >> success;
if (success == 1) {
sf::String name;
net->packet >> name >> myId;
game->field.setName(name, *printFont);
gui.get("Connecting")->hide();
gui.get("opTab")->show();
gui.get("Rooms")->show();
gui.get<tgui::Tab>("opTab")->select(0);
gui.get("MainMenu")->enable();
}
else if (success == 2) {
gui.get("Connecting")->hide();
gui.get("opTab")->show();
gui.get("Rooms")->show();
gui.get<tgui::Tab>("opTab")->select(0);
gui.get("MainMenu")->enable();
}
else {
net->disconnect();
playonline=false;
if (success == 3)
quickMsg("You have the wrong client version");
else if (success == 4)
quickMsg("Name already in use");
else
quickMsg("Authentication failed");
gui.get("Connecting")->hide();
gui.get("MainMenu")->show();
gui.get("Login")->show();
}
}
break;
case 10: //Garbage received
{
sf::Uint8 amount;
net->packet >> amount;
game->addGarbage(amount);
}
break;
case 11: //Server telling me to reset my oppfields. This is the same as Packet 1, but when client is away.
{
resetOppFields();
sf::Uint16 seed;
net->packet >> seed;
game->rander.seedPiece(seed);
net->packet >> seed;
game->rander.seedHole(seed);
game->rander.reset();
game->position=0;
game->countdownText.setString("");
game->drawGameOver();
}
break;
case 12: // Incoming chat msg
{
sf::String from, msg;
sf::Uint8 type;
net->packet >> type >> from >> msg;
if (type == 3)
privMsg(from, msg);
else if (type == 2)
lobbyMsg(from, msg);
else
roomMsg(from, msg);
}
break;
case 13: // Another player went away
{
sf::Uint16 id;
net->packet >> id;
for (auto&& field : fields)
if (field.id == id) {
field.away=true;
drawOppField(field);
}
}
break; // Another player came back
case 14:
{
sf::Uint16 id;
net->packet >> id;
for (auto&& field : fields)
if (field.id == id) {
field.away=false;
drawOppField(field);
}
}
break;
case 15: // Server reported the position of a player
{
sf::Uint16 id;
sf::Uint8 position;
net->packet >> id >> position;
for (auto&& field : fields)
if (field.id == id) {
field.position = position;
drawOppField(field);
}
if (id == myId) {
game->position = position;
game->drawGameOver();
}
}
break;
case 16: // Server sending room list
{ // This is not being used yet, but you could put a "refresh" button in the lobby for the furture?
sf::String name;
sf::Uint8 roomCount, maxPlayers, currentPlayers;
sf::Uint16 id;
net->packet >> roomCount;
removeAllRooms();
for (int i=0; i<roomCount; i++) {
net->packet >> id >> name >> currentPlayers >> maxPlayers;
addRoom(name, currentPlayers, maxPlayers, id);
}
}
break;
case 17: // New room created
{
sf::String name;
sf::Uint16 id;
sf::Uint8 maxPlayers, currentPlayers;
net->packet >> id >> name >> currentPlayers >> maxPlayers;
addRoom(name, currentPlayers, maxPlayers, id);
}
break;
case 18: // Room was removed
{
sf::Uint16 id;
net->packet >> id;
removeRoom(id);
}
break;
}
}