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