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