source: network-game/common/Player.cpp@ d436ac4

Last change on this file since d436ac4 was d436ac4, checked in by dportnoy <dmp1488@…>, 12 years ago

Modified the player class to include the team and whether the player has either of the flags

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[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]11using namespace std;
12
[01d0d00]13Player::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
26Player::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]41Player::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]53Player::~Player()
[2488852]54{
55}
56
[3b8adee]57void 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]70void 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]83void Player::setId(int id)
84{
85 this->id = id;
86}
87
[3b8adee]88void Player::setAddr(sockaddr_in addr)
89{
90 this->addr = addr;
[edfd1d0]91}
[60017fc]92
[d436ac4]93void Player::draw(POSITION pos, bool curPlayer) {
94 if (curPlayer)
95 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0));
96 else
97 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
98
99 if (this->hasBlueFlag)
100 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
101 else if(this->hasRedFlag)
102 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
103}
104
[227baaa]105bool Player::move(WorldMap *map) {
[60017fc]106 int speed = 100; // pixels per second
[8f85180]107 unsigned long long curTime = getCurrentMillis();
[a1a3bd5]108 bool moveCanceled = false;
[60017fc]109
[f401cac]110 // if we're at our target, don't move
[60940f8]111 if (pos.x != target.x || pos.y != target.y) {
[8f85180]112 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
[f401cac]113 double angle = atan2(target.y-pos.y, target.x-pos.x);
[8f85180]114 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
[db58227]115 FLOAT_POSITION newPos;
[a1a3bd5]116
[d211210]117 //cout << "pos.x: " << pos.x << endl;
118 //cout << "pos.y: " << pos.y << endl;
119 //cout << "target.x: " << target.x << endl;
120 //cout << "target.y: " << target.y << endl;
121
[8f85180]122 if (dist <= pixels) {
[d211210]123 newPos.x = target.x;
124 newPos.y = target.y;
[8f85180]125 }else {
[7f2cef0]126 newPos.x = pos.x + cos(angle)*pixels;
127 newPos.y = pos.y + sin(angle)*pixels;
[d211210]128 }
[a1a3bd5]129
[d211210]130 //cout << "newPos.x: " << newPos.x << endl;
131 //cout << "newPos.y: " << newPos.y << endl;
132 //cout << "newPos.x/25: " << newPos.x/25 << endl;
133 //cout << "newPos.y/25: " << newPos.y/25 << endl;
134
135 switch(map->getElement(newPos.x/25, newPos.y/25)) {
136 case WorldMap::TERRAIN_NONE:
137 case WorldMap::TERRAIN_ROCK:
138 cout << "Encountered invalid terrain" << endl;
139 target.x = pos.x;
140 target.y = pos.y;
141 moveCanceled = true;
142 cout << "move canceled" << endl;
143 break;
144 default: // if there are no obstacles
145 pos.x = newPos.x;
146 pos.y = newPos.y;
147 break;
[8f85180]148 }
[f401cac]149 }
[60017fc]150
[8f85180]151 timeLastUpdated = curTime;
[d211210]152
153 if (moveCanceled)
154 cout << "moveCancled == true" << endl;
155
[a1a3bd5]156 return !moveCanceled;
[60017fc]157}
Note: See TracBrowser for help on using the repository browser.