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