Changeset 402cf86 in network-game


Ignore:
Timestamp:
Dec 20, 2013, 1:41:49 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
6c9bcdd
Parents:
fef7c69
Message:

The server checks player movement in individual games and stops players who hit obstacles

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • common/Game.cpp

    rfef7c69 r402cf86  
    8888}
    8989
     90// returns true if the movement should be canceled
     91bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
     92
     93   // check if the move needs to be canceled
     94   switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
     95   {
     96      case WorldMap::TERRAIN_NONE:
     97      case WorldMap::TERRAIN_OCEAN:
     98      case WorldMap::TERRAIN_ROCK:
     99      {
     100         p->pos = oldPos;
     101         p->target.x = p->pos.x;
     102         p->target.y = p->pos.y;
     103         p->isChasing = false;
     104         return true;
     105         break;
     106      }
     107      default:
     108         // if there are no obstacles, don't cancel movement
     109         return false;
     110         break;
     111      }
     112}
     113
    90114void Game::setRedScore(int score) {
    91115   this->redScore = score;
  • common/Game.h

    rfef7c69 r402cf86  
    4242   bool removePlayer(unsigned int id);
    4343   bool startPlayerMovement(unsigned int id, int x, int y);
     44   bool processPlayerMovement(Player* p, FLOAT_POSITION oldPos);
     45
    4446   void setBlueScore(int score);
    4547   void setRedScore(int score);
  • data/map.txt

    rfef7c69 r402cf86  
    10102,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
    11112,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
    12 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
     122,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,2
    13132,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
    14142,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
  • server/server.cpp

    rfef7c69 r402cf86  
    138138   while (!done)
    139139   {
    140 
    141140      usleep(5000);
    142141
     
    217216         }
    218217
    219          // move all players
    220          // maybe put this in a separate method
     218         // process players currently in a game
    221219         FLOAT_POSITION oldPos;
    222          bool broadcastMove = false;
    223220         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    224221         {
     222            bool broadcastMove = false;
     223
     224            // move player and perform associated tasks
    225225            oldPos = it->second->pos;
    226             if (it->second->move(gameMap)) {
    227 
    228                // check if the move needs to be canceled
    229                switch(gameMap->getElement(it->second->pos.x/25, it->second->pos.y/25))
    230                {
    231                   case WorldMap::TERRAIN_NONE:
    232                   case WorldMap::TERRAIN_OCEAN:
    233                   case WorldMap::TERRAIN_ROCK:
    234                   {
    235                      it->second->pos = oldPos;
    236                      it->second->target.x = it->second->pos.x;
    237                      it->second->target.y = it->second->pos.y;
    238                      it->second->isChasing = false;
    239                      broadcastMove = true;
    240                      break;
    241                   }
    242                   default:
    243                      // if there are no obstacles, do nothing
    244                      break;
    245                }
    246 
     226            if (it->second->move(it->second->currentGame->getMap())) {
     227
     228               if (it->second->currentGame->processPlayerMovement(it->second, oldPos))
     229                   broadcastMove = true;
     230
     231               /*
    247232               WorldMap::ObjectType flagType;
    248233               POSITION pos;
     
    397382                  }
    398383               }
     384               */
    399385
    400386               if (broadcastMove)
     
    403389                  it->second->serialize(serverMsg.buffer);
    404390
     391                  // only broadcast message to other players in the same game
    405392                  cout << "about to broadcast move" << endl;
     393
     394                  map<unsigned int, Player*> playersInGame = it->second->currentGame->getPlayers();
    406395                  map<unsigned int, Player*>::iterator it2;
    407                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
     396                  for (it2 = playersInGame.begin(); it2 != playersInGamePlayers.end(); it2++)
    408397                  {
    409398                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
Note: See TracChangeset for help on using the changeset viewer.