Changeset 5b92307 in network-game


Ignore:
Timestamp:
Jan 20, 2014, 6:47:58 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
204edcf
Parents:
949cf70
Message:

id and targetPlayer are now both private members of the Player class and have getters and setters to access them

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • client/Client/GameRender.cpp

    r949cf70 r5b92307  
    7474      pos = mapToScreen(p->pos.toInt());
    7575
    76       if (p->id == curPlayerId)
     76      if (p->getId() == curPlayerId)
    7777         al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
    7878     
  • client/Client/main.cpp

    r949cf70 r5b92307  
    432432                  target = it->second;
    433433                  cout << "set target" << endl;
    434                   if (target->id != curPlayerId && target->team != curPlayer->team)
     434                  if (target->getId() != curPlayerId && target->team != curPlayer->team)
    435435                  {
    436436                     cout << "Found valid target" << endl;
    437437
     438                     unsigned int targetId = target->getId();
     439
    438440                     msgTo.type = MSG_TYPE_ATTACK;
    439441                     memcpy(msgTo.buffer, &curPlayerId, 4);
    440                      memcpy(msgTo.buffer+4, &target->id, 4);
     442                     memcpy(msgTo.buffer+4, &targetId, 4);
    441443
    442444                     msgProcessor.sendMessage(&msgTo, &server);
     
    678680                  p->deserialize(msg.buffer);
    679681
    680                   if (mapPlayers.find(p->id) != mapPlayers.end())
    681                      delete mapPlayers[p->id];
    682                   mapPlayers[p->id] = p;
    683                   curPlayerId = p->id;
     682                  if (mapPlayers.find(p->getId()) != mapPlayers.end())
     683                     delete mapPlayers[p->getId()];
     684                  mapPlayers[p->getId()] = p;
     685                  curPlayerId = p->getId();
    684686
    685687                  cout << "Got a valid login response with the player" << endl;
     
    732734                  p.isDead = false;
    733735
    734                if (mapPlayers.find(p.id) != mapPlayers.end())
    735                   *(mapPlayers[p.id]) = p;
     736               if (mapPlayers.find(p.getId()) != mapPlayers.end())
     737                  *(mapPlayers[p.getId()]) = p;
    736738               else
    737                   mapPlayers[p.id] = new Player(p);
     739                  mapPlayers[p.getId()] = new Player(p);
    738740
    739741               break;
     
    771773
    772774               Player* source = mapPlayers[id];
    773                source->targetPlayer = targetID;
     775               source->setTargetPlayer(targetID);
    774776               source->isChasing = true;
    775777
     
    953955                  p.isDead = false;
    954956
    955                if (mapPlayers.find(p.id) != mapPlayers.end())
    956                   *(mapPlayers[p.id]) = p;
     957               if (mapPlayers.find(p.getId()) != mapPlayers.end())
     958                  *(mapPlayers[p.getId()]) = p;
    957959               else
    958                   mapPlayers[p.id] = new Player(p);
     960                  mapPlayers[p.getId()] = new Player(p);
    959961
    960962               break;
     
    966968               Player p("", "");
    967969               p.deserialize(msg.buffer);
     970               cout << "Deserialized player" << endl;
    968971               p.timeLastUpdated = getCurrentMillis();
    969972               p.isChasing = false;
     
    973976                  p.isDead = false;
    974977
    975                if (mapPlayers.find(p.id) != mapPlayers.end())
    976                   *(mapPlayers[p.id]) = p;
     978               if (mapPlayers.find(p.getId()) != mapPlayers.end())
     979                  *(mapPlayers[p.getId()]) = p;
    977980               else
    978                   mapPlayers[p.id] = new Player(p);
    979 
    980                game->addPlayer(mapPlayers[p.id]);
     981                  mapPlayers[p.getId()] = new Player(p);
     982
     983               game->addPlayer(mapPlayers[p.getId()]);
    981984
    982985               break;
     
    10511054               // need to check the target exists in the current game
    10521055               Player* source = game->getPlayers()[id];
    1053                source->targetPlayer = targetId;
     1056               source->setTargetPlayer(targetId);
    10541057               source->isChasing = true;
    10551058
  • common/Game.cpp

    r949cf70 r5b92307  
    4343
    4444bool Game::addPlayer(Player* p) {
    45    if (players.find(p->id) == players.end()) {
    46       players[p->id] = p;
     45   if (players.find(p->getId()) == players.end()) {
     46      players[p->getId()] = p;
    4747      return true;
    4848   }
     
    429429         cout << "Melee attack" << endl;
    430430
    431          Player* target = players[p->targetPlayer];
     431         Player* target = players[p->getTargetPlayer()];
    432432         this->dealDamageToPlayer(target, p->damage);
    433433      }
     
    436436         cout << "Ranged attack" << endl;
    437437
    438          Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
     438         Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
    439439         this->assignProjectileId(&proj);
    440440         this->addProjectile(proj);
     
    442442         int x = p->pos.x;
    443443         int y = p->pos.y;
     444         unsigned int targetId = p->getTargetPlayer();
    444445
    445446         serverMsg.type = MSG_TYPE_PROJECTILE;
     
    447448         memcpy(serverMsg.buffer+4, &x, 4);
    448449         memcpy(serverMsg.buffer+8, &y, 4);
    449          memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
     450         memcpy(serverMsg.buffer+12, &targetId, 4);
    450451         msgProcessor->broadcastMessage(serverMsg, players);
    451452      }
  • common/Player.cpp

    r949cf70 r5b92307  
    1515   this->pos.x = this->target.x = 0;
    1616   this->pos.y = this->target.y = 0;
     17   this->targetPlayer = 0;
    1718   this->timeLastUpdated = 0;
    1819   this->timeAttackStarted = 0;
     
    4647   this->target.x = p.target.x;
    4748   this->target.y = p.target.y;
     49   this->targetPlayer = p.targetPlayer;
    4850   this->timeLastUpdated = p.timeLastUpdated;
    4951   this->timeAttackStarted = p.timeAttackStarted;
     
    7577   this->pos.x = this->target.x = 200;
    7678   this->pos.y = this->target.y = 200;
     79   this->targetPlayer = 0;
    7780   this->timeLastUpdated = 0;
    7881   this->timeAttackStarted = 0;
     
    100103}
    101104
     105unsigned int Player::getId()
     106{
     107   return this->id;
     108}
     109
     110unsigned int Player::getTargetPlayer()
     111{
     112   return this->targetPlayer;
     113}
     114
    102115void Player::setId(unsigned int id)
    103116{
    104117   this->id = id;
     118}
     119
     120void Player::setTargetPlayer(unsigned int id)
     121{
     122   this->targetPlayer = id;
    105123}
    106124
     
    217235}
    218236
    219 bool Player::updateTarget(const Player* targetPlayer) {
    220    if (this->isChasing) {
    221       this->target.x = targetPlayer->pos.x;
    222       this->target.y = targetPlayer->pos.y;
     237bool Player::updateTarget(map<unsigned int, Player*>& players) {
     238   Player* p = NULL;
     239   if (this->targetPlayer > 0)
     240      p =players[this->targetPlayer];
     241
     242   if (p != NULL && this->isChasing) {
     243      this->target.x = p->pos.x;
     244      this->target.y = p->pos.y;
    223245
    224246      if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
  • common/Player.h

    r949cf70 r5b92307  
    2121
    2222class Player {
     23private:
     24   unsigned int id;
     25   unsigned int targetPlayer;
     26
    2327public:
    2428
     
    4145   ~Player();
    4246
     47   unsigned int getId();
     48   unsigned int getTargetPlayer();
     49
    4350   void setId(unsigned int id);
     51   void setTargetPlayer(unsigned int id);
    4452   void setAddr(sockaddr_in addr);
    4553   void setClass(PlayerClass c);
     
    4856   void deserialize(char* buffer);
    4957
    50    bool updateTarget(const Player* targetPlayer);
     58   bool updateTarget(map<unsigned int, Player*>& players);
    5159   bool move(WorldMap *map);
    5260   void takeDamage(int damage);
     
    5563   void dropFlag(unsigned int flag, WorldMap* map);
    5664
    57    unsigned int id;
    5865   string name;
    5966   string password;
     
    6673   bool isChasing;
    6774   bool isAttacking;
    68    unsigned int targetPlayer;
    6975   bool isDead;
    7076
Note: See TracChangeset for help on using the changeset viewer.