-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonster.cpp
More file actions
128 lines (106 loc) · 4.12 KB
/
Copy pathMonster.cpp
File metadata and controls
128 lines (106 loc) · 4.12 KB
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
//
// Created by danil on 26.11.2020.
//
#include "Monster.h"
#include <cmath>
double Monster::calcDistance(Point target, Point point) {
double x_squared = (target.x - point.x) * (target.x - point.x);
double y_squared = (target.y - point.y) * (target.y - point.y);
double distance = sqrt(x_squared + y_squared);
return distance;
}
void Monster::Monster::move(Point targetPoint, Map map) {
std::map<double, Point> distances;
Point nextPoint;
// right
if(closedMapItem == 'x') { // Âûõîä èç äîìèêà
currentPoint.y -= 2;
return;
}
if ((map.getMapElem(this->currentPoint.x + 1, this->currentPoint.y) != '#') &&
(map.getMapElem(currentPoint.x + 1, currentPoint.y) != 'x') && (this->currentPoint.x + 1 != getPrevPoint().x) &&
(map.getMapElem(currentPoint.x + 1,currentPoint.y) != '-')) {
nextPoint.x = this->currentPoint.x + 1;
nextPoint.y = this->currentPoint.y;
double distance = calcDistance(targetPoint, nextPoint);
distances[distance] = nextPoint;
}
// down
if ((map.getMapElem(this->currentPoint.x, this->currentPoint.y + 1) != '#') &&
(map.getMapElem(currentPoint.x, currentPoint.y + 1) != 'x') && (this->currentPoint.y + 1 != getPrevPoint().y) &&
(map.getMapElem(currentPoint.x,currentPoint.y + 1) != '-')) {
nextPoint.x = this->currentPoint.x;
nextPoint.y = this->currentPoint.y + 1;
double distance = calcDistance(targetPoint, nextPoint);
distances[distance] = nextPoint;
}
// left
if ((map.getMapElem(this->currentPoint.x - 1, this->currentPoint.y) != '#') &&
(map.getMapElem(currentPoint.x - 1, currentPoint.y) != 'x') && (this->currentPoint.x - 1 != getPrevPoint().x) &&
(map.getMapElem(currentPoint.x - 1,currentPoint.y) != '-')) {
nextPoint.x = this->currentPoint.x - 1;
nextPoint.y = this->currentPoint.y;
double distance = calcDistance(targetPoint, nextPoint);
distances[distance] = nextPoint;
}
// up
if ((map.getMapElem(this->currentPoint.x, this->currentPoint.y - 1) != '#') &&
(map.getMapElem(currentPoint.x, currentPoint.y - 1) != 'x') && (this->currentPoint.y - 1 != getPrevPoint().y) &&
(map.getMapElem(currentPoint.x,currentPoint.y - 1) != '-')) {
nextPoint.x = this->currentPoint.x;
nextPoint.y = this->currentPoint.y - 1;
double distance = calcDistance(targetPoint, nextPoint);
distances[distance] = nextPoint;
}
setPrevPoint(this->currentPoint);
this->currentPoint = distances.begin()->second;
}
Point Monster::Monster::getMonsterPos() {
return currentPoint;
}
Point Monster::Monster::getRecessionPoint() {
return recessionPoint;
}
char Monster::Monster::getSprite() {
return sprite;
}
void Monster::Monster::reset() {
currentPoint = startPoint;
prevPoint = {};
closedMapItem = 'x';
windowSprite.setPosition(currentPoint.x*20,currentPoint.y*20);
}
void Monster::Monster::setPrevPoint(Point newPrevPoint) {
prevPoint = newPrevPoint;
}
Point Monster::Monster::getPrevPoint() {
return prevPoint;
}
std::string Monster::Monster::getIndexColor() {
return IndexColor;
}
char Monster::Monster::getClosedMapItem() {
return closedMapItem;
}
void Monster::Monster::setClosedMapItem(Map &map, Point point) {
if (map.getMapElem(point.x, point.y) != ' ' && map.getMapElem(point.x, point.y) != '.' &&
map.getMapElem(point.x, point.y) != 'o' && map.getMapElem(point.x, point.y) != '-'
) {
closedMapItem = ' ';
} else closedMapItem = map.getMapElem(point.x, point.y);
}
bool Monster::Monster::isActive() {
return active;
}
void Monster::Monster::makeActive() {
active = true;
}
sf::Vector2i Monster::Monster::getPosOnWindow() {
return {currentPoint.x*20,currentPoint.y*20};
}
void Monster::Monster::setWindowSprite(sf::Vector2i& point) {
windowSprite.setPosition(point.x,point.y);
}
sf::Sprite Monster::Monster::getSpriteOnWindow() {
return windowSprite;
}