| 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; // no team by default
|
|---|
| 33 | this->hasBlueFlag = false;
|
|---|
| 34 | this->hasRedFlag = false;
|
|---|
| 35 |
|
|---|
| 36 | this->level = 0;
|
|---|
| 37 | this->experience = 0;
|
|---|
| 38 | this->honor = 0;
|
|---|
| 39 | this->wins = 0;
|
|---|
| 40 | this->losses = 0;
|
|---|
| 41 |
|
|---|
| 42 | this->currentGame = NULL;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | Player::Player(const Player& p)
|
|---|
| 46 | {
|
|---|
| 47 | this->id = p.id;
|
|---|
| 48 | this->name = p.name;
|
|---|
| 49 | this->password = p.password;
|
|---|
| 50 | this->addr = p.addr;
|
|---|
| 51 | this->pos.x = p.pos.x;
|
|---|
| 52 | this->pos.y = p.pos.y;
|
|---|
| 53 | this->target.x = p.target.x;
|
|---|
| 54 | this->target.y = p.target.y;
|
|---|
| 55 | this->targetPlayer = p.targetPlayer;
|
|---|
| 56 | this->timeLastUpdated = p.timeLastUpdated;
|
|---|
| 57 | this->timeAttackStarted = p.timeAttackStarted;
|
|---|
| 58 | this->timeDied = p.timeDied;
|
|---|
| 59 | this->isChasing = p.isChasing;
|
|---|
| 60 | this->isAttacking = p.isAttacking;
|
|---|
| 61 | this->isDead = p.isDead;
|
|---|
| 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;
|
|---|
| 68 | this->range = p.range;
|
|---|
| 69 | this->attackCooldown = p.attackCooldown;
|
|---|
| 70 | this->team = p.team;
|
|---|
| 71 | this->hasBlueFlag = p.hasBlueFlag;
|
|---|
| 72 | this->hasRedFlag = p.hasRedFlag;
|
|---|
| 73 |
|
|---|
| 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 |
|
|---|
| 80 | this->currentGame = p.currentGame;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | // eventually make this take a PlayerClass argument as well
|
|---|
| 84 | Player::Player(string name, string password)
|
|---|
| 85 | {
|
|---|
| 86 | this->id = 0;
|
|---|
| 87 | this->name = name;
|
|---|
| 88 | this->password = password;
|
|---|
| 89 | this->pos.x = this->target.x = 200;
|
|---|
| 90 | this->pos.y = this->target.y = 200;
|
|---|
| 91 | this->targetPlayer = 0;
|
|---|
| 92 | this->timeLastUpdated = 0;
|
|---|
| 93 | this->timeAttackStarted = 0;
|
|---|
| 94 | this->timeDied = 0;
|
|---|
| 95 | this->isChasing = false;
|
|---|
| 96 | this->isAttacking = false;
|
|---|
| 97 | this->isDead = false;
|
|---|
| 98 |
|
|---|
| 99 | this->playerClass = CLASS_NONE;
|
|---|
| 100 | this->maxHealth = 0;
|
|---|
| 101 | this->health = 0;
|
|---|
| 102 | this->attackType = ATTACK_NONE;
|
|---|
| 103 | this->damage = 0;
|
|---|
| 104 | this->range = 0;
|
|---|
| 105 | this->attackCooldown = 0;
|
|---|
| 106 | this->team = 0; // no team by default
|
|---|
| 107 | this->hasBlueFlag = false;
|
|---|
| 108 | this->hasRedFlag = false;
|
|---|
| 109 |
|
|---|
| 110 | this->level = 0;
|
|---|
| 111 | this->experience = 0;
|
|---|
| 112 | this->honor = 0;
|
|---|
| 113 | this->wins = 0;
|
|---|
| 114 | this->losses = 0;
|
|---|
| 115 |
|
|---|
| 116 | this->currentGame = NULL;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | Player::~Player()
|
|---|
| 120 | {
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | unsigned int Player::getId()
|
|---|
| 124 | {
|
|---|
| 125 | return this->id;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | unsigned int Player::getTargetPlayer()
|
|---|
| 129 | {
|
|---|
| 130 | return this->targetPlayer;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void Player::setId(unsigned int id)
|
|---|
| 134 | {
|
|---|
| 135 | this->id = id;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | void Player::setTargetPlayer(unsigned int id)
|
|---|
| 139 | {
|
|---|
| 140 | this->targetPlayer = id;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 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;
|
|---|
| 153 | this->maxHealth = this->health = 120;
|
|---|
| 154 | this->attackType = ATTACK_MELEE;
|
|---|
| 155 | this->damage = 10;
|
|---|
| 156 | this->range = 30;
|
|---|
| 157 | this->attackCooldown = 800;
|
|---|
| 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;
|
|---|
| 164 | this->range = 100;
|
|---|
| 165 | this->attackCooldown = 1000;
|
|---|
| 166 | break;
|
|---|
| 167 | case CLASS_NONE:
|
|---|
| 168 | cout << "No class" << endl;
|
|---|
| 169 | break;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | void Player::serialize(char* buffer)
|
|---|
| 174 | {
|
|---|
| 175 | memcpy(buffer, &this->id, 4);
|
|---|
| 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);
|
|---|
| 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);
|
|---|
| 189 | memcpy(buffer+46, &this->range, 4);
|
|---|
| 190 |
|
|---|
| 191 | strcpy(buffer+50, this->name.c_str());
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | void Player::deserialize(char* buffer)
|
|---|
| 195 | {
|
|---|
| 196 | memcpy(&this->id, buffer, 4);
|
|---|
| 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);
|
|---|
| 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);
|
|---|
| 210 | memcpy(&this->range, buffer+46, 4);
|
|---|
| 211 |
|
|---|
| 212 | this->name.assign(buffer+50);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | bool Player::move(WorldMap *map) {
|
|---|
| 216 | int speed = 100; // pixels per second. should probably be in the constructor
|
|---|
| 217 | unsigned long long curTime = getCurrentMillis();
|
|---|
| 218 |
|
|---|
| 219 | // if we're at our target, don't move
|
|---|
| 220 | bool moving = (pos.x != target.x || pos.y != target.y);
|
|---|
| 221 |
|
|---|
| 222 | if (moving) {
|
|---|
| 223 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
|---|
| 224 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
|---|
| 225 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
|---|
| 226 |
|
|---|
| 227 | if (dist <= pixels) {
|
|---|
| 228 | pos.x = target.x;
|
|---|
| 229 | pos.y = target.y;
|
|---|
| 230 | }else {
|
|---|
| 231 | pos.x = pos.x + cos(angle)*pixels;
|
|---|
| 232 | pos.y = pos.y + sin(angle)*pixels;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | timeLastUpdated = curTime;
|
|---|
| 237 |
|
|---|
| 238 | return moving;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 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 |
|
|---|
| 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;
|
|---|
| 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();
|
|---|
| 268 |
|
|---|
| 269 | return true;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | return false;
|
|---|
| 274 | }
|
|---|