-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGhost.cpp
131 lines (111 loc) · 3.45 KB
/
Ghost.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 "Ghost.h"
Ghost::Ghost(int tilePosX_, int tilePosY_, int targetX_, int targetY_, float speed_, int number_)
: Entity(tilePosX_ * 16.0f + 8.0f + 8.0f, tilePosY_ * 16.0f + 8.0f, speed_, tilePosX_, tilePosY_) {
targetX = targetX_;
targetY = targetY_;
moving = Direction::Unset;
scattering = true;
outHome = false;
decision = true;
frightened = false;
number = number_;
}
void Ghost::set_target(int x, int y) {
targetX = x;
targetY = y;
}
void Ghost::movement() {
switch (moving) {
case Direction::Up:
move(0, -speed);
break;
case Direction::Down:
move(0, speed);
break;
case Direction::Left:
move(-speed, 0);
break;
case Direction::Right:
move(speed, 0);
break;
}
}
bool Ghost::isScattering() {
return scattering;
}
bool Ghost::isOutHome() {
return outHome;
}
void Ghost::teleport(int x, int y) {
tileX = x;
tileY = y;
screenX = x * 16.0f + 8.0f;
screenY = y * 16.0f + 8.0f;
outHome = true;
}
void Ghost::setFrightened(bool f) {
if (f)
frightened = 2000;
else
frightened = 0;
}
bool Ghost::isFrightened() {
if (frightened > 0) frightened--;
return frightened > 0;
}
bool Ghost::canMove(Labyrinth &labyrinth) {
switch (moving) {
case Direction::Up:
return !labyrinth.tileEntity(tileX, tileY - 1);
case Direction::Down:
return !labyrinth.tileEntity(tileX, tileY + 1);
case Direction::Left:
return !labyrinth.tileEntity(tileX - 1, tileY);
case Direction::Right:
return !labyrinth.tileEntity(tileX + 1, tileY);
default:
return false;
}
}
void Ghost::getSprite(int i) {
if (!isFrightened())
sprite = Resources::get(4 * (number + 1), moving);
else
sprite = Resources::get(Resources::FrightenedGhost, moving);
}
float Ghost::calculateDistance(Labyrinth &labyrinth, int addX, int addY) const {
float distance = 1000000.0f;
if (!labyrinth.tileEntity(tileX + addX, tileY + addY))
distance = static_cast<float>(sqrt(pow((targetX - (tileX + addX)), 2) + pow((targetY - (tileY + addY)), 2)));
return distance;
}
bool Ghost::render(int &delay, const std::vector<std::unique_ptr<Entity>> &entities, sf::RenderWindow *window,
Labyrinth &labyrinth) {
if (isScattering()) {
if (tileX == targetX && tileY == targetY)
scattering = false;
}
if (labyrinth.isIntersection(tileX, tileY)) {
if (decision) {
float dRight = calculateDistance(labyrinth, 1, 0);
float dLeft = calculateDistance(labyrinth, -1, 0);
float dUp = calculateDistance(labyrinth, 0, -1);
float dDown = calculateDistance(labyrinth, 0, 1);
if (dRight < dLeft && dRight < dUp && dRight < dDown)
moving = Direction::Right;
else if (dLeft < dRight && dLeft < dUp && dLeft < dDown)
moving = Direction::Left;
else if (dUp < dLeft && dUp < dRight && dUp < dDown)
moving = Direction::Up;
else if (dDown < dLeft && dDown < dUp && dDown < dRight)
moving = Direction::Down;
}
decision = false;
} else
decision = true;
if (canMove(labyrinth) && outHome)
movement();
else
decision = true;
return true;
}