-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
727 lines (582 loc) · 19.3 KB
/
server.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#include <SFML/Graphics.hpp>
#include <string>
#include <SFML/Graphics.hpp>
#include "AnimatedSprite.hpp"
#include <iostream>
#include <math.h>
#include "json.hpp"
using json = nlohmann::json;
#include "./UDP/UDPSystem.hpp"
#define TIMEOUT 10000 //timeout value for recvPacket()
//TO_* used as param. for sendPacket()
#define TO_BOTH_PLAYER 0
#define TO_PLAYER_ONE 1
#define TO_PLAYER_TWO 2
char CONNECT_VALUE[] = "start"; //used to compare msg's content
enum State { walkLeft, walkRight, crouchLeft, crouchRight , idleLeft, idleRight, jumpLeft, jumpRight, chuckLeft, chuckRight , die };
enum Input { none, up, left, right, down, action, enter};
float FLOOR_HEIGHT = 100.0;
#define PI 3.14159265
sf::Texture texture;
sf::Font font;
Animation idleAnimationLeft(false,"idleLeft");
Animation crouchingAnimationLeft(false,"crouchLeft");
Animation walkingAnimationLeft(true,"walkLeft");
Animation idleAnimationRight(false, "idleRight");
Animation crouchingAnimationRight(false,"crouchRight");
Animation walkingAnimationRight(true,"walkRight");
Animation hammerMovingRight(true, "hammerMoveRight");
Animation hammerMovingLeft(true, "hammerMoveLeft");
Animation hammerIdleRight(true, "hammerIdleRight");
Animation hammerIdleLeft(true, "hammerIdleLeft");
float scalingFactor = 3.5;
sf::Sprite s;
float walkSpeed = 200;
float stopFactor = 0.10 ;
float gravity = 9.8;
float jumpSpeed = 500;
int carryList[2] = { 0 };
int chuckList[2] = { 0 };
int teamList[2] = { 0 };
int victory = 0;
std::pair<float, float> p1V, p2V, h1V, h2V;
sf::Vector2i screenDimensions(800, 600);
sf::RectangleShape floorBox(sf::Vector2f(screenDimensions.x, FLOOR_HEIGHT));
sf::RectangleShape halfLine(sf::Vector2f(10, FLOOR_HEIGHT));
//based on input, se state correctly
State getCurrentState(AnimatedSprite sprite, Input input) {
State previousState = static_cast<State>(sprite.getPrevState());
State currentState = static_cast<State>(sprite.getCurrState());;
if (input == action)
{
if (previousState == chuckLeft || previousState == chuckRight) {
chuckList[sprite.getTeam() - 1] = 0;
currentState = previousState;
}
else {
if ((currentState == idleRight) || (currentState == walkRight) || (currentState == crouchRight) || (currentState == idleRight) || (currentState == jumpRight)) {
currentState = chuckRight;
chuckList[sprite.getTeam() - 1] = 2;
}
else {
currentState = chuckLeft;
chuckList[sprite.getTeam() - 1] = 1;
}
}
}
else if (input == down)
{
if ((currentState == walkLeft) || (currentState == idleLeft) || (currentState == chuckLeft) || (currentState == jumpLeft) || (currentState == crouchLeft)) {
currentState = crouchLeft;
}
else {
currentState = crouchRight;
}
}
else if (input == left)
{
if ((currentState == jumpLeft) || (currentState == jumpRight)) {
currentState = jumpLeft;
}
else if (currentState == crouchRight) {
currentState = crouchLeft;
}
else
currentState = walkLeft;
}
else if (input == right)
{
if ((currentState == jumpRight) || (currentState == jumpRight)) {
currentState = jumpRight;
}
else if (currentState == crouchLeft) {
currentState = crouchRight;
}
else
currentState = walkRight;
}
else if (input == up)
{
if ((currentState == idleRight) || (currentState == walkRight) || (currentState == crouchRight) || (currentState == idleRight) || (currentState == jumpRight)) {
currentState = jumpRight;
}
else
currentState = jumpLeft;
}
// if no key was pressed stop the animation
else if (input == none)
{
if ((currentState == walkLeft) || (currentState == idleLeft) || (currentState == chuckLeft) || (currentState == jumpLeft) || (currentState == crouchLeft)) {
currentState = idleLeft;
}
else
currentState = idleRight;
}
return currentState;
}
//set the correct animation of the sprite based on state
const Animation* getCurrentAnimation(AnimatedSprite sprite,bool isHammer) {
const Animation* tmp(NULL);
State current = static_cast<State>(sprite.getCurrState());
if (isHammer) {
tmp = &hammerIdleRight;
}
else {
if (current == idleLeft) {
tmp = &idleAnimationLeft;
}
else if (current == idleRight) {
tmp = &idleAnimationRight;
}
else if (current == walkLeft) {
tmp = &walkingAnimationLeft;
}
else if (current == walkRight) {
tmp = &walkingAnimationRight;
}
else if (current == crouchLeft) {
tmp = &crouchingAnimationLeft;
}
else
tmp = &crouchingAnimationLeft;
}
return tmp;
}
//here we take the spritesheet and section parts off into animations
void setupAnimations() {
s = sf::Sprite(texture, sf::IntRect(231 + 18, 62, -18, 32));
s.scale(scalingFactor, scalingFactor);
idleAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(14 + 17, 59, -18, 32));
s.scale(scalingFactor, scalingFactor);
idleAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(231 + 18, 62, -18, 32));
s.scale(scalingFactor, scalingFactor);
crouchingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(232, 62, -18, 32));
s.scale(scalingFactor, scalingFactor);
crouchingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(30, 59, -18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(30 + 18, 59, -18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(47 + 18, 59, -18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(30 + 18, 59, -18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationLeft.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(13, 59, 18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(30, 59, 18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(47, 59, 18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(30, 59, 18, 32));
s.scale(scalingFactor, scalingFactor);
walkingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(231, 62, 18, 32));
s.scale(scalingFactor, scalingFactor);
crouchingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(232 - 18, 62, 18, 32));
s.scale(scalingFactor, scalingFactor);
crouchingAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(231, 62, 18, 32));
s.scale(scalingFactor, scalingFactor);
idleAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(13, 59, 18, 32));
s.scale(scalingFactor, scalingFactor);
idleAnimationRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331, 343, 17, 18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331 - 17-17, 343, 17, 18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331-17, 343, 17, 18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331 - 17 -17 , 343+18, 17, -18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331, 343+18, 17, -18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331 - 17, 343 +18 , -17, -18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331 , 343, -17, 18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
s = sf::Sprite(texture, sf::IntRect(331 - 17 , 343, -17, 18));
s.scale(scalingFactor, scalingFactor);
hammerIdleRight.addFrame(s);
}
//given a sprite and a time elapsed, output the correct velocity in x and y
std::pair<float, float> updatePlayer(AnimatedSprite sprite, sf::Time frameTime) {
float fr = frameTime.asSeconds();
float x = sprite.getVelocityX() / fr;
float y = sprite.getVelocityY() / fr;
State current = static_cast<State>(sprite.getCurrState());
if (current == walkRight) {
if (x > walkSpeed) {
x = walkSpeed;
}
x = walkSpeed;
}
else if (current == walkLeft) {
x = -walkSpeed;
}
else if (current == crouchLeft) {
x += walkSpeed*stopFactor;
if (x > 0) {
x = 0;
}
}
else if (current == crouchRight) {
x -= walkSpeed*stopFactor;
if (x < 0) {
x = 0;
}
}
else if (current == idleLeft || current == idleRight) {
x = 0;
}
y += gravity;
if (sprite.getGlobalBounds().intersects(floorBox.getGlobalBounds())) {
sprite.setIsOnGround(true);
sprite.setIsJumping(false);
y = 0;
}
else {
sprite.setIsOnGround(false);
}
if (current == jumpLeft || current == jumpRight) {
if (!sprite.getIsJumping() && sprite.getIsOnGround() && abs(y) < 1) {
y = -jumpSpeed;
sprite.setIsJumping(true);
}
}
float spriteLeft = sprite.getGlobalBounds().left;
float spriteRight = sprite.getGlobalBounds().width + spriteLeft;
float lineLeft = halfLine.getGlobalBounds().left;
float lineRight = halfLine.getGlobalBounds().width + lineLeft;
if (sprite.getTeam() == 1) {
if (spriteLeft < 1) {
if (current == walkLeft || current == jumpLeft || current == crouchLeft || current == idleLeft) {
x = 0;
}
}
if (spriteRight > lineLeft - 1) {
if (current == walkRight || current == jumpRight || current == crouchRight || current == idleRight) {
x = 0;
}
}
}
else if (sprite.getTeam() == 2) {
if (spriteLeft - lineRight < 1) {
if (current == walkLeft || current == jumpLeft || current == crouchLeft || current == idleLeft) {
x = 0;
}
}
if (spriteRight > screenDimensions.x - 1) {
if (current == walkRight || current == jumpRight || current == crouchRight || current == idleRight) {
x = 0;
}
}
}
return std::pair<float, float>(x*fr, y*fr);
}
//this checks collisions between hammers and players. If a correct collision, victory is set to the correct team
void checkCollision(AnimatedSprite player, AnimatedSprite hammer, int id) {
if (hammer.getGlobalBounds().intersects(player.getGlobalBounds())) {
if (hammer.getTeam() == 0) {
teamList[id-1] =player.getTeam();
carryList[player.getTeam()-1] = 1;
}
else if (hammer.getTeam() != player.getTeam()) {
victory = hammer.getTeam();
}
}
}
//given a hammer and a time elapsed, output the correct velocity in x and y
std::pair<float, float> updateHammer(AnimatedSprite sprite, sf::Time frameTime, int id) {
float fr = frameTime.asSeconds();
float x = sprite.getVelocityX() / fr;
float y = sprite.getVelocityY() / fr;
float throwingVelocity = 500;
float angle = -30;
std::pair<float, float> outputPair;
y += gravity;
int team = teamList[id - 1];
if (carryList[team-1] == 1) {
if (chuckList[sprite.getTeam() - 1] > 0) {
carryList[team - 1] = 0;
y = throwingVelocity*sin(angle*PI / 180);
x = throwingVelocity*cos(angle*PI / 180);
if (sprite.getTeam() == 1) {
x += abs(p1V.first / fr);
y += p1V.second / fr;
}
else {
x += abs(p2V.first / fr);
y += p2V.second / fr;
}
//printf("%i", chuckList[sprite.getTeam() - 1]);
if (chuckList[sprite.getTeam() - 1] == 1)
x = -x;
}
else {
if (sprite.getTeam() == 1){
x = p1V.first / fr;
y = p1V.second / fr;
}
else {
x = p2V.first / fr;
y = p2V.second / fr;
}
//printf("x = %f, y = %f ", x, y);
}
}
float spriteLeft = sprite.getGlobalBounds().left;
float spriteRight = sprite.getGlobalBounds().width + spriteLeft;
if (spriteRight > screenDimensions.x - 1) {
if (x > 0) {
x = -x;
}
}
if (spriteLeft < 1) {
if (x < 0) {
x = -x;
}
}
if (sprite.getGlobalBounds().intersects(floorBox.getGlobalBounds())) {
if (y > 0) {
y = 0;
}
if (carryList[team - 1] == 0) {
if (y == 0) {
x = 0;
teamList[id - 1] = 0;
}
}
}
outputPair.first = x*fr;
outputPair.second = y*fr;
return outputPair;
}
int main(int argc, char *argv[])
{
//create server object and initialize it
char *serverPort = argv[1];
UDPSystem udpServer(NULL, serverPort);
udpServer.init();
messageContainer playerMessage;
//udp handshake
while(udpServer.getClients() != 2)
{
printf("waiting for connections...\n");
playerMessage = udpServer.recvPacket(TIMEOUT);
}
//Server tells both clients which player they are
char ack[] = "1";
char *ggg = ack;
udpServer.sendPacket(TO_PLAYER_ONE, ggg);
char ack2[] = "2";
udpServer.sendPacket(TO_PLAYER_TWO, ack2);
// setup window
floorBox.setPosition(0, screenDimensions.y - FLOOR_HEIGHT);
halfLine.setPosition(screenDimensions.x/2, screenDimensions.y - FLOOR_HEIGHT);
sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Animations!");
window.setFramerateLimit(60);
// load texture (spritesheet)
if (!texture.loadFromFile("spritesheet.png"))
{
std::cout << "Failed to load player spritesheet!" << std::endl;
return 1;
}
if (!font.loadFromFile("smb.ttf"))
{
// error...
}
sf::Text sideText;
// select the font
sideText.setFont(font); // font is a sf::Font // set the string to display
sideText.setString("Your Side");
// set the character size
sideText.setCharacterSize(24); // in pixels, not point
// set the text style
sideText.setStyle(sf::Text::Bold);
sideText.setPosition(screenDimensions.x / 6, screenDimensions.y * 7 / 8);
// set up the animations for all four directions (set spritesheet and push frames)
setupAnimations();
// set up AnimatedSprite
AnimatedSprite player1(sf::seconds(1/25.f),1);
player1.changePos(screenDimensions.x/2 -300, screenDimensions.y*2/3);
AnimatedSprite player2(sf::seconds(1 / 25.f), 2);
player2.changePos(screenDimensions.x / 2 + 300, screenDimensions.y *2/3);
AnimatedSprite hammer1(sf::seconds(1 / 25.f), 0);
hammer1.changePos(screenDimensions.x / 2 - 100, screenDimensions.y* 2/3);
AnimatedSprite hammer2(sf::seconds(1 / 25.f), 0);
hammer2.changePos(screenDimensions.x / 2 + 100, screenDimensions.y * 2 / 3);
sf::Clock frameClock;
sf::Text endText;
sf::Clock updateClock;
Input p1Input = none;
Input p2Input = none;
int jsonInt = 0;
//this is the loop
while (window.isOpen())
{
sf::Event event;
std::pair<float, float> newVelocity;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
}
// set tick rate to 60hz
while (updateClock.getElapsedTime().asMilliseconds() < 1000 / 60) {
}
updateClock.restart();
sf::Time frameTime = sf::milliseconds(1000/60);
if (victory == 0) {
//take the first packet off the socket's queue
playerMessage = udpServer.recvPacket(TIMEOUT);
if((strcmp(playerMessage.msg,CONNECT_VALUE) == 0) || playerMessage.msg == NULL)
{
//if msg is NULL or a connect() msg then skip this loop
std::cout << "packet NULL" << std::endl;
continue;
}
else
{
//collect input from clients
std::string jsonString(playerMessage.msg);
std::cout << "got : " << jsonString << std::endl;
auto jsonInput = json::parse(jsonString);
if(playerMessage.player == 1)
{
jsonInt = jsonInput["input"];
p1Input = static_cast<Input>(jsonInt);
}
else if(playerMessage.player == 2)
{
jsonInt = jsonInput["input"];
p2Input = static_cast<Input>(jsonInt);
}
else
{
std::cout << "player not recognized" << std::endl;
}
}
//update game
player1.setAnimation(getCurrentAnimation(player1, false));
State p1State = getCurrentState(player1, p1Input);
player1.setState(p1State);
player1.update(frameTime);
newVelocity = updatePlayer(player1, frameTime);
player1.setVelocity(newVelocity.first, newVelocity.second);
player1.movePosition();
p1V = newVelocity;
player2.setAnimation(getCurrentAnimation(player2, false));
State p2State = getCurrentState(player2, p2Input);
player2.setState(p1State);
player2.update(frameTime);
newVelocity = updatePlayer(player2, frameTime);
player2.setVelocity(newVelocity.first, newVelocity.second);
player2.movePosition();
p2V = newVelocity;
hammer1.setTeam(teamList[0]);
hammer1.setAnimation(getCurrentAnimation(hammer1, true));
hammer1.update(frameTime);
newVelocity = updateHammer(hammer1, frameTime,1);
hammer1.setVelocity(newVelocity.first, newVelocity.second);
hammer1.movePosition();
h1V = newVelocity;
checkCollision(player1, hammer1,1);
checkCollision(player2, hammer1,1);
hammer2.setTeam(teamList[1]);
hammer2.setAnimation(getCurrentAnimation(hammer2, true));
hammer2.update(frameTime);
newVelocity = updateHammer(hammer2, frameTime,2);
hammer2.setVelocity(newVelocity.first, newVelocity.second);
hammer2.movePosition();
h2V = newVelocity;
checkCollision(player1, hammer2,2);
checkCollision(player2, hammer2,2);
//send to both player the same info
json output =
{
{"h1X", hammer1.getPosX()},
{"h1Y", hammer1.getPosY()},
{"h2X", hammer2.getPosX()},
{"h2Y", hammer2.getPosY()},
{"p1X", player1.getPosX()},
{"p1Y", player1.getPosY()},
{"p2X", player2.getPosX()},
{"p2Y", player2.getPosY()},
{"p1VX", p1V.first},
{"p2VX", p2V.first},
{"h1VX", h1V.first},
{"h2VX", h2V.first},
{"p1VY", p1V.second},
{"p2VY", p2V.second},
{"h1VY", h1V.second},
{"h2VY", h2V.second},
{"victory", victory},
{"p1State", (int)p1State},
{"p2State", (int)p2State}
};
std::string stringOutput = output.dump();
char *charOutput = &stringOutput[0];
if(charOutput != NULL)
{
udpServer.sendPacket(TO_BOTH_PLAYER,charOutput);
std::cout << "packet sent" << std::endl;
}
else
{
std::cout << "not sent" << std::endl;
}
}
else
{
// select the font
//endText.setFont(font); // font is a sf::Font // set the string to display
if (victory == 1) {
endText.setString("YOU WIN!!");
}
else {
endText.setString("YOU LOSE :(");
}
// set the character size
endText.setCharacterSize(100); // in pixels, not point
// set the text style
endText.setStyle(sf::Text::Bold);
endText.setPosition(screenDimensions.x / 6, screenDimensions.y / 8);
}
// draw
window.clear();
// inside the main loop, between window.clear() and window.display()
window.draw(player1);
window.draw(player2);
window.draw(hammer1);
window.draw(hammer2);
window.draw(floorBox);
window.draw(halfLine);
window.draw(sideText);
if (victory != 0) {
window.draw(endText);
}
window.display();
}
return 0;
}