| 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 | }
|
|---|
| 19 |
|
|---|
| 20 | Player::Player(const Player& p)
|
|---|
| 21 | {
|
|---|
| 22 | this->id = p.id;
|
|---|
| 23 | this->name = p.name;
|
|---|
| 24 | this->password = p.password;
|
|---|
| 25 | this->pos.x = p.pos.x;
|
|---|
| 26 | this->pos.y = p.pos.y;
|
|---|
| 27 | this->target.x = p.target.x;
|
|---|
| 28 | this->target.y = p.target.y;
|
|---|
| 29 | this->addr = p.addr;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | Player::Player(string name, string password)
|
|---|
| 33 | {
|
|---|
| 34 | this->id = 0;
|
|---|
| 35 | this->name = name;
|
|---|
| 36 | this->password = password;
|
|---|
| 37 | this->pos.x = this->target.x = 200;
|
|---|
| 38 | this->pos.y = this->target.y = 200;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | Player::Player(string name, sockaddr_in addr)
|
|---|
| 42 | {
|
|---|
| 43 | this->id = 0;
|
|---|
| 44 | this->name = name;
|
|---|
| 45 | this->password = "";
|
|---|
| 46 | this->pos.x = this->target.x = 200;
|
|---|
| 47 | this->pos.y = this->target.y = 200;
|
|---|
| 48 | this->addr = addr;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | Player::~Player()
|
|---|
| 52 | {
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void Player::serialize(char* buffer)
|
|---|
| 56 | {
|
|---|
| 57 | memcpy(buffer, &this->id, 4);
|
|---|
| 58 | memcpy(buffer+4, &this->pos.x, 4);
|
|---|
| 59 | memcpy(buffer+8, &this->pos.y, 4);
|
|---|
| 60 | memcpy(buffer+12, &this->target.x, 4);
|
|---|
| 61 | memcpy(buffer+16, &this->target.y, 4);
|
|---|
| 62 | strcpy(buffer+20, this->name.c_str());
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void Player::deserialize(char* buffer)
|
|---|
| 66 | {
|
|---|
| 67 | memcpy(&this->id, buffer, 4);
|
|---|
| 68 | memcpy(&this->pos.x, buffer+4, 4);
|
|---|
| 69 | memcpy(&this->pos.y, buffer+8, 4);
|
|---|
| 70 | memcpy(&this->target.x, buffer+12, 4);
|
|---|
| 71 | memcpy(&this->target.y, buffer+16, 4);
|
|---|
| 72 | this->name.assign(buffer+20);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void Player::setId(int id)
|
|---|
| 76 | {
|
|---|
| 77 | this->id = id;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void Player::setAddr(sockaddr_in addr)
|
|---|
| 81 | {
|
|---|
| 82 | this->addr = addr;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | bool Player::move(WorldMap *map) {
|
|---|
| 86 | int speed = 100; // pixels per second
|
|---|
| 87 | unsigned long long curTime = getCurrentMillis();
|
|---|
| 88 | bool moveCanceled = false;
|
|---|
| 89 |
|
|---|
| 90 | // if we're at our target, don't move
|
|---|
| 91 | if (pos.x != target.x || pos.y != target.y) {
|
|---|
| 92 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
|---|
| 93 | double angle = atan2(target.y-pos.y, target.x-pos.x);
|
|---|
| 94 | float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
|
|---|
| 95 | FLOAT_POSITION newPos;
|
|---|
| 96 |
|
|---|
| 97 | //cout << "pos.x: " << pos.x << endl;
|
|---|
| 98 | //cout << "pos.y: " << pos.y << endl;
|
|---|
| 99 | //cout << "target.x: " << target.x << endl;
|
|---|
| 100 | //cout << "target.y: " << target.y << endl;
|
|---|
| 101 |
|
|---|
| 102 | if (dist <= pixels) {
|
|---|
| 103 | newPos.x = target.x;
|
|---|
| 104 | newPos.y = target.y;
|
|---|
| 105 | }else {
|
|---|
| 106 | newPos.x = int(pos.x + cos(angle)*pixels);
|
|---|
| 107 | newPos.y = int(pos.y + sin(angle)*pixels);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | //cout << "newPos.x: " << newPos.x << endl;
|
|---|
| 111 | //cout << "newPos.y: " << newPos.y << endl;
|
|---|
| 112 | //cout << "newPos.x/25: " << newPos.x/25 << endl;
|
|---|
| 113 | //cout << "newPos.y/25: " << newPos.y/25 << endl;
|
|---|
| 114 |
|
|---|
| 115 | switch(map->getElement(newPos.x/25, newPos.y/25)) {
|
|---|
| 116 | case WorldMap::TERRAIN_NONE:
|
|---|
| 117 | case WorldMap::TERRAIN_ROCK:
|
|---|
| 118 | cout << "Encountered invalid terrain" << endl;
|
|---|
| 119 | target.x = pos.x;
|
|---|
| 120 | target.y = pos.y;
|
|---|
| 121 | moveCanceled = true;
|
|---|
| 122 | cout << "move canceled" << endl;
|
|---|
| 123 | break;
|
|---|
| 124 | default: // if there are no obstacles
|
|---|
| 125 | pos.x = newPos.x;
|
|---|
| 126 | pos.y = newPos.y;
|
|---|
| 127 | break;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | timeLastUpdated = curTime;
|
|---|
| 132 |
|
|---|
| 133 | if (moveCanceled)
|
|---|
| 134 | cout << "moveCancled == true" << endl;
|
|---|
| 135 |
|
|---|
| 136 | return !moveCanceled;
|
|---|
| 137 | }
|
|---|