Changeset 45734ff in network-game
- Timestamp:
- Dec 22, 2013, 10:14:43 PM (11 years ago)
- Branches:
- master
- Children:
- 58ca135
- Parents:
- 1d96513
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Game.cpp
r1d96513 r45734ff 37 37 } 38 38 39 bool Game::addPlayer(Player* p) { 40 if (players.find(p->id) == players.end()) { 41 players[p->id] = p; 42 return true; 43 } 44 else 45 return false; 46 } 47 48 bool Game::removePlayer(unsigned int id) { 49 if (players.erase(id) == 1) 50 return true; 51 else 52 return false; 53 } 54 39 55 map<unsigned int, Projectile>& Game::getProjectiles() { 40 56 return this->projectiles; 57 } 58 59 bool Game::addProjectile(Projectile p) { 60 if (projectiles.find(p.id) == projectiles.end()) { 61 projectiles[p.id] = p; 62 return true; 63 } 64 else 65 return false; 66 } 67 68 bool Game::removeProjectile(unsigned int id) { 69 if (projectiles.erase(id) == 1) 70 return true; 71 else 72 return false; 41 73 } 42 74 … … 63 95 void Game::setBlueScore(int score) { 64 96 this->blueScore = score; 65 }66 67 bool Game::addPlayer(Player* p) {68 if (players.find(p->id) == players.end()) {69 players[p->id] = p;70 return true;71 }72 else73 return false;74 }75 76 bool Game::removePlayer(unsigned int id) {77 if (players.erase(id) == 1)78 return true;79 else80 return false;81 97 } 82 98 … … 159 175 } 160 176 161 bool Game::addProjectile(Projectile p) { 162 if (projectiles.find(p.id) == projectiles.end()) { 163 projectiles[p.id] = p; 164 return true; 165 } 166 else 167 return false; 177 void Game::assignProjectileId(Projectile* p) { 178 p->id = unusedProjectileId; 179 updateUnusedProjectileId(); 168 180 } 169 181 170 bool Game::removeProjectile(unsigned int id) { 171 if (projectiles.erase(id) == 1) 172 return true; 173 else 174 return false; 182 void Game::updateUnusedProjectileId() { 183 while (projectiles.find(unusedProjectileId) != projectiles.end()) 184 unusedProjectileId++; 175 185 } -
common/Game.h
r1d96513 r45734ff 26 26 int blueScore; 27 27 int redScore; 28 unsigned int unusedProjectileId; 28 29 29 30 public: … … 35 36 string getName(); 36 37 int getNumPlayers(); 37 map<unsigned int, Player*>& getPlayers();38 map<unsigned int, Projectile>& getProjectiles();39 38 int getBlueScore(); 40 39 int getRedScore(); … … 45 44 void setRedScore(int score); 46 45 46 map<unsigned int, Player*>& getPlayers(); 47 47 bool addPlayer(Player* p); 48 48 bool removePlayer(unsigned int id); 49 50 map<unsigned int, Projectile>& getProjectiles(); 51 bool addProjectile(Projectile p); 52 bool removeProjectile(unsigned int id); 53 49 54 bool startPlayerMovement(unsigned int id, int x, int y); 50 55 bool processPlayerMovement(Player* p, FLOAT_POSITION oldPos); 51 56 int processFlagPickupRequest(Player* p); 52 57 53 bool addProjectile(Projectilep);54 bool removeProjectile(unsigned int id);58 void assignProjectileId(Projectile* p); 59 void updateUnusedProjectileId(); 55 60 }; 56 61 -
server/server.cpp
r1d96513 r45734ff 48 48 49 49 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers); 50 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);51 50 Player *findPlayerByName(map<unsigned int, Player*> &m, string name); 52 51 Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr); … … 476 475 477 476 Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage); 478 p roj.id = unusedProjectileId;479 updateUnusedProjectileId(unusedProjectileId, mapProjectiles);480 mapProjectiles[proj.id] = proj;481 482 int x = it->second->pos.x;483 int y = it->second->pos.y;477 p->currentGame->assignProjectileId(&proj); 478 479 p->currentGame->addProjectile(proj); 480 481 int x = p->pos.x; 482 int y = p->pos.y; 484 483 485 484 serverMsg.type = MSG_TYPE_PROJECTILE; … … 487 486 memcpy(serverMsg.buffer+4, &x, 4); 488 487 memcpy(serverMsg.buffer+8, &y, 4); 489 memcpy(serverMsg.buffer+12, & it->second->targetPlayer, 4);488 memcpy(serverMsg.buffer+12, &p->targetPlayer, 4); 490 489 } 491 490 else … … 507 506 // move all projectiles 508 507 // see if this can be moved inside the game class 508 // this method can be moved when I add a MessageProcessor to the Game class 509 509 map<unsigned int, Projectile>::iterator itProj; 510 510 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) … … 1198 1198 } 1199 1199 1200 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)1201 {1202 while (mapProjectiles.find(id) != mapProjectiles.end())1203 id++;1204 }1205 1206 1200 Player *findPlayerByName(map<unsigned int, Player*> &m, string name) 1207 1201 {
Note:
See TracChangeset
for help on using the changeset viewer.