Changeset 8f85180 in network-game for common/Player.cpp


Ignore:
Timestamp:
Feb 24, 2013, 12:09:43 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
ca44f82
Parents:
66906aa
Message:

Added a method for measuring milliseconds and implemented smooth player movement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r66906aa r8f85180  
    1515   this->pos.x = this->target.x = 0;
    1616   this->pos.y = this->target.y = 0;
     17   this->timeLastUpdated = 0;
    1718}
    1819
     
    5556{
    5657   memcpy(buffer, &this->id, 4);
    57    strcpy(buffer+4, this->name.c_str());
    58    memcpy(buffer+5+this->name.length(), &this->pos.x, 4);
    59    memcpy(buffer+9+this->name.length(), &this->pos.y, 4);
     58   memcpy(buffer+4, &this->pos.x, 4);
     59   memcpy(buffer+8, &this->pos.y, 4);
     60   memcpy(buffer+12, &this->target.x, 4);
     61   memcpy(buffer+16, &this->target.y, 4);
     62   strcpy(buffer+20, this->name.c_str());
    6063}
    6164
     
    6366{
    6467   memcpy(&this->id, buffer, 4);
    65    this->name.assign(buffer+4);
    66    memcpy(&this->pos.x, buffer+5+this->name.size(), 4);
    67    memcpy(&this->pos.y, buffer+9+this->name.size(), 4);
     68   memcpy(&this->pos.x, buffer+4, 4);
     69   memcpy(&this->pos.y, buffer+8, 4);
     70   memcpy(&this->target.x, buffer+12, 4);
     71   memcpy(&this->target.y, buffer+16, 4);
     72   this->name.assign(buffer+20);
    6873
    6974   cout << "id: " << this->id << endl;
     75   cout << "pos x: " << this->pos.x << endl;
     76   cout << "pos y: " << this->pos.y << endl;
     77   cout << "target x: " << this->target.x << endl;
     78   cout << "target y: " << this->target.y << endl;
    7079   cout << "name: " << this->name << endl;
    71    cout << "x: " << this->pos.x << endl;
    72    cout << "y: " << this->pos.y << endl;
    7380}
    7481
     
    8491
    8592void Player::move(void) {
    86    // timeLastMoved
    87    // pos
    88    // target
    8993   int speed = 100; // pixels per second
    90 
    91    timespec curTS, diffTS;
    92    clock_gettime(CLOCK_REALTIME, &curTS);
    93 
    94    // get time elapsed
    95    diffTS.tv_sec = curTS.tv_sec - timeLastUpdated.tv_sec;
    96    diffTS.tv_nsec = curTS.tv_nsec - timeLastUpdated.tv_nsec;
    97    if (diffTS.tv_nsec < 0) {
    98       diffTS.tv_sec -= 1;
    99       diffTS.tv_nsec += 1000000000;
    100    }
    101 
    102    cout << "elapsed secs: " << diffTS.tv_sec << endl;
    103    cout << "elapsed nsecs: " << diffTS.tv_nsec << endl;
     94   unsigned long long curTime = getCurrentMillis();
    10495
    10596   // if we're at our target, don't move
    106    if (pos.x == target.x || pos.y == target.y)
     97   if (pos.x == target.x && pos.y == target.y)
    10798      cout << "We're already at our target" << endl;
    10899   else {
    109       float pixels = speed * (diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);
     100      float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
    110101      cout << "We need to move " << pixels << " pixels" << endl;
    111102
    112103      double angle = atan2(target.y-pos.y, target.x-pos.x);
    113104
    114       // we just need to check that we don't overjump the target
    115       pos.x += cos(angle)*pixels;
    116       pos.y += sin(angle)*pixels;
     105      float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
     106      if (dist <= pixels) {
     107         pos.x = target.x;
     108         pos.y = target.y;
     109      }else {
     110         pos.x += cos(angle)*pixels;
     111         pos.y += sin(angle)*pixels;
     112      }
    117113   }
    118114
    119    timeLastUpdated.tv_sec = curTS.tv_sec;
    120    timeLastUpdated.tv_nsec = curTS.tv_nsec;
     115   timeLastUpdated = curTime;
    121116}
Note: See TracChangeset for help on using the changeset viewer.