Changeset ff2133a in network-game
- Timestamp:
- Jun 11, 2013, 1:24:09 AM (12 years ago)
- Branches:
- master
- Children:
- 5b1e31e
- Parents:
- 11d21ee
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Player.cpp
r11d21ee rff2133a 191 191 return moving; 192 192 } 193 194 void Player::updateTarget(map<unsigned int, Player>& mapPlayers) { 195 if (this->isChasing) { 196 this->target.x = mapPlayers[this->targetPlayer].pos.x; 197 this->target.y = mapPlayers[this->targetPlayer].pos.y; 198 199 if (posDistance(this->pos, this->target.toFloat()) <= this->range) { 200 this->target.x = this->pos.x; 201 this->target.y = this->pos.y; 202 203 this->isChasing = false; 204 this->isAttacking = true; 205 this->timeAttackStarted = getCurrentMillis(); 206 } 207 } 208 } -
common/Player.h
r11d21ee rff2133a 12 12 13 13 #include <string> 14 #include <map> 14 15 15 16 #include "Common.h" … … 46 47 void deserialize(char* buffer); 47 48 49 void updateTarget(map<unsigned int, Player>& mapPlayers); 48 50 bool move(WorldMap *map); 49 51 50 void takeFlag(int flag, WorldMap *map);51 void dropFlag(int flag, WorldMap *map);52 void takeFlag(int flag, WorldMap* map); 53 void dropFlag(int flag, WorldMap* map); 52 54 53 55 int id; -
common/Projectile.cpp
r11d21ee rff2133a 72 72 73 73 bool Projectile::move(map<unsigned int, Player>& mapPlayers) { 74 // if the current target logs off, this method will run into problems 75 76 //cout << "Inside projectile move" << endl; 77 74 78 unsigned long long curTime = getCurrentMillis(); 75 79 Player targetP = mapPlayers[target]; … … 85 89 float dist = sqrt(pow(targetP.pos.x-pos.x, 2) + pow(targetP.pos.y-pos.y, 2)); 86 90 91 //cout << "About to finish projectile move" << endl; 92 87 93 if (dist <= pixels) { 88 94 pos.x = targetP.pos.x; -
server/server.cpp
r11d21ee rff2133a 149 149 // set targets for all chasing players (or make them attack if they're close enough) 150 150 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 151 Player* p = &it->second; 152 151 //Player* p = &it->second; 152 it->second.updateTarget(mapPlayers); 153 154 /* 153 155 if (p->isChasing) { 154 156 p->target.x = mapPlayers[p->targetPlayer].pos.x; … … 164 166 } 165 167 } 168 */ 166 169 } 167 170 … … 171 174 bool broadcastMove = false; 172 175 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 176 cout << "Starting new for loop iteration" << endl; 173 177 oldPos = it->second.pos; 174 178 if (it->second.move(gameMap)) { … … 362 366 363 367 if (it->second.attackType == Player::ATTACK_MELEE) { 368 cout << "Melee attack" << endl; 369 364 370 Player* target = &mapPlayers[it->second.targetPlayer]; 365 371 … … 371 377 target->serialize(serverMsg.buffer); 372 378 }else if (it->second.attackType == Player::ATTACK_RANGED) { 379 cout << "Ranged attack" << endl; 380 373 381 Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage); 374 382 proj.id = unusedProjectileId; … … 389 397 390 398 // broadcast either a PLAYER or PROJECTILE message 399 cout << "Broadcasting player or projectile message" << endl; 400 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 401 { 402 if (sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 403 error("sendMessage"); 404 } 405 cout << "Done broadcasting" << endl; 406 } 407 } 408 409 cout << "Done with the for loop" << endl; 410 411 // move all projectiles 412 cout << "Moving projectiles" << endl; 413 map<unsigned int, Projectile>::iterator itProj; 414 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) { 415 if (itProj->second.move(mapPlayers)) { 416 // send a REMOVE_PROJECTILE message 417 cout << "send a REMOVE_PROJECTILE message" << endl; 418 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE; 419 memcpy(serverMsg.buffer, &itProj->second.id, 4); 420 mapProjectiles.erase(itProj->second.id); 421 422 map<unsigned int, Player>::iterator it2; 423 cout << "Broadcasting REMOVE_PROJECTILE" << endl; 391 424 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 392 425 { … … 394 427 error("sendMessage"); 395 428 } 396 } 397 }398 399 // move all projectiles400 map<unsigned int, Projectile>::iterator itProj; 401 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {402 if (itProj->second.move(mapPlayers)) {403 // send a REMOVE_PROJECTILE message404 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE; 405 memcpy(serverMsg.buffer, &itProj->second.id, 4);406 mapProjectiles.erase(itProj->second.id);407 408 map<unsigned int, Player>::iterator it2;429 430 cout << "send a PLAYER message after dealing damage" << endl; 431 // send a PLAYER message after dealing damage 432 Player* target = &mapPlayers[itProj->second.target]; 433 434 target->health -= itProj->second.damage; 435 if (target->health < 0) 436 target->health = 0; 437 438 serverMsg.type = MSG_TYPE_PLAYER; 439 target->serialize(serverMsg.buffer); 440 441 cout << "Sending a PLAYER message" << endl; 409 442 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 410 443 { … … 412 445 error("sendMessage"); 413 446 } 414 415 // send a PLAYER message after dealing damage 416 Player* target = &mapPlayers[itProj->second.target]; 417 418 target->health -= itProj->second.damage; 419 if (target->health < 0) 420 target->health = 0; 421 422 serverMsg.type = MSG_TYPE_PLAYER; 423 target->serialize(serverMsg.buffer); 424 425 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 426 { 427 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 428 error("sendMessage"); 429 } 430 } 431 } 432 } 447 } 448 } 449 } 450 cout << "Done moving projectiles" << endl; 433 451 434 452 n = receiveMessage(&clientMsg, sock, &from);
Note:
See TracChangeset
for help on using the changeset viewer.