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