[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;
|
---|
[d436ac4] | 32 | this->team = 0; // blue team by default
|
---|
| 33 | this->hasBlueFlag = false;
|
---|
| 34 | this->hasRedFlag = false;
|
---|
[f41a7f9] | 35 |
|
---|
| 36 | this->currentGame = NULL;
|
---|
[01d0d00] | 37 | }
|
---|
| 38 |
|
---|
| 39 | Player::Player(const Player& p)
|
---|
| 40 | {
|
---|
| 41 | this->id = p.id;
|
---|
| 42 | this->name = p.name;
|
---|
| 43 | this->password = p.password;
|
---|
[07c73fa] | 44 | this->addr = p.addr;
|
---|
[01d0d00] | 45 | this->pos.x = p.pos.x;
|
---|
| 46 | this->pos.y = p.pos.y;
|
---|
[60017fc] | 47 | this->target.x = p.target.x;
|
---|
| 48 | this->target.y = p.target.y;
|
---|
[5b92307] | 49 | this->targetPlayer = p.targetPlayer;
|
---|
[07c73fa] | 50 | this->timeLastUpdated = p.timeLastUpdated;
|
---|
[8dad966] | 51 | this->timeAttackStarted = p.timeAttackStarted;
|
---|
[c76134b] | 52 | this->timeDied = p.timeDied;
|
---|
[11d21ee] | 53 | this->isChasing = p.isChasing;
|
---|
[8dad966] | 54 | this->isAttacking = p.isAttacking;
|
---|
[c76134b] | 55 | this->isDead = p.isDead;
|
---|
[07c73fa] | 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;
|
---|
[11d21ee] | 62 | this->range = p.range;
|
---|
[8dad966] | 63 | this->attackCooldown = p.attackCooldown;
|
---|
[df79cfd] | 64 | this->team = p.team;
|
---|
| 65 | this->hasBlueFlag = p.hasBlueFlag;
|
---|
| 66 | this->hasRedFlag = p.hasRedFlag;
|
---|
[f41a7f9] | 67 |
|
---|
| 68 | this->currentGame = p.currentGame;
|
---|
[01d0d00] | 69 | }
|
---|
| 70 |
|
---|
[07c73fa] | 71 | // eventually make this take a PlayerClass argument as well
|
---|
[59061f6] | 72 | Player::Player(string name, string password)
|
---|
| 73 | {
|
---|
[01d0d00] | 74 | this->id = 0;
|
---|
[59061f6] | 75 | this->name = name;
|
---|
| 76 | this->password = password;
|
---|
[60017fc] | 77 | this->pos.x = this->target.x = 200;
|
---|
| 78 | this->pos.y = this->target.y = 200;
|
---|
[5b92307] | 79 | this->targetPlayer = 0;
|
---|
[8dad966] | 80 | this->timeLastUpdated = 0;
|
---|
| 81 | this->timeAttackStarted = 0;
|
---|
[c76134b] | 82 | this->timeDied = 0;
|
---|
[11d21ee] | 83 | this->isChasing = false;
|
---|
[8dad966] | 84 | this->isAttacking = false;
|
---|
[c76134b] | 85 | this->isDead = false;
|
---|
[07c73fa] | 86 |
|
---|
| 87 | this->playerClass = CLASS_NONE;
|
---|
| 88 | this->maxHealth = 0;
|
---|
| 89 | this->health = 0;
|
---|
| 90 | this->attackType = ATTACK_NONE;
|
---|
| 91 | this->damage = 0;
|
---|
[11d21ee] | 92 | this->range = 0;
|
---|
[8dad966] | 93 | this->attackCooldown = 0;
|
---|
[d436ac4] | 94 | this->team = 0; // blue team by default
|
---|
[e4a5786] | 95 | this->hasBlueFlag = false;
|
---|
| 96 | this->hasRedFlag = false;
|
---|
[f41a7f9] | 97 |
|
---|
| 98 | this->currentGame = NULL;
|
---|
[2488852] | 99 | }
|
---|
| 100 |
|
---|
[8e540f4] | 101 | Player::~Player()
|
---|
[2488852] | 102 | {
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[5b92307] | 105 | unsigned int Player::getId()
|
---|
| 106 | {
|
---|
| 107 | return this->id;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | unsigned int Player::getTargetPlayer()
|
---|
| 111 | {
|
---|
| 112 | return this->targetPlayer;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[9ba9b96] | 115 | void Player::setId(unsigned int id)
|
---|
[46fa35a] | 116 | {
|
---|
| 117 | this->id = id;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[5b92307] | 120 | void Player::setTargetPlayer(unsigned int id)
|
---|
| 121 | {
|
---|
| 122 | this->targetPlayer = id;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[46fa35a] | 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;
|
---|
[8dad966] | 135 | this->maxHealth = this->health = 120;
|
---|
[46fa35a] | 136 | this->attackType = ATTACK_MELEE;
|
---|
| 137 | this->damage = 10;
|
---|
[11d21ee] | 138 | this->range = 30;
|
---|
[8dad966] | 139 | this->attackCooldown = 800;
|
---|
[46fa35a] | 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;
|
---|
[11d21ee] | 146 | this->range = 100;
|
---|
[8dad966] | 147 | this->attackCooldown = 1000;
|
---|
[46fa35a] | 148 | break;
|
---|
| 149 | case CLASS_NONE:
|
---|
[521c88b] | 150 | cout << "No class" << endl;
|
---|
[46fa35a] | 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[3b8adee] | 155 | void Player::serialize(char* buffer)
|
---|
[59061f6] | 156 | {
|
---|
[80b3f94] | 157 | memcpy(buffer, &this->id, 4);
|
---|
[8f85180] | 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);
|
---|
[07c73fa] | 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);
|
---|
[5b1e31e] | 171 | memcpy(buffer+46, &this->range, 4);
|
---|
[07c73fa] | 172 |
|
---|
[88c0536] | 173 | strcpy(buffer+50, this->name.c_str());
|
---|
[59061f6] | 174 | }
|
---|
[edfd1d0] | 175 |
|
---|
[3b8adee] | 176 | void Player::deserialize(char* buffer)
|
---|
[edfd1d0] | 177 | {
|
---|
[80b3f94] | 178 | memcpy(&this->id, buffer, 4);
|
---|
[8f85180] | 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);
|
---|
[07c73fa] | 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);
|
---|
[5b1e31e] | 192 | memcpy(&this->range, buffer+46, 4);
|
---|
[07c73fa] | 193 |
|
---|
[88c0536] | 194 | this->name.assign(buffer+50);
|
---|
[3b8adee] | 195 | }
|
---|
| 196 |
|
---|
[227baaa] | 197 | bool Player::move(WorldMap *map) {
|
---|
[23559e7] | 198 | int speed = 100; // pixels per second. should probably be in the constructor
|
---|
[8f85180] | 199 | unsigned long long curTime = getCurrentMillis();
|
---|
[60017fc] | 200 |
|
---|
[f401cac] | 201 | // if we're at our target, don't move
|
---|
[23559e7] | 202 | bool moving = (pos.x != target.x || pos.y != target.y);
|
---|
| 203 |
|
---|
| 204 | if (moving) {
|
---|
[8f85180] | 205 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
[f401cac] | 206 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
[8f85180] | 207 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
---|
[a1a3bd5] | 208 |
|
---|
[8f85180] | 209 | if (dist <= pixels) {
|
---|
[b81cea1] | 210 | pos.x = target.x;
|
---|
| 211 | pos.y = target.y;
|
---|
[8f85180] | 212 | }else {
|
---|
[b81cea1] | 213 | pos.x = pos.x + cos(angle)*pixels;
|
---|
| 214 | pos.y = pos.y + sin(angle)*pixels;
|
---|
[d211210] | 215 | }
|
---|
[f401cac] | 216 | }
|
---|
[60017fc] | 217 |
|
---|
[8f85180] | 218 | timeLastUpdated = curTime;
|
---|
[d211210] | 219 |
|
---|
[23559e7] | 220 | return moving;
|
---|
[60017fc] | 221 | }
|
---|
[ff2133a] | 222 |
|
---|
[6054f1e] | 223 | void 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 |
|
---|
[5b92307] | 234 | bool 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;
|
---|
[ff2133a] | 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();
|
---|
[5b1e31e] | 250 |
|
---|
| 251 | return true;
|
---|
[ff2133a] | 252 | }
|
---|
| 253 | }
|
---|
[5b1e31e] | 254 |
|
---|
| 255 | return false;
|
---|
[ff2133a] | 256 | }
|
---|