-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.cpp
executable file
·343 lines (331 loc) · 13.6 KB
/
Client.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
#include "Client.h"
#include "Mage.h"
#include "Ranger.h"
#include "Knight.h"
Client::Client(sf::UdpSocket & socket, userInfo server) : server(server) {
otherPlayers = std::vector<PlayerData>(0);
if (server.ip != sf::IpAddress::None) {
// setup port stuff
if (socket.bind(sf::Socket::AnyPort) != sf::Socket::Done) {
// failed to bind
}
port = socket.getLocalPort();
}
}
Client::~Client() {
// stop server
stopReceivingPackets();
for (int i = 0; i < otherPlayers.size(); i++) {
delete otherPlayers[i].userCharacter;
otherPlayers[i].userCharacter = NULL;
}
}
void Client::operator=(const Client & other) {
port = other.port;
server = other.server;
otherPlayers = std::vector<PlayerData>(0);
if (other.running) {
other.~Client();
}
}
void Client::sendPacket(sf::UdpSocket & socket, sf::Packet & packet) {
if (socket.send(packet, server.ip, server.port) != sf::Socket::Done) {
// failed to send packet
}
}
void Client::receivePackets(sf::UdpSocket & socket, int & kills, sf::Texture & rangerTexture, sf::Texture & mageTexture, sf::Texture & knightTexture, sf::Texture & arrowTexture, sf::Texture & fireballA, sf::Texture & fireballB, sf::Texture & swordTexture, sf::Texture & bowTexture, sf::Texture & staffTexture, sf::Texture & healthBarForegroundTexture, sf::Texture & healthBarBackgroundTexture) {
socket.setBlocking(false);
// send "join game" packet to the server
sf::Packet joinServerPacket;
joinServerPacket.clear();
joinServerPacket << "join game";
sendPacket(socket, joinServerPacket);
// setup variables and run receive
sf::Packet packet;
sf::IpAddress senderIp;
unsigned short senderPort;
bool wasServerClosed = false;
running = true;
Knight knight(healthBarForegroundTexture, healthBarBackgroundTexture, knightTexture, swordTexture);
Mage mage(healthBarForegroundTexture, healthBarBackgroundTexture, mageTexture, staffTexture, fireballA, fireballB);
Ranger ranger(healthBarForegroundTexture, healthBarBackgroundTexture, rangerTexture, bowTexture, arrowTexture, arrowTexture);
while (running) {
packet.clear();
int rec = socket.receive(packet, senderIp, senderPort);
if (rec == sf::Socket::Done) {
// manage packet data
std::string data;
packet >> data;
if (strcmp(data.c_str(), "success") == 0) {
packet >> clientIDfromServer >> timeLeftInGame >> gameNotInProgress;
serverConnectionStatus = 2;
printf("[Client] joined server as %s\n", clientIDfromServer.c_str());
continue; // connected to server successfully
}
else if (serverConnectionStatus <= 1) {
serverConnectionStatus = 1;
continue;
}
else if (strcmp(data.c_str(), "kill") == 0) {
std::string killerId;
std::string killedId;
packet >> killerId >> killedId >> timeLeftInGame >> gameNotInProgress;
if (killerId == clientIDfromServer) {
kills++;
}
for (int i = 0; i < otherPlayers.size(); i++) {
if (killedId == otherPlayers[i].userID) {
otherPlayers[i].userCharacter->justAdded = true;
break;
}
}
continue;
}
else if (strcmp(data.c_str(), "left game") == 0) {
// remove the player (that left) from the list of players
std::string leftPlayerId;
packet >> leftPlayerId;
printf("[Client] %s left game\n", leftPlayerId.c_str());
for (int i = 0; i < otherPlayers.size(); i++) {
if (leftPlayerId == otherPlayers[i].userID) {
otherPlayers[i].isViable = false;
break;
}
}
continue;
}
else if (strcmp(data.c_str(), "closed") == 0) {
printf("[Client] server closed so returning to main menu in 3 seconds...\n");
sf::sleep(sf::seconds(3));
wasServerClosed = true;
break;
}
std::string fighterName;
packet >> fighterName;
if (data == clientIDfromServer) {
if (fighterName == "Knight") {
knight.extractPacketToData(packet);
}
else if (fighterName == "Mage") {
mage.extractPacketToData(packet);
}
else if (fighterName == "Ranger") {
ranger.extractPacketToData(packet);
}
packet >> timeLeftInGame >> gameNotInProgress;
continue;
}
int i;
for (i = 0; i < otherPlayers.size(); i++) {
if (data == otherPlayers[i].userID) {
if (fighterName == otherPlayers[i].fighterClass) {
float heath = otherPlayers[i].userCharacter->getHealth();
if (fighterName == "Knight" && otherPlayers[i].userCharacter != NULL) {
((Knight *)(otherPlayers[i].userCharacter))->extractPacketToData(packet);
}
else if (fighterName == "Mage" && otherPlayers[i].userCharacter != NULL) {
((Mage *)(otherPlayers[i].userCharacter))->extractPacketToData(packet);
}
else if (fighterName == "Ranger" && otherPlayers[i].userCharacter != NULL) {
((Ranger *)(otherPlayers[i].userCharacter))->extractPacketToData(packet);
}
if (heath > otherPlayers[i].userCharacter->getHealth() && otherPlayers[i].didTakeDamage) {
otherPlayers[i].confirmDamageTaken = true;
}
}
else if (fighterName != otherPlayers[i].fighterClass && data != "") {
otherPlayers[i].isViable = false;
PlayerData userData;
userData.userID = data;
userData.fighterClass = fighterName;
if (fighterName == "Knight") {
userData.userCharacter = (Character*)(new Knight(healthBarForegroundTexture, healthBarBackgroundTexture, knightTexture, swordTexture, 3.2f, 125, 22, 37));
}
else if (fighterName == "Mage") {
userData.userCharacter = (Character*)(new Mage(healthBarForegroundTexture, healthBarBackgroundTexture, mageTexture, staffTexture, fireballA, fireballB, 7.0f, 0.7f, 0.3f, 0.0f, 80, 25, 40, 2.2f));
}
else if (fighterName == "Ranger") {
userData.userCharacter = (Character*)(new Ranger(healthBarForegroundTexture, healthBarBackgroundTexture, rangerTexture, bowTexture, arrowTexture, arrowTexture, 10.0f, 0.7f, 0.3f, 1.3f, 110, 16, 22, 2.6f));
}
userData.confirmDamageTaken = true;
userData.didTakeDamage = false;
userData.userCharacter->justAdded = true;
userData.isViable = true;
userData.userCharacter->extractPacketToData(packet);
packet >> timeLeftInGame >> gameNotInProgress;
otherPlayers.push_back(userData);
}
packet >> timeLeftInGame >> gameNotInProgress;
break;
}
}
if (i == otherPlayers.size() && data != "") {
PlayerData userData;
userData.userID = data;
userData.fighterClass = fighterName;
if (fighterName == "Knight") {
userData.userCharacter = (Character*)(new Knight(healthBarForegroundTexture, healthBarBackgroundTexture, knightTexture, swordTexture, 3.2f, 125, 22, 37));
}
else if (fighterName == "Mage") {
userData.userCharacter = (Character*)(new Mage(healthBarForegroundTexture, healthBarBackgroundTexture, mageTexture, staffTexture, fireballA, fireballB, 7.0f, 0.7f, 0.3f, 0.0f, 80, 25, 40, 2.2f));
}
else if (fighterName == "Ranger") {
userData.userCharacter = (Character*)(new Ranger(healthBarForegroundTexture, healthBarBackgroundTexture, rangerTexture, bowTexture, arrowTexture, arrowTexture, 10.0f, 0.7f, 0.3f, 1.3f, 110, 16, 22, 2.6f));
}
userData.confirmDamageTaken = true;
userData.didTakeDamage = false;
userData.userCharacter->justAdded = true;
userData.isViable = true;
userData.userCharacter->extractPacketToData(packet);
packet >> timeLeftInGame >> gameNotInProgress;
otherPlayers.push_back(userData);
printf("[Client] %s joined game\n", userData.userID.c_str());
}
}
}
// send "left game" packet to the server if needed
if (!wasServerClosed) {
sf::Packet leftServerPacket;
leftServerPacket.clear();
leftServerPacket << "left game";
sendPacket(socket, leftServerPacket);
}
// unbind port
socket.unbind();
}
void Client::stopReceivingPackets() {
running = false;
}
void Client::checkCollisions(Character * player, classTypes currentClass, sf::UdpSocket & socket, sf::Sound & takeDamageSound, sf::Sound & doDamageSound) {
for (int i = 0; i < otherPlayers.size(); i++) {
if (otherPlayers[i].isViable) {
if (otherPlayers[i].fighterClass == "Knight") {
if (otherPlayers[i].userCharacter != NULL && player != NULL && ((Knight *)(otherPlayers[i].userCharacter))->collisionSP(player->getCollisionCircle())) {
if (!gameNotInProgress && !player->getIsDead() && player->takeDamage(((Knight *)(otherPlayers[i].userCharacter))->getDamage()) == 0) {
sf::Packet killPacket;
killPacket.clear();
killPacket << "kill" << otherPlayers[i].userID;
sendPacket(socket, killPacket);
}
takeDamageSound.play();
damageTakenVisualEffectTimer.restart();
}
}
else if (otherPlayers[i].fighterClass == "Mage") {
if (otherPlayers[i].userCharacter != NULL && player != NULL && ((Mage *)(otherPlayers[i].userCharacter))->collisionPP(player->getCollisionCircle())) {
if (!gameNotInProgress && !player->getIsDead() && player->takeDamage(((Mage *)(otherPlayers[i].userCharacter))->getDamage()) == 0) {
sf::Packet killPacket;
killPacket.clear();
killPacket << "kill" << otherPlayers[i].userID << clientIDfromServer;
sendPacket(socket, killPacket);
}
takeDamageSound.play();
damageTakenVisualEffectTimer.restart();
}
}
else if (otherPlayers[i].fighterClass == "Ranger") {
if (otherPlayers[i].userCharacter != NULL && player != NULL && ((Ranger *)(otherPlayers[i].userCharacter))->collisionPP(player->getCollisionCircle())) {
if (!gameNotInProgress && !player->getIsDead() && player->takeDamage(((Ranger *)(otherPlayers[i].userCharacter))->getDamage()) == 0) {
sf::Packet killPacket;
killPacket.clear();
killPacket << "kill" << otherPlayers[i].userID;
sendPacket(socket, killPacket);
}
takeDamageSound.play();
damageTakenVisualEffectTimer.restart();
}
}
// get rid of projectiles if colliding
if (currentClass == knight) {
if (((Knight *)player)->collisionSP(otherPlayers[i].userCharacter->getCollisionCircle())) {
otherPlayers[i].didTakeDamage = true;
if (gameNotInProgress) {
doDamageSound.play();
}
}
}
else if (currentClass == mage) {
if (((Mage *)player)->collisionPP(otherPlayers[i].userCharacter->getCollisionCircle())) {
otherPlayers[i].didTakeDamage = true;
if (gameNotInProgress) {
doDamageSound.play();
}
}
}
else if (currentClass == ranger) {
if (((Ranger *)player)->collisionPP(otherPlayers[i].userCharacter->getCollisionCircle())) {
otherPlayers[i].didTakeDamage = true;
if (gameNotInProgress) {
doDamageSound.play();
}
}
}
// play damage sound if needed
if (!gameNotInProgress && otherPlayers[i].confirmDamageTaken && otherPlayers[i].didTakeDamage) {
doDamageSound.play();
otherPlayers[i].didTakeDamage = false;
otherPlayers[i].confirmDamageTaken = false;
}
}
}
}
void Client::draw(sf::RenderWindow & window, sf::Vector2f playerPosition, Map *map) {
if (damageTakenVisualEffectTimer.getElapsedTime().asSeconds() < damageTakenVisualTime) {
sf::RectangleShape frontdrop((sf::Vector2f) windowSize);
frontdrop.setFillColor(sf::Color(255, 0, 0, 25));
frontdrop.setPosition(playerPosition - sf::Vector2f(windowSize.x / 2.0f, windowSize.y / 2.0f));
window.draw(frontdrop);
}
for (int i = 0; i < otherPlayers.size(); i++) {
if (!otherPlayers[i].isViable || otherPlayers[i].userID.find_first_of("Player") == std::string::npos || isDuplicate(otherPlayers[i].userID, i + 1)) {
if (otherPlayers[i].fighterClass == "Knight" && otherPlayers[i].userCharacter != NULL) {
delete (Knight *)(otherPlayers[i].userCharacter);
otherPlayers[i].userCharacter = NULL;
}
else if (otherPlayers[i].fighterClass == "Mage" && otherPlayers[i].userCharacter != NULL) {
delete (Mage *)(otherPlayers[i].userCharacter);
otherPlayers[i].userCharacter = NULL;
}
else if (otherPlayers[i].fighterClass == "Ranger" && otherPlayers[i].userCharacter != NULL) {
delete (Ranger *)(otherPlayers[i].userCharacter);
otherPlayers[i].userCharacter = NULL;
}
otherPlayers.erase(otherPlayers.begin() + i);
i--;
}
else {
if (otherPlayers[i].fighterClass == "Knight" && otherPlayers[i].userCharacter != NULL) {
((Knight *)(otherPlayers[i].userCharacter))->swingSword();
((Knight *)(otherPlayers[i].userCharacter))->draw(window);
}
else if (otherPlayers[i].fighterClass == "Mage" && otherPlayers[i].userCharacter != NULL) {
((Mage *)(otherPlayers[i].userCharacter))->shoot(window);
map->collisionMProj(*(ProjectileShooter*)otherPlayers[i].userCharacter);
((Mage *)(otherPlayers[i].userCharacter))->setWeapon(window);
((Mage *)(otherPlayers[i].userCharacter))->drawProjectiles(window);
((Mage *)(otherPlayers[i].userCharacter))->draw(window);
}
else if (otherPlayers[i].fighterClass == "Ranger" && otherPlayers[i].userCharacter != NULL) {
((Ranger *)(otherPlayers[i].userCharacter))->shoot(window);
map->collisionMProj(*(ProjectileShooter*)otherPlayers[i].userCharacter);
((Ranger *)(otherPlayers[i].userCharacter))->setWeapon(window);
((Ranger *)(otherPlayers[i].userCharacter))->drawProjectiles(window);
((Ranger *)(otherPlayers[i].userCharacter))->draw(window);
}
}
}
}
std::string Client::getClientId() {
return clientIDfromServer;
}
bool Client::isGameInProgress() {
return !gameNotInProgress;
}
bool Client::isDuplicate(std::string name, int start) {
for (; start < otherPlayers.size(); start++) {
if (otherPlayers[start].userID == name) {
return true;
}
}
return false;
}