-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.cpp
131 lines (112 loc) · 3.62 KB
/
Utility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <utility>
#include <vector>
#include <string>
#include <memory>
#include <set>
#include "Utility.hpp"
#include "Enemy.hpp"
class Entity;
class Player;
class Projectile;
namespace util {
Point::Point() : x(0), y(0) {}
Point::Point(int xCoord, int yCoord) : x(xCoord), y(yCoord) {}
Point::Point(std::pair<int, int> coords) : x(coords.first), y(coords.second) {}
Point::Point(const Point& src) : x(src.x), y(src.y) {}
Point Point::operator+(const Point& lhs) const {
return Point(this->x + lhs.x, this->y + lhs.y);
}
Point& Point::operator+=(const Point& lhs) {
x += lhs.x;
y += lhs.y;
return *this;
}
Point Point::operator-(const Point& lhs) const {
return Point(this->x - lhs.x, this->y - lhs.y);
}
Point& Point::operator-=(const Point& lhs) {
x -= lhs.x;
y -= lhs.y;
return *this;
}
Point Point::operator*(int lhs) const {
return Point(this->x * lhs, this->y * lhs);
}
Point& Point::operator*=(int lhs) {
x *= lhs;
y *= lhs;
return *this;
}
Point Point::operator/(int lhs) const {
return Point(this->x / lhs, this->y / lhs);
}
Point& Point::operator/=(int lhs) {
x /= lhs;
y /= lhs;
return *this;
}
bool Point::operator==(const Point& lhs) const {
return this->x == lhs.x && this->y == lhs.y;
}
bool Point::operator!=(const Point& lhs) const {
return !((*this) == lhs);
}
Point& Point::randomize(const std::vector<std::string>& map) {
const Point d[] = { {0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
size_t randIndx = rand() % 5;
while (!checkPoint(map, *this + d[randIndx])) {
randIndx = rand() % 5;
}
return (*this += d[randIndx]);
}
Point Point::Right() { return Point(1, 0); }
Point Point::Left() { return Point(-1, 0); }
Point Point::Up() { return Point(0, -1); }
Point Point::Down() { return Point(0, 1); }
Point Point::URight() { return Point(1, -1); }
Point Point::ULeft() { return Point(-1, -1); }
Point Point::DRight() { return Point(1, 1); }
Point Point::DLeft() { return Point(-1, 1); }
bool operator<(const Point& rhs, const Point& lhs) {
if (lhs.x == rhs.x)
return rhs.y < lhs.y;
return rhs.x < lhs.x;
}
int distance(const Point& lhs, const Point& rhs) {
return std::abs(lhs.x - rhs.x) + std::abs(lhs.y - rhs.y);
}
bool checkPoint(const std::vector<std::string>& map, const Point& p) {
return p.x >= 0 && p.y >= 0 && p.y < static_cast<int>(map.size()) && p.x < static_cast<int>(map[0].size());
}
void GameInfo::updateAll() {
for (size_t i = 0; i < this->projectiles.size(); i++) {
if (this->projectiles[i]->isEnabled()) {
this->projectiles[i]->update(*this);
}
}
for (size_t i = 0; i < this->projectiles.size(); i++) {
if (this->projectiles[i]->isEnabled())
break;
this->projectiles.pop_front();
i--;
}
for (int i = this->entities.size() - 1; i >= 0; i--) {
if (this->entities[i]->isEnabled()) {
this->entities[i]->update(*this);
}
}
}
int GameInfo::getNextId() {
this->curId++;
return this->curId - 1;
}
char& GameInfo::operator[](const Point& pos) {
return map[pos.y][pos.x];
}
void GameInfo::reset() {
this->isWin = false;
this->map.clear();
this->items.clear();
this->projectiles.clear();
}
}