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