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

Last change on this file since ce2bb87 was e6c26b8, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

The client dynamically allocates memory for players and passes around a map with player pointers and some includes are now in individual files instead of in Common.h

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