1 | #include "Projectile.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 | #include <cstring>
|
---|
6 | #include <cmath>
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | Projectile::Projectile()
|
---|
11 | {
|
---|
12 | this->id = 0;
|
---|
13 | this->pos.x = 0;
|
---|
14 | this->pos.y = 0;
|
---|
15 | this->target = 0;
|
---|
16 | this->speed = 0;
|
---|
17 | this->damage = 0;
|
---|
18 | this->timeLastUpdated = 0;
|
---|
19 | }
|
---|
20 |
|
---|
21 | Projectile::Projectile(const Projectile& p)
|
---|
22 | {
|
---|
23 | this->id = p.id;
|
---|
24 | this->pos.x = p.pos.x;
|
---|
25 | this->pos.y = p.pos.y;
|
---|
26 | this->target = p.target;
|
---|
27 | this->speed = p.speed;
|
---|
28 | this->damage = p.damage;
|
---|
29 | this->timeLastUpdated = p.timeLastUpdated;
|
---|
30 | }
|
---|
31 |
|
---|
32 | Projectile::Projectile(int x, int y, int targetId, int damage)
|
---|
33 | {
|
---|
34 | this->id = 0; // the server probably sets this by calling setId passing in the length of the current projectile list
|
---|
35 | this->pos.x = x;
|
---|
36 | this->pos.y = y;
|
---|
37 | this->target = targetId;
|
---|
38 | this->speed = 400;
|
---|
39 | this->damage = damage;
|
---|
40 | this->timeLastUpdated = 0;
|
---|
41 | }
|
---|
42 |
|
---|
43 | Projectile::~Projectile()
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | void Projectile::setId(int id)
|
---|
48 | {
|
---|
49 | this->id = id;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void Projectile::serialize(char* buffer)
|
---|
53 | {
|
---|
54 | memcpy(buffer, &this->id, 4);
|
---|
55 | memcpy(buffer+4, &this->pos.x, 4);
|
---|
56 | memcpy(buffer+8, &this->pos.y, 4);
|
---|
57 | memcpy(buffer+12, &this->target, 4);
|
---|
58 | memcpy(buffer+16, &this->speed, 4);
|
---|
59 | memcpy(buffer+20, &this->damage, 4);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void Projectile::deserialize(char* buffer)
|
---|
63 | {
|
---|
64 | memcpy(&this->id, buffer, 4);
|
---|
65 | memcpy(&this->pos.x, buffer+4, 4);
|
---|
66 | memcpy(&this->pos.y, buffer+8, 4);
|
---|
67 | memcpy(&this->target, buffer+12, 4);
|
---|
68 | memcpy(&this->speed, buffer+16, 4);
|
---|
69 | memcpy(buffer+16, &this->speed, 4);
|
---|
70 | memcpy(buffer+20, &this->damage, 4);
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool Projectile::move(map<unsigned int, Player>& mapPlayers) {
|
---|
74 | // if the current target logs off, this method will run into problems
|
---|
75 |
|
---|
76 | unsigned long long curTime = getCurrentMillis();
|
---|
77 |
|
---|
78 | Player targetP = mapPlayers[target];
|
---|
79 |
|
---|
80 | if (timeLastUpdated == 0) {
|
---|
81 | timeLastUpdated = curTime;
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
|
---|
87 | double angle = atan2(targetP.pos.y-pos.y, targetP.pos.x-pos.x);
|
---|
88 | float dist = sqrt(pow(targetP.pos.x-pos.x, 2) + pow(targetP.pos.y-pos.y, 2));
|
---|
89 |
|
---|
90 | if (dist <= pixels) {
|
---|
91 | pos.x = targetP.pos.x;
|
---|
92 | pos.y = targetP.pos.y;
|
---|
93 | return true;
|
---|
94 | }else {
|
---|
95 | pos.x = pos.x + cos(angle)*pixels;
|
---|
96 | pos.y = pos.y + sin(angle)*pixels;
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 | }
|
---|