- Timestamp:
- Jan 19, 2014, 7:40:55 PM (11 years ago)
- Branches:
- master
- Children:
- e5697b1
- Parents:
- 6054f1e
- Location:
- common
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Game.cpp
r6054f1e rd05c484 1 1 #include "Game.h" 2 3 #include <iostream> 4 #include <cstring> 2 5 3 6 #include "Common.h" … … 11 14 this->redScore = 0; 12 15 this->worldMap = NULL; 13 } 14 15 Game::Game(string name, string filepath) { 16 this->msgProcessor = NULL; 17 } 18 19 Game::Game(string name, string filepath, MessageProcessor* msgProcessor) { 16 20 this->id = 0; 17 21 this->name = name; … … 19 23 this->redScore = 0; 20 24 this->worldMap = WorldMap::loadMapFromFile(filepath); 25 this->msgProcessor = msgProcessor; 21 26 } 22 27 … … 95 100 void Game::setBlueScore(unsigned int score) { 96 101 this->blueScore = score; 102 } 103 104 void Game::addObjectToMap(WorldMap::ObjectType objectType, int x, int y) { 105 NETWORK_MSG serverMsg; 106 107 this->getMap()->addObject(objectType, x, y); 108 109 serverMsg.type = MSG_TYPE_OBJECT; 110 this->worldMap->getObjects()->back().serialize(serverMsg.buffer); 111 112 this->msgProcessor->broadcastMessage(serverMsg, this->players); 97 113 } 98 114 … … 175 191 } 176 192 193 void Game::dealDamageToPlayer(Player* p, int damage) { 194 p->takeDamage(damage); 195 196 if (p->isDead) 197 { 198 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE; 199 if (p->hasBlueFlag) 200 flagType = WorldMap::OBJECT_BLUE_FLAG; 201 else if (p->hasRedFlag) 202 flagType = WorldMap::OBJECT_RED_FLAG; 203 204 if (flagType != WorldMap::OBJECT_NONE) 205 this->addObjectToMap(flagType, p->pos.x, p->pos.y); 206 } 207 208 // send a PLAYER message after dealing damage 209 NETWORK_MSG serverMsg; 210 serverMsg.type = MSG_TYPE_PLAYER; 211 p->serialize(serverMsg.buffer); 212 msgProcessor->broadcastMessage(serverMsg, this->players); 213 } 214 215 bool Game::handleGameEvents() { 216 NETWORK_MSG serverMsg; 217 map<unsigned int, Player*>::iterator it; 218 bool gameFinished = false; 219 220 for (it = this->getPlayers().begin(); it != this->getPlayers().end(); it++) 221 { 222 gameFinished = gameFinished || 223 this->handlePlayerEvents(it->second); 224 } 225 226 if (gameFinished) { 227 for (it = this->players.begin(); it != this->players.end(); it++) 228 { 229 it->second->currentGame = NULL; 230 } 231 } 232 233 return gameFinished; 234 } 235 236 bool Game::handlePlayerEvents(Player* p) { 237 NETWORK_MSG serverMsg; 238 FLOAT_POSITION oldPos; 239 bool gameFinished = false; 240 bool broadcastMove = false; 241 242 cout << "moving player" << endl; 243 244 // move player and perform associated tasks 245 oldPos = p->pos; 246 if (p->move(this->worldMap)) { 247 248 cout << "player moved" << endl; 249 if (this->processPlayerMovement(p, oldPos)) 250 broadcastMove = true; 251 cout << "player move processed" << endl; 252 253 WorldMap::ObjectType flagType; 254 POSITION pos; 255 bool flagTurnedIn = false; 256 bool flagReturned = false; 257 bool ownFlagAtBase = false; 258 259 switch(this->worldMap->getStructure(p->pos.x/25, p->pos.y/25)) 260 { 261 case WorldMap::STRUCTURE_BLUE_FLAG: 262 { 263 if (p->team == 0 && p->hasRedFlag) 264 { 265 // check that your flag is at your base 266 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); 267 268 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects(); 269 vector<WorldMap::Object>::iterator itObjects; 270 271 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) 272 { 273 if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) 274 { 275 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) 276 { 277 ownFlagAtBase = true; 278 break; 279 } 280 } 281 } 282 283 if (ownFlagAtBase) 284 { 285 p->hasRedFlag = false; 286 flagType = WorldMap::OBJECT_RED_FLAG; 287 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG); 288 flagTurnedIn = true; 289 this->blueScore++; 290 } 291 } 292 293 break; 294 } 295 case WorldMap::STRUCTURE_RED_FLAG: 296 { 297 if (p->team == 1 && p->hasBlueFlag) 298 { 299 // check that your flag is at your base 300 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG); 301 302 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects(); 303 vector<WorldMap::Object>::iterator itObjects; 304 305 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) 306 { 307 if (itObjects->type == WorldMap::OBJECT_RED_FLAG) 308 { 309 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) 310 { 311 ownFlagAtBase = true; 312 break; 313 } 314 } 315 } 316 317 if (ownFlagAtBase) 318 { 319 p->hasBlueFlag = false; 320 flagType = WorldMap::OBJECT_BLUE_FLAG; 321 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); 322 flagTurnedIn = true; 323 this->redScore++; 324 } 325 } 326 327 break; 328 } 329 } 330 331 if (flagTurnedIn) 332 { 333 unsigned int blueScore = this->blueScore; 334 unsigned int redScore = this->redScore; 335 336 pos.x = pos.x*25+12; 337 pos.y = pos.y*25+12; 338 this->addObjectToMap(flagType, pos.x, pos.y); 339 340 serverMsg.type = MSG_TYPE_SCORE; 341 memcpy(serverMsg.buffer, &blueScore, 4); 342 memcpy(serverMsg.buffer+4, &redScore, 4); 343 msgProcessor->broadcastMessage(serverMsg, this->players); 344 345 // check to see if the game should end 346 // move to its own method 347 if (this->blueScore == 3 || this->redScore == 3) { 348 gameFinished = true; 349 350 unsigned int winningTeam; 351 if (this->blueScore == 3) 352 winningTeam = 0; 353 else if (this->redScore == 3) 354 winningTeam = 1; 355 356 serverMsg.type = MSG_TYPE_FINISH_GAME; 357 358 // I should create an instance of the GameSummary object here and just serialize it into this message 359 memcpy(serverMsg.buffer, &winningTeam, 4); 360 memcpy(serverMsg.buffer+4, &blueScore, 4); 361 memcpy(serverMsg.buffer+8, &redScore, 4); 362 strcpy(serverMsg.buffer+12, this->getName().c_str()); 363 msgProcessor->broadcastMessage(serverMsg, this->players); 364 } 365 366 // this means a PLAYER message will be sent 367 broadcastMove = true; 368 } 369 370 // go through all objects and check if the player is close to one and if its their flag 371 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects(); 372 vector<WorldMap::Object>::iterator itObjects; 373 POSITION structPos; 374 375 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) 376 { 377 POSITION pos = itObjects->pos; 378 379 if (posDistance(p->pos, pos.toFloat()) < 10) 380 { 381 if (p->team == 0 && 382 itObjects->type == WorldMap::OBJECT_BLUE_FLAG) 383 { 384 structPos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); 385 flagReturned = true; 386 break; 387 } 388 else if (p->team == 1 && 389 itObjects->type == WorldMap::OBJECT_RED_FLAG) 390 { 391 structPos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG); 392 flagReturned = true; 393 break; 394 } 395 } 396 } 397 398 if (flagReturned) 399 { 400 itObjects->pos.x = structPos.x*25+12; 401 itObjects->pos.y = structPos.y*25+12; 402 403 serverMsg.type = MSG_TYPE_OBJECT; 404 itObjects->serialize(serverMsg.buffer); 405 msgProcessor->broadcastMessage(serverMsg, this->players); 406 } 407 408 if (broadcastMove) 409 { 410 serverMsg.type = MSG_TYPE_PLAYER; 411 p->serialize(serverMsg.buffer); 412 msgProcessor->broadcastMessage(serverMsg, this->players); 413 } 414 } 415 416 cout << "processing player attack" << endl; 417 418 // check if the player's attack animation is complete 419 if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) 420 { 421 p->isAttacking = false; 422 cout << "Attack animation is complete" << endl; 423 424 //send everyone an ATTACK message 425 cout << "about to broadcast attack" << endl; 426 427 if (p->attackType == Player::ATTACK_MELEE) 428 { 429 cout << "Melee attack" << endl; 430 431 Player* target = players[p->targetPlayer]; 432 this->dealDamageToPlayer(target, p->damage); 433 } 434 else if (p->attackType == Player::ATTACK_RANGED) 435 { 436 cout << "Ranged attack" << endl; 437 438 Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage); 439 this->assignProjectileId(&proj); 440 this->addProjectile(proj); 441 442 int x = p->pos.x; 443 int y = p->pos.y; 444 445 serverMsg.type = MSG_TYPE_PROJECTILE; 446 memcpy(serverMsg.buffer, &proj.id, 4); 447 memcpy(serverMsg.buffer+4, &x, 4); 448 memcpy(serverMsg.buffer+8, &y, 4); 449 memcpy(serverMsg.buffer+12, &p->targetPlayer, 4); 450 msgProcessor->broadcastMessage(serverMsg, players); 451 } 452 else 453 cout << "Invalid attack type: " << p->attackType << endl; 454 } 455 456 return gameFinished; 457 } 458 177 459 void Game::assignProjectileId(Projectile* p) { 178 460 p->id = unusedProjectileId; -
common/Game.h
r6054f1e rd05c484 14 14 #include "WorldMap.h" 15 15 #include "Projectile.h" 16 #include "MessageProcessor.h" 16 17 17 18 using namespace std; … … 27 28 unsigned int redScore; 28 29 unsigned int unusedProjectileId; 30 MessageProcessor* msgProcessor; 29 31 30 32 public: 31 33 Game(); 32 Game(string name, string filepath );34 Game(string name, string filepath, MessageProcessor* msgProcessor); 33 35 34 36 ~Game(); … … 44 46 void setRedScore(unsigned int score); 45 47 48 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y); 49 46 50 map<unsigned int, Player*>& getPlayers(); 47 51 bool addPlayer(Player* p); … … 55 59 bool processPlayerMovement(Player* p, FLOAT_POSITION oldPos); 56 60 int processFlagPickupRequest(Player* p); 61 void dealDamageToPlayer(Player* p, int damage); 62 63 bool handleGameEvents(); 64 bool handlePlayerEvents(Player* p); 57 65 58 66 void assignProjectileId(Projectile* p); -
common/MessageProcessor.cpp
r6054f1e rd05c484 95 95 } 96 96 97 void MessageProcessor::broadcastMessage(NETWORK_MSG &msg, map<unsigned int, Player*>& players) { 98 map<unsigned int, Player*>::iterator it; 99 for (it = players.begin(); it != players.end(); it++) { 100 this->sendMessage(&msg, &(it->second->addr)); 101 } 102 } 103 97 104 void MessageProcessor::resendUnackedMessages() { 98 105 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it; -
common/MessageProcessor.h
r6054f1e rd05c484 5 5 6 6 #include "MessageContainer.h" 7 #include "Player.h" 7 8 8 9 using namespace std; … … 29 30 int sendMessage(NETWORK_MSG *msg, struct sockaddr_in *dest); 30 31 int receiveMessage(NETWORK_MSG *msg, struct sockaddr_in *source); 32 void broadcastMessage(NETWORK_MSG &msg, map<unsigned int, Player*>& players); 31 33 void resendUnackedMessages(); 32 34 void cleanAckedMessages();
Note:
See TracChangeset
for help on using the changeset viewer.