source: network-game/common/Player.cpp@ 9ee50ce

Last change on this file since 9ee50ce was 0678d60, checked in by dportnoy <dmp1488@…>, 11 years ago

All server warnings have been fixed and the WorldMap class has a new method to create flags (and, in the future, other objects) based on structures present on the map

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#include "Player.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstring>
6#include <cmath>
7
8using namespace std;
9
10Player::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
39Player::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
72Player::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
101Player::~Player()
102{
103}
104
105unsigned int Player::getId()
106{
107 return this->id;
108}
109
110unsigned int Player::getTargetPlayer()
111{
112 return this->targetPlayer;
113}
114
115void Player::setId(unsigned int id)
116{
117 this->id = id;
118}
119
120void Player::setTargetPlayer(unsigned int id)
121{
122 this->targetPlayer = id;
123}
124
125void Player::setAddr(sockaddr_in addr)
126{
127 this->addr = addr;
128}
129
130void 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 }
153}
154
155void Player::serialize(char* buffer)
156{
157 memcpy(buffer, &this->id, 4);
158 memcpy(buffer+4, &this->pos.x, 4);
159 memcpy(buffer+8, &this->pos.y, 4);
160 memcpy(buffer+12, &this->target.x, 4);
161 memcpy(buffer+16, &this->target.y, 4);
162
163 memcpy(buffer+20, &this->playerClass, 4);
164 memcpy(buffer+24, &this->maxHealth, 4);
165 memcpy(buffer+28, &this->health, 4);
166 memcpy(buffer+32, &this->attackType, 4);
167 memcpy(buffer+36, &this->damage, 4);
168 memcpy(buffer+40, &this->team, 4);
169 memcpy(buffer+44, &this->hasBlueFlag, 1);
170 memcpy(buffer+45, &this->hasRedFlag, 1);
171 memcpy(buffer+46, &this->range, 4);
172
173 strcpy(buffer+50, this->name.c_str());
174}
175
176void Player::deserialize(char* buffer)
177{
178 memcpy(&this->id, buffer, 4);
179 memcpy(&this->pos.x, buffer+4, 4);
180 memcpy(&this->pos.y, buffer+8, 4);
181 memcpy(&this->target.x, buffer+12, 4);
182 memcpy(&this->target.y, buffer+16, 4);
183
184 memcpy(&this->playerClass, buffer+20, 4);
185 memcpy(&this->maxHealth, buffer+24, 4);
186 memcpy(&this->health, buffer+28, 4);
187 memcpy(&this->attackType, buffer+32, 4);
188 memcpy(&this->damage, buffer+36, 4);
189 memcpy(&this->team, buffer+40, 4);
190 memcpy(&this->hasBlueFlag, buffer+44, 1);
191 memcpy(&this->hasRedFlag, buffer+45, 1);
192 memcpy(&this->range, buffer+46, 4);
193
194 this->name.assign(buffer+50);
195}
196
197bool Player::move(WorldMap *map) {
198 int speed = 100; // pixels per second. should probably be in the constructor
199 unsigned long long curTime = getCurrentMillis();
200
201 // if we're at our target, don't move
202 bool moving = (pos.x != target.x || pos.y != target.y);
203
204 if (moving) {
205 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
206 double angle = atan2(target.y-pos.y, target.x-pos.x);
207 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
208
209 if (dist <= pixels) {
210 pos.x = target.x;
211 pos.y = target.y;
212 }else {
213 pos.x = pos.x + cos(angle)*pixels;
214 pos.y = pos.y + sin(angle)*pixels;
215 }
216 }
217
218 timeLastUpdated = curTime;
219
220 return moving;
221}
222
223void Player::takeDamage(int damage) {
224 this->health -= damage;
225 if (this->health < 0)
226 this->health = 0;
227 if (this->health == 0) {
228 cout << "Player died" << endl;
229 this->isDead = true;
230 this->timeDied = getCurrentMillis();
231 }
232}
233
234bool Player::updateTarget(map<unsigned int, Player*>& players) {
235 Player* p = NULL;
236 if (this->targetPlayer > 0)
237 p =players[this->targetPlayer];
238
239 if (p != NULL && this->isChasing) {
240 this->target.x = p->pos.x;
241 this->target.y = p->pos.y;
242
243 if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
244 this->target.x = this->pos.x;
245 this->target.y = this->pos.y;
246
247 this->isChasing = false;
248 this->isAttacking = true;
249 this->timeAttackStarted = getCurrentMillis();
250
251 return true;
252 }
253 }
254
255 return false;
256}
Note: See TracBrowser for help on using the repository browser.