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
Line 
1#include "Player.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstring>
6#include <cmath>
7
8#include <allegro5/allegro.h>
9#include <allegro5/allegro_primitives.h>
10
11using namespace std;
12
13Player::Player()
14{
15 this->id = 0;
16 this->name = "";
17 this->password = "";
18 this->pos.x = this->target.x = 0;
19 this->pos.y = this->target.y = 0;
20 this->timeLastUpdated = 0;
21 this->team = 0; // blue team by default
22 this->hasBlueFlag = false;
23 this->hasRedFlag = false;
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;
33 this->target.x = p.target.x;
34 this->target.y = p.target.y;
35 this->addr = p.addr;
36 this->team = 0; // blue team by default
37 this->hasBlueFlag = false;
38 this->hasRedFlag = false;
39}
40
41Player::Player(string name, string password)
42{
43 this->id = 0;
44 this->name = name;
45 this->password = password;
46 this->pos.x = this->target.x = 200;
47 this->pos.y = this->target.y = 200;
48 this->team = 0; // blue team by default
49 this->hasBlueFlag = true;
50 this->hasRedFlag = true;
51}
52
53Player::~Player()
54{
55}
56
57void Player::serialize(char* buffer)
58{
59 memcpy(buffer, &this->id, 4);
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);
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());
68}
69
70void Player::deserialize(char* buffer)
71{
72 memcpy(&this->id, buffer, 4);
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);
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);
81}
82
83void Player::setId(int id)
84{
85 this->id = id;
86}
87
88void Player::setAddr(sockaddr_in addr)
89{
90 this->addr = addr;
91}
92
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
105bool Player::move(WorldMap *map) {
106 int speed = 100; // pixels per second
107 unsigned long long curTime = getCurrentMillis();
108 bool moveCanceled = false;
109
110 // if we're at our target, don't move
111 if (pos.x != target.x || pos.y != target.y) {
112 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
113 double angle = atan2(target.y-pos.y, target.x-pos.x);
114 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
115 FLOAT_POSITION newPos;
116
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
122 if (dist <= pixels) {
123 newPos.x = target.x;
124 newPos.y = target.y;
125 }else {
126 newPos.x = pos.x + cos(angle)*pixels;
127 newPos.y = pos.y + sin(angle)*pixels;
128 }
129
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;
148 }
149 }
150
151 timeLastUpdated = curTime;
152
153 if (moveCanceled)
154 cout << "moveCancled == true" << endl;
155
156 return !moveCanceled;
157}
Note: See TracBrowser for help on using the repository browser.