[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;
|
---|
[d436ac4] | 18 | this->team = 0; // blue team by default
|
---|
| 19 | this->hasBlueFlag = false;
|
---|
| 20 | this->hasRedFlag = false;
|
---|
[01d0d00] | 21 | }
|
---|
| 22 |
|
---|
| 23 | Player::Player(const Player& p)
|
---|
| 24 | {
|
---|
| 25 | this->id = p.id;
|
---|
| 26 | this->name = p.name;
|
---|
| 27 | this->password = p.password;
|
---|
| 28 | this->pos.x = p.pos.x;
|
---|
| 29 | this->pos.y = p.pos.y;
|
---|
[60017fc] | 30 | this->target.x = p.target.x;
|
---|
| 31 | this->target.y = p.target.y;
|
---|
[01d0d00] | 32 | this->addr = p.addr;
|
---|
[d436ac4] | 33 | this->team = 0; // blue team by default
|
---|
| 34 | this->hasBlueFlag = false;
|
---|
| 35 | this->hasRedFlag = false;
|
---|
[01d0d00] | 36 | }
|
---|
| 37 |
|
---|
[59061f6] | 38 | Player::Player(string name, string password)
|
---|
| 39 | {
|
---|
[01d0d00] | 40 | this->id = 0;
|
---|
[59061f6] | 41 | this->name = name;
|
---|
| 42 | this->password = password;
|
---|
[60017fc] | 43 | this->pos.x = this->target.x = 200;
|
---|
| 44 | this->pos.y = this->target.y = 200;
|
---|
[d436ac4] | 45 | this->team = 0; // blue team by default
|
---|
[e4a5786] | 46 | this->hasBlueFlag = false;
|
---|
| 47 | this->hasRedFlag = false;
|
---|
[2488852] | 48 | }
|
---|
| 49 |
|
---|
[8e540f4] | 50 | Player::~Player()
|
---|
[2488852] | 51 | {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[3b8adee] | 54 | void Player::serialize(char* buffer)
|
---|
[59061f6] | 55 | {
|
---|
[80b3f94] | 56 | memcpy(buffer, &this->id, 4);
|
---|
[8f85180] | 57 | memcpy(buffer+4, &this->pos.x, 4);
|
---|
| 58 | memcpy(buffer+8, &this->pos.y, 4);
|
---|
| 59 | memcpy(buffer+12, &this->target.x, 4);
|
---|
| 60 | memcpy(buffer+16, &this->target.y, 4);
|
---|
[d436ac4] | 61 | memcpy(buffer+20, &this->team, 4);
|
---|
| 62 | memcpy(buffer+24, &this->hasBlueFlag, 1);
|
---|
| 63 | memcpy(buffer+25, &this->hasRedFlag, 1);
|
---|
| 64 | strcpy(buffer+26, this->name.c_str());
|
---|
[59061f6] | 65 | }
|
---|
[edfd1d0] | 66 |
|
---|
[3b8adee] | 67 | void Player::deserialize(char* buffer)
|
---|
[edfd1d0] | 68 | {
|
---|
[80b3f94] | 69 | memcpy(&this->id, buffer, 4);
|
---|
[8f85180] | 70 | memcpy(&this->pos.x, buffer+4, 4);
|
---|
| 71 | memcpy(&this->pos.y, buffer+8, 4);
|
---|
| 72 | memcpy(&this->target.x, buffer+12, 4);
|
---|
| 73 | memcpy(&this->target.y, buffer+16, 4);
|
---|
[d436ac4] | 74 | memcpy(&this->team, buffer+20, 4);
|
---|
| 75 | memcpy(&this->hasBlueFlag, buffer+24, 1);
|
---|
| 76 | memcpy(&this->hasRedFlag, buffer+25, 1);
|
---|
| 77 | this->name.assign(buffer+26);
|
---|
[3b8adee] | 78 | }
|
---|
| 79 |
|
---|
[01d0d00] | 80 | void Player::setId(int id)
|
---|
| 81 | {
|
---|
| 82 | this->id = id;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3b8adee] | 85 | void Player::setAddr(sockaddr_in addr)
|
---|
| 86 | {
|
---|
| 87 | this->addr = addr;
|
---|
[edfd1d0] | 88 | }
|
---|
[60017fc] | 89 |
|
---|
[227baaa] | 90 | bool Player::move(WorldMap *map) {
|
---|
[60017fc] | 91 | int speed = 100; // pixels per second
|
---|
[8f85180] | 92 | unsigned long long curTime = getCurrentMillis();
|
---|
[a1a3bd5] | 93 | bool moveCanceled = false;
|
---|
[60017fc] | 94 |
|
---|
[f401cac] | 95 | // if we're at our target, don't move
|
---|
[60940f8] | 96 | if (pos.x != target.x || pos.y != target.y) {
|
---|
[8f85180] | 97 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
[f401cac] | 98 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
---|
[8f85180] | 99 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
---|
[db58227] | 100 | FLOAT_POSITION newPos;
|
---|
[a1a3bd5] | 101 |
|
---|
[8f85180] | 102 | if (dist <= pixels) {
|
---|
[d211210] | 103 | newPos.x = target.x;
|
---|
| 104 | newPos.y = target.y;
|
---|
[8f85180] | 105 | }else {
|
---|
[7f2cef0] | 106 | newPos.x = pos.x + cos(angle)*pixels;
|
---|
| 107 | newPos.y = pos.y + sin(angle)*pixels;
|
---|
[d211210] | 108 | }
|
---|
[a1a3bd5] | 109 |
|
---|
[d211210] | 110 | switch(map->getElement(newPos.x/25, newPos.y/25)) {
|
---|
| 111 | case WorldMap::TERRAIN_NONE:
|
---|
[e76055f] | 112 | case WorldMap::TERRAIN_OCEAN:
|
---|
[d211210] | 113 | case WorldMap::TERRAIN_ROCK:
|
---|
| 114 | target.x = pos.x;
|
---|
| 115 | target.y = pos.y;
|
---|
| 116 | moveCanceled = true;
|
---|
| 117 | break;
|
---|
| 118 | default: // if there are no obstacles
|
---|
| 119 | pos.x = newPos.x;
|
---|
| 120 | pos.y = newPos.y;
|
---|
| 121 | break;
|
---|
[8f85180] | 122 | }
|
---|
[e76055f] | 123 |
|
---|
| 124 | // using moveCanceled in a hacky way just to indicate that the server
|
---|
| 125 | // has updated some player info. Should change the variable name
|
---|
[05051c7] | 126 | switch(map->getStructure(newPos.x/25, newPos.y/25)) {
|
---|
| 127 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
[e76055f] | 128 | hasBlueFlag = true;
|
---|
| 129 | moveCanceled = true;
|
---|
| 130 | break;
|
---|
[05051c7] | 131 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
[e76055f] | 132 | hasRedFlag = true;
|
---|
| 133 | moveCanceled = true;
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
[f401cac] | 136 | }
|
---|
[60017fc] | 137 |
|
---|
[8f85180] | 138 | timeLastUpdated = curTime;
|
---|
[d211210] | 139 |
|
---|
[a1a3bd5] | 140 | return !moveCanceled;
|
---|
[60017fc] | 141 | }
|
---|