source: network-game/common/Player.cpp@ 7ca5d21

Last change on this file since 7ca5d21 was 521c88b, checked in by dportnoy <dmp1488@…>, 11 years ago

Upon player registration, the server stores the selected player class and no longer generates a random player class every time the player logs in

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