[8e540f4] | 1 | #include "Player.h"
|
---|
[2488852] | 2 |
|
---|
| 3 | #include <iostream>
|
---|
[3b8adee] | 4 | #include <sstream>
|
---|
| 5 | #include <cstring>
|
---|
[60017fc] | 6 | #include <cmath>
|
---|
[2488852] | 7 |
|
---|
| 8 | using namespace std;
|
---|
| 9 |
|
---|
[01d0d00] | 10 | Player::Player()
|
---|
| 11 | {
|
---|
| 12 | this->id = 0;
|
---|
| 13 | this->name = "";
|
---|
| 14 | this->password = "";
|
---|
[60017fc] | 15 | this->pos.x = this->target.x = 0;
|
---|
| 16 | this->pos.y = this->target.y = 0;
|
---|
[01d0d00] | 17 | }
|
---|
| 18 |
|
---|
| 19 | Player::Player(const Player& p)
|
---|
| 20 | {
|
---|
| 21 | this->id = p.id;
|
---|
| 22 | this->name = p.name;
|
---|
| 23 | this->password = p.password;
|
---|
| 24 | this->pos.x = p.pos.x;
|
---|
| 25 | this->pos.y = p.pos.y;
|
---|
[60017fc] | 26 | this->target.x = p.target.x;
|
---|
| 27 | this->target.y = p.target.y;
|
---|
[01d0d00] | 28 | this->addr = p.addr;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[59061f6] | 31 | Player::Player(string name, string password)
|
---|
| 32 | {
|
---|
[01d0d00] | 33 | this->id = 0;
|
---|
[59061f6] | 34 | this->name = name;
|
---|
| 35 | this->password = password;
|
---|
[60017fc] | 36 | this->pos.x = this->target.x = 200;
|
---|
| 37 | this->pos.y = this->target.y = 200;
|
---|
[59061f6] | 38 | }
|
---|
| 39 |
|
---|
[8e540f4] | 40 | Player::Player(string name, sockaddr_in addr)
|
---|
[2488852] | 41 | {
|
---|
[01d0d00] | 42 | this->id = 0;
|
---|
[2488852] | 43 | this->name = name;
|
---|
[59061f6] | 44 | this->password = "";
|
---|
[60017fc] | 45 | this->pos.x = this->target.x = 200;
|
---|
| 46 | this->pos.y = this->target.y = 200;
|
---|
[2488852] | 47 | this->addr = addr;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[8e540f4] | 50 | Player::~Player()
|
---|
[2488852] | 51 | {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[3b8adee] | 54 | void Player::serialize(char* buffer)
|
---|
[59061f6] | 55 | {
|
---|
[80b3f94] | 56 | memcpy(buffer, &this->id, 4);
|
---|
| 57 | strcpy(buffer+4, this->name.c_str());
|
---|
[ad5d122] | 58 | memcpy(buffer+5+this->name.length(), &this->pos.x, 4);
|
---|
| 59 | memcpy(buffer+9+this->name.length(), &this->pos.y, 4);
|
---|
[59061f6] | 60 | }
|
---|
[edfd1d0] | 61 |
|
---|
[3b8adee] | 62 | void Player::deserialize(char* buffer)
|
---|
[edfd1d0] | 63 | {
|
---|
[80b3f94] | 64 | memcpy(&this->id, buffer, 4);
|
---|
[5806dc2] | 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);
|
---|
[3b8adee] | 68 |
|
---|
[80b3f94] | 69 | cout << "id: " << this->id << endl;
|
---|
[5806dc2] | 70 | cout << "name: " << this->name << endl;
|
---|
[80b3f94] | 71 | cout << "x: " << this->pos.x << endl;
|
---|
| 72 | cout << "y: " << this->pos.y << endl;
|
---|
[3b8adee] | 73 | }
|
---|
| 74 |
|
---|
[01d0d00] | 75 | void Player::setId(int id)
|
---|
| 76 | {
|
---|
| 77 | this->id = id;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[3b8adee] | 80 | void Player::setAddr(sockaddr_in addr)
|
---|
| 81 | {
|
---|
| 82 | this->addr = addr;
|
---|
[edfd1d0] | 83 | }
|
---|
[60017fc] | 84 |
|
---|
| 85 | void Player::move(void) {
|
---|
| 86 | // timeLastMoved
|
---|
| 87 | // pos
|
---|
| 88 | // target
|
---|
| 89 | 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;
|
---|
| 104 |
|
---|
[f401cac] | 105 | // if we're at our target, don't move
|
---|
| 106 | if (pos.x == target.x || pos.y == target.y)
|
---|
| 107 | cout << "We're already at our target" << endl;
|
---|
| 108 | else {
|
---|
| 109 | float pixels = speed * (diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);
|
---|
| 110 | cout << "We need to move " << pixels << " pixels" << endl;
|
---|
| 111 |
|
---|
| 112 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
| 113 |
|
---|
| 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;
|
---|
| 117 | }
|
---|
[60017fc] | 118 |
|
---|
| 119 | timeLastUpdated.tv_sec = curTS.tv_sec;
|
---|
| 120 | timeLastUpdated.tv_nsec = curTS.tv_nsec;
|
---|
| 121 | }
|
---|