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

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

Added class, health, attackType, and damage to the Player class

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