Changes in common/Player.cpp [8f85180:01d0d00] in network-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r8f85180 r01d0d00  
    44#include <sstream>
    55#include <cstring>
    6 #include <cmath>
    76
    87using namespace std;
     
    1312   this->name = "";
    1413   this->password = "";
    15    this->pos.x = this->target.x = 0;
    16    this->pos.y = this->target.y = 0;
    17    this->timeLastUpdated = 0;
     14   this->pos.x = 0;
     15   this->pos.y = 0;
    1816}
    1917
     
    2523   this->pos.x = p.pos.x;
    2624   this->pos.y = p.pos.y;
    27    this->target.x = p.target.x;
    28    this->target.y = p.target.y;
    2925   this->addr = p.addr;
    3026}
     
    3531   this->name = name;
    3632   this->password = password;
    37    this->pos.x = this->target.x = 200;
    38    this->pos.y = this->target.y = 200;
     33   this->pos.x = 200;
     34   this->pos.y = 200;
    3935}
    4036
     
    4440   this->name = name;
    4541   this->password = "";
    46    this->pos.x = this->target.x = 200;
    47    this->pos.y = this->target.y = 200;
     42   this->pos.x = 200;
     43   this->pos.y = 200;
    4844   this->addr = addr;
    4945}
     
    5551void Player::serialize(char* buffer)
    5652{
    57    memcpy(buffer, &this->id, 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());
     53   ostringstream oss;
     54
     55   oss.write((char*)&(this->id), sizeof(int));
     56   oss << this->name;
     57   oss.write("\0", 1);
     58   oss.write((char*)&(this->pos.x), sizeof(int));
     59   oss.write((char*)&(this->pos.y), sizeof(int));
     60
     61   memcpy(buffer, oss.str().c_str(), this->name.length()+1+2*sizeof(int));
    6362}
    6463
    6564void Player::deserialize(char* buffer)
    6665{
    67    memcpy(&this->id, buffer, 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);
     66   istringstream iss;
     67   iss.str(buffer);
    7368
    74    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;
    79    cout << "name: " << this->name << endl;
     69   iss.read((char*)&(this->id), sizeof(int));
     70   iss >> this->name;
     71   iss.read((char*)&(this->pos.x), sizeof(int));
     72   iss.read((char*)&(this->pos.y), sizeof(int));
    8073}
    8174
     
    8982   this->addr = addr;
    9083}
    91 
    92 void Player::move(void) {
    93    int speed = 100; // pixels per second
    94    unsigned long long curTime = getCurrentMillis();
    95 
    96    // if we're at our target, don't move
    97    if (pos.x == target.x && pos.y == target.y)
    98       cout << "We're already at our target" << endl;
    99    else {
    100       float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
    101       cout << "We need to move " << pixels << " pixels" << endl;
    102 
    103       double angle = atan2(target.y-pos.y, target.x-pos.x);
    104 
    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       }
    113    }
    114 
    115    timeLastUpdated = curTime;
    116 }
Note: See TracChangeset for help on using the changeset viewer.