1 | #include "Player.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 | #include <cstring>
|
---|
6 | #include <cmath>
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | Player::Player()
|
---|
11 | {
|
---|
12 | this->id = 0;
|
---|
13 | this->name = "";
|
---|
14 | this->password = "";
|
---|
15 | this->pos.x = this->target.x = 0;
|
---|
16 | this->pos.y = this->target.y = 0;
|
---|
17 | this->timeLastUpdated = 0;
|
---|
18 | this->team = 0; // blue team by default
|
---|
19 | this->hasBlueFlag = false;
|
---|
20 | this->hasRedFlag = false;
|
---|
21 | }
|
---|
22 |
|
---|
23 | Player::Player(const Player& p)
|
---|
24 | {
|
---|
25 | this->id = p.id;
|
---|
26 | this->name = p.name;
|
---|
27 | this->password = p.password;
|
---|
28 | this->pos.x = p.pos.x;
|
---|
29 | this->pos.y = p.pos.y;
|
---|
30 | this->target.x = p.target.x;
|
---|
31 | this->target.y = p.target.y;
|
---|
32 | this->addr = p.addr;
|
---|
33 | this->team = 0; // blue team by default
|
---|
34 | this->hasBlueFlag = false;
|
---|
35 | this->hasRedFlag = false;
|
---|
36 | }
|
---|
37 |
|
---|
38 | Player::Player(string name, string password)
|
---|
39 | {
|
---|
40 | this->id = 0;
|
---|
41 | this->name = name;
|
---|
42 | this->password = password;
|
---|
43 | this->pos.x = this->target.x = 200;
|
---|
44 | this->pos.y = this->target.y = 200;
|
---|
45 | this->team = 0; // blue team by default
|
---|
46 | this->hasBlueFlag = false;
|
---|
47 | this->hasRedFlag = false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | Player::~Player()
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | void Player::serialize(char* buffer)
|
---|
55 | {
|
---|
56 | memcpy(buffer, &this->id, 4);
|
---|
57 | memcpy(buffer+4, &this->pos.x, 4);
|
---|
58 | memcpy(buffer+8, &this->pos.y, 4);
|
---|
59 | memcpy(buffer+12, &this->target.x, 4);
|
---|
60 | memcpy(buffer+16, &this->target.y, 4);
|
---|
61 | memcpy(buffer+20, &this->team, 4);
|
---|
62 | memcpy(buffer+24, &this->hasBlueFlag, 1);
|
---|
63 | memcpy(buffer+25, &this->hasRedFlag, 1);
|
---|
64 | strcpy(buffer+26, this->name.c_str());
|
---|
65 | }
|
---|
66 |
|
---|
67 | void Player::deserialize(char* buffer)
|
---|
68 | {
|
---|
69 | memcpy(&this->id, buffer, 4);
|
---|
70 | memcpy(&this->pos.x, buffer+4, 4);
|
---|
71 | memcpy(&this->pos.y, buffer+8, 4);
|
---|
72 | memcpy(&this->target.x, buffer+12, 4);
|
---|
73 | memcpy(&this->target.y, buffer+16, 4);
|
---|
74 | memcpy(&this->team, buffer+20, 4);
|
---|
75 | memcpy(&this->hasBlueFlag, buffer+24, 1);
|
---|
76 | memcpy(&this->hasRedFlag, buffer+25, 1);
|
---|
77 | this->name.assign(buffer+26);
|
---|
78 | }
|
---|
79 |
|
---|
80 | void Player::setId(int id)
|
---|
81 | {
|
---|
82 | this->id = id;
|
---|
83 | }
|
---|
84 |
|
---|
85 | void Player::setAddr(sockaddr_in addr)
|
---|
86 | {
|
---|
87 | this->addr = addr;
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool Player::move(WorldMap *map) {
|
---|
91 | int speed = 100; // pixels per second. should probably be in the constructor
|
---|
92 | unsigned long long curTime = getCurrentMillis();
|
---|
93 |
|
---|
94 | // if we're at our target, don't move
|
---|
95 | bool moving = (pos.x != target.x || pos.y != target.y);
|
---|
96 |
|
---|
97 | if (moving) {
|
---|
98 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
99 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
100 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
---|
101 |
|
---|
102 | if (dist <= pixels) {
|
---|
103 | pos.x = target.x;
|
---|
104 | pos.y = target.y;
|
---|
105 | }else {
|
---|
106 | pos.x = pos.x + cos(angle)*pixels;
|
---|
107 | pos.y = pos.y + sin(angle)*pixels;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | timeLastUpdated = curTime;
|
---|
112 |
|
---|
113 | return moving;
|
---|
114 | }
|
---|