Changeset a1a3bd5 in network-game for common/Player.cpp


Ignore:
Timestamp:
Apr 23, 2013, 1:31:54 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
227baaa
Parents:
054b50b
Message:

Made client changes for smooth player movement, changed the player move method to check the map for obstacles and return a bool indicating success or failure.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r054b50b ra1a3bd5  
    8383}
    8484
    85 void Player::move(void) {
     85bool Player::move(WorldMap map) {
    8686   int speed = 100; // pixels per second
    8787   unsigned long long curTime = getCurrentMillis();
     88   bool moveCanceled = false;
    8889
    8990   // if we're at our target, don't move
    9091   if (pos.x != target.x || pos.y != target.y) {
    9192      float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
     93      double angle = atan2(target.y-pos.y, target.x-pos.x);
     94      float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
     95      POSITION newPos;
    9296
    93       double angle = atan2(target.y-pos.y, target.x-pos.x);
    94 
    95       float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
    9697      if (dist <= pixels) {
    9798         pos.x = target.x;
    9899         pos.y = target.y;
    99100      }else {
    100          pos.x += cos(angle)*pixels;
    101          pos.y += sin(angle)*pixels;
     101         newPos.x = int(pos.x + cos(angle)*pixels);
     102         newPos.y = int(pos.y + sin(angle)*pixels);
     103
     104         switch(map.getElement(newPos.x/25, newPos.y/25)) {
     105            case WorldMap.TerrainType.TERRAIN_OCEAN:
     106            case WorldMap.TerrainType.TERRAIN_ROCK:
     107               target.x = pos.x;
     108               target.y = pos.y;
     109               moveCanceled = true;
     110               break;
     111         }
    102112      }
    103113   }
    104114
    105115   timeLastUpdated = curTime;
     116   return !moveCanceled;
    106117}
Note: See TracChangeset for help on using the changeset viewer.