source: network-game/common/Player.cpp@ 032e550

Last change on this file since 032e550 was 032e550, checked in by dportnoy <dmp1488@…>, 12 years ago

Add some more debug info

  • 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;
[11d21ee]19 this->isChasing = false;
[8dad966]20 this->isAttacking = false;
[07c73fa]21
22 this->playerClass = CLASS_NONE;
23 this->maxHealth = 0;
24 this->health = 0;
25 this->attackType = ATTACK_NONE;
26 this->damage = 0;
[11d21ee]27 this->range = 0;
[8dad966]28 this->attackCooldown = 0;
[d436ac4]29 this->team = 0; // blue team by default
30 this->hasBlueFlag = false;
31 this->hasRedFlag = false;
[01d0d00]32}
33
34Player::Player(const Player& p)
35{
36 this->id = p.id;
37 this->name = p.name;
38 this->password = p.password;
[07c73fa]39 this->addr = p.addr;
[01d0d00]40 this->pos.x = p.pos.x;
41 this->pos.y = p.pos.y;
[60017fc]42 this->target.x = p.target.x;
43 this->target.y = p.target.y;
[07c73fa]44 this->timeLastUpdated = p.timeLastUpdated;
[8dad966]45 this->timeAttackStarted = p.timeAttackStarted;
[11d21ee]46 this->isChasing = p.isChasing;
[8dad966]47 this->isAttacking = p.isAttacking;
[07c73fa]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;
[11d21ee]54 this->range = p.range;
[8dad966]55 this->attackCooldown = p.attackCooldown;
[df79cfd]56 this->team = p.team;
57 this->hasBlueFlag = p.hasBlueFlag;
58 this->hasRedFlag = p.hasRedFlag;
[01d0d00]59}
60
[07c73fa]61// eventually make this take a PlayerClass argument as well
[59061f6]62Player::Player(string name, string password)
63{
[01d0d00]64 this->id = 0;
[59061f6]65 this->name = name;
66 this->password = password;
[60017fc]67 this->pos.x = this->target.x = 200;
68 this->pos.y = this->target.y = 200;
[8dad966]69 this->timeLastUpdated = 0;
70 this->timeAttackStarted = 0;
[11d21ee]71 this->isChasing = false;
[8dad966]72 this->isAttacking = false;
[07c73fa]73
74 this->playerClass = CLASS_NONE;
75 this->maxHealth = 0;
76 this->health = 0;
77 this->attackType = ATTACK_NONE;
78 this->damage = 0;
[11d21ee]79 this->range = 0;
[8dad966]80 this->attackCooldown = 0;
[d436ac4]81 this->team = 0; // blue team by default
[e4a5786]82 this->hasBlueFlag = false;
83 this->hasRedFlag = false;
[2488852]84}
85
[8e540f4]86Player::~Player()
[2488852]87{
88}
89
[46fa35a]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;
[8dad966]105 this->maxHealth = this->health = 120;
[46fa35a]106 this->attackType = ATTACK_MELEE;
107 this->damage = 10;
[11d21ee]108 this->range = 30;
[8dad966]109 this->attackCooldown = 800;
[46fa35a]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;
[11d21ee]116 this->range = 100;
[8dad966]117 this->attackCooldown = 1000;
[46fa35a]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
[3b8adee]128void Player::serialize(char* buffer)
[59061f6]129{
[80b3f94]130 memcpy(buffer, &this->id, 4);
[8f85180]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);
[07c73fa]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);
[5b1e31e]144 memcpy(buffer+46, &this->range, 4);
[07c73fa]145
146 strcpy(buffer+46, this->name.c_str());
[59061f6]147}
[edfd1d0]148
[3b8adee]149void Player::deserialize(char* buffer)
[edfd1d0]150{
[80b3f94]151 memcpy(&this->id, buffer, 4);
[8f85180]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);
[07c73fa]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);
[5b1e31e]165 memcpy(&this->range, buffer+46, 4);
[07c73fa]166
167 this->name.assign(buffer+46);
[3b8adee]168}
169
[227baaa]170bool Player::move(WorldMap *map) {
[23559e7]171 int speed = 100; // pixels per second. should probably be in the constructor
[8f85180]172 unsigned long long curTime = getCurrentMillis();
[60017fc]173
[f401cac]174 // if we're at our target, don't move
[23559e7]175 bool moving = (pos.x != target.x || pos.y != target.y);
176
177 if (moving) {
[8f85180]178 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
[f401cac]179 double angle = atan2(target.y-pos.y, target.x-pos.x);
[8f85180]180 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
[a1a3bd5]181
[8f85180]182 if (dist <= pixels) {
[b81cea1]183 pos.x = target.x;
184 pos.y = target.y;
[8f85180]185 }else {
[b81cea1]186 pos.x = pos.x + cos(angle)*pixels;
187 pos.y = pos.y + sin(angle)*pixels;
[d211210]188 }
[f401cac]189 }
[60017fc]190
[8f85180]191 timeLastUpdated = curTime;
[d211210]192
[23559e7]193 return moving;
[60017fc]194}
[ff2133a]195
[5b1e31e]196bool Player::updateTarget(map<unsigned int, Player>& mapPlayers) {
[ff2133a]197 if (this->isChasing) {
198 this->target.x = mapPlayers[this->targetPlayer].pos.x;
199 this->target.y = mapPlayers[this->targetPlayer].pos.y;
200
[032e550]201 cout << "Setting target" << endl;
202 cout << "target id: " << mapPlayers[this->targetPlayer].id << endl;
203 cout << "cur player id: " << this->id << endl;
204
[ff2133a]205 if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
[032e550]206 cout << "Stopped chasing" << endl;
207
[ff2133a]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();
[5b1e31e]214
215 return true;
[ff2133a]216 }
217 }
[5b1e31e]218
219 return false;
[ff2133a]220}
Note: See TracBrowser for help on using the repository browser.