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

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

The server randomly chooses a player's class when they log on

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