source: network-game/common/Player.cpp@ 8c74150

Last change on this file since 8c74150 was 8dad966, checked in by dportnoy <dmp1488@…>, 11 years ago

Added server support for player attack animations and cooldowns and firing projectiles for ranged attacks

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