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->targetPlayer = 0;
|
---|
18 | this->timeLastUpdated = 0;
|
---|
19 | this->timeAttackStarted = 0;
|
---|
20 | this->timeDied = 0;
|
---|
21 | this->isChasing = false;
|
---|
22 | this->isAttacking = false;
|
---|
23 | this->isDead = false;
|
---|
24 |
|
---|
25 | this->playerClass = CLASS_NONE;
|
---|
26 | this->maxHealth = 0;
|
---|
27 | this->health = 0;
|
---|
28 | this->attackType = ATTACK_NONE;
|
---|
29 | this->damage = 0;
|
---|
30 | this->range = 0;
|
---|
31 | this->attackCooldown = 0;
|
---|
32 | this->team = 0; // blue team by default
|
---|
33 | this->hasBlueFlag = false;
|
---|
34 | this->hasRedFlag = false;
|
---|
35 |
|
---|
36 | this->currentGame = NULL;
|
---|
37 | }
|
---|
38 |
|
---|
39 | Player::Player(const Player& p)
|
---|
40 | {
|
---|
41 | this->id = p.id;
|
---|
42 | this->name = p.name;
|
---|
43 | this->password = p.password;
|
---|
44 | this->addr = p.addr;
|
---|
45 | this->pos.x = p.pos.x;
|
---|
46 | this->pos.y = p.pos.y;
|
---|
47 | this->target.x = p.target.x;
|
---|
48 | this->target.y = p.target.y;
|
---|
49 | this->targetPlayer = p.targetPlayer;
|
---|
50 | this->timeLastUpdated = p.timeLastUpdated;
|
---|
51 | this->timeAttackStarted = p.timeAttackStarted;
|
---|
52 | this->timeDied = p.timeDied;
|
---|
53 | this->isChasing = p.isChasing;
|
---|
54 | this->isAttacking = p.isAttacking;
|
---|
55 | this->isDead = p.isDead;
|
---|
56 |
|
---|
57 | this->playerClass = p.playerClass;
|
---|
58 | this->maxHealth = p.maxHealth;
|
---|
59 | this->health = p.health;
|
---|
60 | this->attackType = p.attackType;
|
---|
61 | this->damage = p.damage;
|
---|
62 | this->range = p.range;
|
---|
63 | this->attackCooldown = p.attackCooldown;
|
---|
64 | this->team = p.team;
|
---|
65 | this->hasBlueFlag = p.hasBlueFlag;
|
---|
66 | this->hasRedFlag = p.hasRedFlag;
|
---|
67 |
|
---|
68 | this->currentGame = p.currentGame;
|
---|
69 | }
|
---|
70 |
|
---|
71 | // eventually make this take a PlayerClass argument as well
|
---|
72 | Player::Player(string name, string password)
|
---|
73 | {
|
---|
74 | this->id = 0;
|
---|
75 | this->name = name;
|
---|
76 | this->password = password;
|
---|
77 | this->pos.x = this->target.x = 200;
|
---|
78 | this->pos.y = this->target.y = 200;
|
---|
79 | this->targetPlayer = 0;
|
---|
80 | this->timeLastUpdated = 0;
|
---|
81 | this->timeAttackStarted = 0;
|
---|
82 | this->timeDied = 0;
|
---|
83 | this->isChasing = false;
|
---|
84 | this->isAttacking = false;
|
---|
85 | this->isDead = false;
|
---|
86 |
|
---|
87 | this->playerClass = CLASS_NONE;
|
---|
88 | this->maxHealth = 0;
|
---|
89 | this->health = 0;
|
---|
90 | this->attackType = ATTACK_NONE;
|
---|
91 | this->damage = 0;
|
---|
92 | this->range = 0;
|
---|
93 | this->attackCooldown = 0;
|
---|
94 | this->team = 0; // blue team by default
|
---|
95 | this->hasBlueFlag = false;
|
---|
96 | this->hasRedFlag = false;
|
---|
97 |
|
---|
98 | this->currentGame = NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | Player::~Player()
|
---|
102 | {
|
---|
103 | }
|
---|
104 |
|
---|
105 | unsigned int Player::getId()
|
---|
106 | {
|
---|
107 | return this->id;
|
---|
108 | }
|
---|
109 |
|
---|
110 | unsigned int Player::getTargetPlayer()
|
---|
111 | {
|
---|
112 | return this->targetPlayer;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void Player::setId(unsigned int id)
|
---|
116 | {
|
---|
117 | this->id = id;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void Player::setTargetPlayer(unsigned int id)
|
---|
121 | {
|
---|
122 | this->targetPlayer = id;
|
---|
123 | }
|
---|
124 |
|
---|
125 | void Player::setAddr(sockaddr_in addr)
|
---|
126 | {
|
---|
127 | this->addr = addr;
|
---|
128 | }
|
---|
129 |
|
---|
130 | void Player::setClass(PlayerClass c)
|
---|
131 | {
|
---|
132 | switch (c) {
|
---|
133 | case CLASS_WARRIOR:
|
---|
134 | this->playerClass = CLASS_WARRIOR;
|
---|
135 | this->maxHealth = this->health = 120;
|
---|
136 | this->attackType = ATTACK_MELEE;
|
---|
137 | this->damage = 10;
|
---|
138 | this->range = 30;
|
---|
139 | this->attackCooldown = 800;
|
---|
140 | break;
|
---|
141 | case CLASS_RANGER:
|
---|
142 | this->playerClass = CLASS_RANGER;
|
---|
143 | this->maxHealth = this->health = 60;
|
---|
144 | this->attackType = ATTACK_RANGED;
|
---|
145 | this->damage = 6;
|
---|
146 | this->range = 100;
|
---|
147 | this->attackCooldown = 1000;
|
---|
148 | break;
|
---|
149 | case CLASS_NONE:
|
---|
150 | cout << "No class" << endl;
|
---|
151 | break;
|
---|
152 | dafault:
|
---|
153 | cout << "nvalid class" << endl;
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | void Player::serialize(char* buffer)
|
---|
159 | {
|
---|
160 | memcpy(buffer, &this->id, 4);
|
---|
161 | memcpy(buffer+4, &this->pos.x, 4);
|
---|
162 | memcpy(buffer+8, &this->pos.y, 4);
|
---|
163 | memcpy(buffer+12, &this->target.x, 4);
|
---|
164 | memcpy(buffer+16, &this->target.y, 4);
|
---|
165 |
|
---|
166 | memcpy(buffer+20, &this->playerClass, 4);
|
---|
167 | memcpy(buffer+24, &this->maxHealth, 4);
|
---|
168 | memcpy(buffer+28, &this->health, 4);
|
---|
169 | memcpy(buffer+32, &this->attackType, 4);
|
---|
170 | memcpy(buffer+36, &this->damage, 4);
|
---|
171 | memcpy(buffer+40, &this->team, 4);
|
---|
172 | memcpy(buffer+44, &this->hasBlueFlag, 1);
|
---|
173 | memcpy(buffer+45, &this->hasRedFlag, 1);
|
---|
174 | memcpy(buffer+46, &this->range, 4);
|
---|
175 |
|
---|
176 | strcpy(buffer+50, this->name.c_str());
|
---|
177 | }
|
---|
178 |
|
---|
179 | void Player::deserialize(char* buffer)
|
---|
180 | {
|
---|
181 | memcpy(&this->id, buffer, 4);
|
---|
182 | memcpy(&this->pos.x, buffer+4, 4);
|
---|
183 | memcpy(&this->pos.y, buffer+8, 4);
|
---|
184 | memcpy(&this->target.x, buffer+12, 4);
|
---|
185 | memcpy(&this->target.y, buffer+16, 4);
|
---|
186 |
|
---|
187 | memcpy(&this->playerClass, buffer+20, 4);
|
---|
188 | memcpy(&this->maxHealth, buffer+24, 4);
|
---|
189 | memcpy(&this->health, buffer+28, 4);
|
---|
190 | memcpy(&this->attackType, buffer+32, 4);
|
---|
191 | memcpy(&this->damage, buffer+36, 4);
|
---|
192 | memcpy(&this->team, buffer+40, 4);
|
---|
193 | memcpy(&this->hasBlueFlag, buffer+44, 1);
|
---|
194 | memcpy(&this->hasRedFlag, buffer+45, 1);
|
---|
195 | memcpy(&this->range, buffer+46, 4);
|
---|
196 |
|
---|
197 | this->name.assign(buffer+50);
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool Player::move(WorldMap *map) {
|
---|
201 | int speed = 100; // pixels per second. should probably be in the constructor
|
---|
202 | unsigned long long curTime = getCurrentMillis();
|
---|
203 |
|
---|
204 | // if we're at our target, don't move
|
---|
205 | bool moving = (pos.x != target.x || pos.y != target.y);
|
---|
206 |
|
---|
207 | if (moving) {
|
---|
208 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
209 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
210 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
---|
211 |
|
---|
212 | if (dist <= pixels) {
|
---|
213 | pos.x = target.x;
|
---|
214 | pos.y = target.y;
|
---|
215 | }else {
|
---|
216 | pos.x = pos.x + cos(angle)*pixels;
|
---|
217 | pos.y = pos.y + sin(angle)*pixels;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | timeLastUpdated = curTime;
|
---|
222 |
|
---|
223 | return moving;
|
---|
224 | }
|
---|
225 |
|
---|
226 | void Player::takeDamage(int damage) {
|
---|
227 | this->health -= damage;
|
---|
228 | if (this->health < 0)
|
---|
229 | this->health = 0;
|
---|
230 | if (this->health == 0) {
|
---|
231 | cout << "Player died" << endl;
|
---|
232 | this->isDead = true;
|
---|
233 | this->timeDied = getCurrentMillis();
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | bool Player::updateTarget(map<unsigned int, Player*>& players) {
|
---|
238 | Player* p = NULL;
|
---|
239 | if (this->targetPlayer > 0)
|
---|
240 | p =players[this->targetPlayer];
|
---|
241 |
|
---|
242 | if (p != NULL && this->isChasing) {
|
---|
243 | this->target.x = p->pos.x;
|
---|
244 | this->target.y = p->pos.y;
|
---|
245 |
|
---|
246 | if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
|
---|
247 | this->target.x = this->pos.x;
|
---|
248 | this->target.y = this->pos.y;
|
---|
249 |
|
---|
250 | this->isChasing = false;
|
---|
251 | this->isAttacking = true;
|
---|
252 | this->timeAttackStarted = getCurrentMillis();
|
---|
253 |
|
---|
254 | return true;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | return false;
|
---|
259 | }
|
---|