source: network-game/common/Player.cpp@ 88c0536

Last change on this file since 88c0536 was 88c0536, checked in by dportnoy <dmp1488@…>, 11 years ago

Fixed player serialization bug

  • Property mode set to 100644
File size: 6.1 KB
Line 
1#include "Player.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstring>
6#include <cmath>
7
8using namespace std;
9
10Player::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 this->timeAttackStarted = 0;
19 this->isChasing = false;
20 this->isAttacking = false;
21
22 this->playerClass = CLASS_NONE;
23 this->maxHealth = 0;
24 this->health = 0;
25 this->attackType = ATTACK_NONE;
26 this->damage = 0;
27 this->range = 0;
28 this->attackCooldown = 0;
29 this->team = 0; // blue team by default
30 this->hasBlueFlag = false;
31 this->hasRedFlag = false;
32}
33
34Player::Player(const Player& p)
35{
36 this->id = p.id;
37 this->name = p.name;
38 this->password = p.password;
39 this->addr = p.addr;
40 this->pos.x = p.pos.x;
41 this->pos.y = p.pos.y;
42 this->target.x = p.target.x;
43 this->target.y = p.target.y;
44 this->timeLastUpdated = p.timeLastUpdated;
45 this->timeAttackStarted = p.timeAttackStarted;
46 this->isChasing = p.isChasing;
47 this->isAttacking = p.isAttacking;
48
49 this->playerClass = p.playerClass;
50 this->maxHealth = p.maxHealth;
51 this->health = p.health;
52 this->attackType = p.attackType;
53 this->damage = p.damage;
54 this->range = p.range;
55 this->attackCooldown = p.attackCooldown;
56 this->team = p.team;
57 this->hasBlueFlag = p.hasBlueFlag;
58 this->hasRedFlag = p.hasRedFlag;
59}
60
61// eventually make this take a PlayerClass argument as well
62Player::Player(string name, string password)
63{
64 this->id = 0;
65 this->name = name;
66 this->password = password;
67 this->pos.x = this->target.x = 200;
68 this->pos.y = this->target.y = 200;
69 this->timeLastUpdated = 0;
70 this->timeAttackStarted = 0;
71 this->isChasing = false;
72 this->isAttacking = false;
73
74 this->playerClass = CLASS_NONE;
75 this->maxHealth = 0;
76 this->health = 0;
77 this->attackType = ATTACK_NONE;
78 this->damage = 0;
79 this->range = 0;
80 this->attackCooldown = 0;
81 this->team = 0; // blue team by default
82 this->hasBlueFlag = false;
83 this->hasRedFlag = false;
84}
85
86Player::~Player()
87{
88}
89
90void Player::setId(int id)
91{
92 this->id = id;
93}
94
95void Player::setAddr(sockaddr_in addr)
96{
97 this->addr = addr;
98}
99
100void Player::setClass(PlayerClass c)
101{
102 switch (c) {
103 case CLASS_WARRIOR:
104 this->playerClass = CLASS_WARRIOR;
105 this->maxHealth = this->health = 120;
106 this->attackType = ATTACK_MELEE;
107 this->damage = 10;
108 this->range = 30;
109 this->attackCooldown = 800;
110 break;
111 case CLASS_RANGER:
112 this->playerClass = CLASS_RANGER;
113 this->maxHealth = this->health = 60;
114 this->attackType = ATTACK_RANGED;
115 this->damage = 6;
116 this->range = 100;
117 this->attackCooldown = 1000;
118 break;
119 case CLASS_NONE:
120 cout << "No clas" << endl;
121 break;
122 dafault:
123 cout << "nvalid class" << endl;
124 break;
125 }
126}
127
128void Player::serialize(char* buffer)
129{
130 memcpy(buffer, &this->id, 4);
131 memcpy(buffer+4, &this->pos.x, 4);
132 memcpy(buffer+8, &this->pos.y, 4);
133 memcpy(buffer+12, &this->target.x, 4);
134 memcpy(buffer+16, &this->target.y, 4);
135
136 memcpy(buffer+20, &this->playerClass, 4);
137 memcpy(buffer+24, &this->maxHealth, 4);
138 memcpy(buffer+28, &this->health, 4);
139 memcpy(buffer+32, &this->attackType, 4);
140 memcpy(buffer+36, &this->damage, 4);
141 memcpy(buffer+40, &this->team, 4);
142 memcpy(buffer+44, &this->hasBlueFlag, 1);
143 memcpy(buffer+45, &this->hasRedFlag, 1);
144 memcpy(buffer+46, &this->range, 4);
145
146 strcpy(buffer+50, this->name.c_str());
147}
148
149void Player::deserialize(char* buffer)
150{
151 memcpy(&this->id, buffer, 4);
152 memcpy(&this->pos.x, buffer+4, 4);
153 memcpy(&this->pos.y, buffer+8, 4);
154 memcpy(&this->target.x, buffer+12, 4);
155 memcpy(&this->target.y, buffer+16, 4);
156
157 memcpy(&this->playerClass, buffer+20, 4);
158 memcpy(&this->maxHealth, buffer+24, 4);
159 memcpy(&this->health, buffer+28, 4);
160 memcpy(&this->attackType, buffer+32, 4);
161 memcpy(&this->damage, buffer+36, 4);
162 memcpy(&this->team, buffer+40, 4);
163 memcpy(&this->hasBlueFlag, buffer+44, 1);
164 memcpy(&this->hasRedFlag, buffer+45, 1);
165 memcpy(&this->range, buffer+46, 4);
166
167 this->name.assign(buffer+50);
168}
169
170bool Player::move(WorldMap *map) {
171 int speed = 100; // pixels per second. should probably be in the constructor
172 unsigned long long curTime = getCurrentMillis();
173
174 // if we're at our target, don't move
175 bool moving = (pos.x != target.x || pos.y != target.y);
176
177 if (moving) {
178 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
179 double angle = atan2(target.y-pos.y, target.x-pos.x);
180 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
181
182 if (dist <= pixels) {
183 pos.x = target.x;
184 pos.y = target.y;
185 }else {
186 pos.x = pos.x + cos(angle)*pixels;
187 pos.y = pos.y + sin(angle)*pixels;
188 }
189 }
190
191 timeLastUpdated = curTime;
192
193 return moving;
194}
195
196bool Player::updateTarget(map<unsigned int, Player>& mapPlayers) {
197 if (this->isChasing) {
198 this->target.x = mapPlayers[this->targetPlayer].pos.x;
199 this->target.y = mapPlayers[this->targetPlayer].pos.y;
200
201 cout << "Setting target" << endl;
202 cout << "target id: " << mapPlayers[this->targetPlayer].id << endl;
203 cout << "cur player id: " << this->id << endl;
204 cout << "this->pos.x: " << this->pos.x << endl;
205 cout << "this->pos.x: " << this->pos.x << endl;
206 cout << "this->target.y: " << this->target.y << endl;
207 cout << "this->target.y: " << this->target.y << endl;
208 cout << "this->target.toFloat().x: " << this->target.toFloat().y << endl;
209 cout << "this->target.toFloat().y: " << this->target.toFloat().y << endl;
210 cout << "posDistance: " << posDistance(this->pos, this->target.toFloat()) << endl;
211
212 if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
213 cout << "Stopped chasing" << endl;
214
215 this->target.x = this->pos.x;
216 this->target.y = this->pos.y;
217
218 this->isChasing = false;
219 this->isAttacking = true;
220 this->timeAttackStarted = getCurrentMillis();
221
222 return true;
223 }
224 }
225
226 return false;
227}
Note: See TracBrowser for help on using the repository browser.