[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;
|
---|
[8f85180] | 17 | this->timeLastUpdated = 0;
|
---|
[01d0d00] | 18 | }
|
---|
| 19 |
|
---|
| 20 | Player::Player(const Player& p)
|
---|
| 21 | {
|
---|
| 22 | this->id = p.id;
|
---|
| 23 | this->name = p.name;
|
---|
| 24 | this->password = p.password;
|
---|
| 25 | this->pos.x = p.pos.x;
|
---|
| 26 | this->pos.y = p.pos.y;
|
---|
[60017fc] | 27 | this->target.x = p.target.x;
|
---|
| 28 | this->target.y = p.target.y;
|
---|
[01d0d00] | 29 | this->addr = p.addr;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[59061f6] | 32 | Player::Player(string name, string password)
|
---|
| 33 | {
|
---|
[01d0d00] | 34 | this->id = 0;
|
---|
[59061f6] | 35 | this->name = name;
|
---|
| 36 | this->password = password;
|
---|
[60017fc] | 37 | this->pos.x = this->target.x = 200;
|
---|
| 38 | this->pos.y = this->target.y = 200;
|
---|
[59061f6] | 39 | }
|
---|
| 40 |
|
---|
[8e540f4] | 41 | Player::Player(string name, sockaddr_in addr)
|
---|
[2488852] | 42 | {
|
---|
[01d0d00] | 43 | this->id = 0;
|
---|
[2488852] | 44 | this->name = name;
|
---|
[59061f6] | 45 | this->password = "";
|
---|
[60017fc] | 46 | this->pos.x = this->target.x = 200;
|
---|
| 47 | this->pos.y = this->target.y = 200;
|
---|
[2488852] | 48 | this->addr = addr;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[8e540f4] | 51 | Player::~Player()
|
---|
[2488852] | 52 | {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[3b8adee] | 55 | void Player::serialize(char* buffer)
|
---|
[59061f6] | 56 | {
|
---|
[80b3f94] | 57 | memcpy(buffer, &this->id, 4);
|
---|
[8f85180] | 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());
|
---|
[59061f6] | 63 | }
|
---|
[edfd1d0] | 64 |
|
---|
[3b8adee] | 65 | void Player::deserialize(char* buffer)
|
---|
[edfd1d0] | 66 | {
|
---|
[80b3f94] | 67 | memcpy(&this->id, buffer, 4);
|
---|
[8f85180] | 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);
|
---|
[3b8adee] | 73 |
|
---|
[80b3f94] | 74 | cout << "id: " << this->id << endl;
|
---|
[8f85180] | 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;
|
---|
[5806dc2] | 79 | cout << "name: " << this->name << endl;
|
---|
[3b8adee] | 80 | }
|
---|
| 81 |
|
---|
[01d0d00] | 82 | void Player::setId(int id)
|
---|
| 83 | {
|
---|
| 84 | this->id = id;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[3b8adee] | 87 | void Player::setAddr(sockaddr_in addr)
|
---|
| 88 | {
|
---|
| 89 | this->addr = addr;
|
---|
[edfd1d0] | 90 | }
|
---|
[60017fc] | 91 |
|
---|
| 92 | void Player::move(void) {
|
---|
| 93 | int speed = 100; // pixels per second
|
---|
[8f85180] | 94 | unsigned long long curTime = getCurrentMillis();
|
---|
[60017fc] | 95 |
|
---|
[f401cac] | 96 | // if we're at our target, don't move
|
---|
[8f85180] | 97 | if (pos.x == target.x && pos.y == target.y)
|
---|
[f401cac] | 98 | cout << "We're already at our target" << endl;
|
---|
| 99 | else {
|
---|
[8f85180] | 100 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
[f401cac] | 101 | cout << "We need to move " << pixels << " pixels" << endl;
|
---|
| 102 |
|
---|
| 103 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
| 104 |
|
---|
[8f85180] | 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 | }
|
---|
[f401cac] | 113 | }
|
---|
[60017fc] | 114 |
|
---|
[8f85180] | 115 | timeLastUpdated = curTime;
|
---|
[60017fc] | 116 | }
|
---|