Changeset 23559e7 in network-game


Ignore:
Timestamp:
May 24, 2013, 1:23:49 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
b81cea1
Parents:
45b2750
git-author:
dportnoy <dmp1488@…> (05/24/13 01:14:53)
git-committer:
dportnoy <dmp1488@…> (05/24/13 01:23:49)
Message:

Move player interaction with objects on the map from Player::move to the server

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r45b2750 r23559e7  
    8989
    9090bool Player::move(WorldMap *map) {
    91    int speed = 100; // pixels per second
     91   int speed = 100; // pixels per second. should probably be in the constructor
    9292   unsigned long long curTime = getCurrentMillis();
    93    bool moveCanceled = false;
    9493
    9594   // if we're at our target, don't move
    96    if (pos.x != target.x || pos.y != target.y) {
     95   bool moving = (pos.x != target.x || pos.y != target.y);
     96
     97   if (moving) {
    9798      float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
    9899      double angle = atan2(target.y-pos.y, target.x-pos.x);
     
    107108         newPos.y = pos.y + sin(angle)*pixels;
    108109      }
    109 
    110       switch(map->getElement(newPos.x/25, newPos.y/25)) {
    111       case WorldMap::TERRAIN_NONE:
    112       case WorldMap::TERRAIN_OCEAN:
    113       case WorldMap::TERRAIN_ROCK:
    114          target.x = pos.x;
    115          target.y = pos.y;
    116          moveCanceled = true;
    117          break;
    118       default: // if there are no obstacles
    119          pos.x = newPos.x;
    120          pos.y = newPos.y;
    121          break;
    122       }
    123 
    124       // using moveCanceled in a hacky way just to indicate that the server
    125       // has updated some player info. Should change the variable name
    126       switch(map->getStructure(newPos.x/25, newPos.y/25)) {
    127       case WorldMap::STRUCTURE_BLUE_FLAG:
    128          hasBlueFlag = true;
    129          moveCanceled = true;
    130          break;
    131       case WorldMap::STRUCTURE_RED_FLAG:
    132          hasRedFlag = true;
    133          moveCanceled = true;
    134          break;
    135       }
    136110   }
    137111
    138112   timeLastUpdated = curTime;
    139113
    140    return !moveCanceled;
     114   return moving;
    141115}
  • server/server.cpp

    r45b2750 r23559e7  
    139139
    140140         // maybe put this in a separate method
    141          map<unsigned int, Player>::iterator it, it2;
     141         map<unsigned int, Player>::iterator it;
     142         FLOAT_POSITION oldPos;
     143         bool broadcastMove = false;
    142144         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
    143             if (!it->second.move(gameMap)) {
    144                cout << "Cenceling move" << endl;
    145                serverMsg.type = MSG_TYPE_PLAYER;
    146                it->second.serialize(serverMsg.buffer);
    147 
    148                cout << "(" << it->second.pos.x << "," << it->second.pos.y << ")" << endl;
    149 
    150                if (it->second.hasBlueFlag)
    151                   cout << "Got blue flag" << endl;
    152                if (it->second.hasRedFlag)
    153                   cout << "Got red flag" << endl;
    154 
    155                cout << "about to send move cencellation" << endl;
    156                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    157                {
    158                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    159                      error("sendMessage");
     145            oldPos = it->second.pos;
     146            if (it->second.move(gameMap)) {
     147
     148               // check if the move needs to be canceled
     149               switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
     150                  case WorldMap::TERRAIN_NONE:
     151                  case WorldMap::TERRAIN_OCEAN:
     152                  case WorldMap::TERRAIN_ROCK:
     153                     it->second.pos = oldPos;
     154                     it->second.target.x = it->second.pos.x;
     155                     it->second.target.y = it->second.pos.y;
     156                     broadcastMove = true;
     157                     break;
     158                  default:
     159                     // if there are no obstacles, do nothing
     160                     break;
     161               }
     162
     163               switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
     164                  case WorldMap::STRUCTURE_BLUE_FLAG:
     165                     cout << "Got blue flag" << endl;
     166                     it->second.hasBlueFlag = true;
     167                     broadcastMove = true;
     168                     break;
     169                  case WorldMap::STRUCTURE_RED_FLAG:
     170                     cout << "Got red flag" << endl;
     171                     it->second.hasRedFlag = true;
     172                     broadcastMove = true;
     173                     break;
     174               }
     175
     176               if (broadcastMove) {
     177                  serverMsg.type = MSG_TYPE_PLAYER;
     178                  it->second.serialize(serverMsg.buffer);
     179
     180                  cout << "about to broadcast move" << endl;
     181                  map<unsigned int, Player>::iterator it, it2;
     182                  for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
     183                  {
     184                     if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     185                        error("sendMessage");
     186                  }
    160187               }
    161188            }
Note: See TracChangeset for help on using the changeset viewer.