-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waypoint.cpp
81 lines (73 loc) · 2.15 KB
/
Waypoint.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
#include "Waypoint.h"
Waypoint::Waypoint(const Pixel& coordinates, const Pixel& margin, Waypoint* previous) :
Screen(WAYPOINT_ICON, coordinates, margin, WAYPOINT_COLOR)
{
this->coordinates = coordinates;
this->previous = previous;
if (previous == nullptr) distance = 0;
else {
distance = previous->distance + 1;
this->updateIcon();
}
}
Pixel Waypoint::getCoordinates() const
{
return coordinates;
}
short Waypoint::getDistance() const
{
return distance;
}
Waypoint* Waypoint::getPrevious() const
{
return previous;
}
void Waypoint::update(Waypoint* waypoint)
{
if (waypoint == nullptr) return;
if (this == waypoint) return;
if (this == waypoint->previous) return;
if (waypoint == this->previous) return;
if (notNeighbours(waypoint)) return;
if (this->distance == waypoint->distance) return;
if (this->distance < waypoint->distance + 1) {
waypoint->distance = this->distance + 1;
waypoint->update(waypoint->previous);
waypoint->previous = this;
waypoint->updateIcon();
waypoint->draw();
}
else if (this->distance > waypoint->distance + 1) {
this->distance = waypoint->distance + 1;
this->update(this->previous);
this->previous = waypoint;
this->updateIcon();
this->draw();
}
}
bool Waypoint::notNeighbours(const Waypoint* waypoint) const
{
if (abs(this->coordinates.y - waypoint->coordinates.y) > 1 ||
abs(this->coordinates.x - waypoint->coordinates.x) > 1)
return true;
return false;
}
void Waypoint::updateIcon()
{
if (pixel.x == previous->pixel.x) {
if (pixel.y > previous->pixel.y) icon = ARROW_UP;
else icon = ARROW_DOWN;
}
else if (pixel.y == previous->pixel.y) {
if (pixel.x > previous->pixel.x) icon = ARROW_LEFT;
else icon = ARROW_RIGHT;
}
else if (pixel.x > previous->pixel.x) {
if (pixel.y > previous->pixel.y) icon = ARROW_UPLEFT;
else icon = ARROW_DOWNLEFT;
}
else if (pixel.x < previous->pixel.x) {
if (pixel.y > previous->pixel.x) icon = ARROW_UPRIGHT;
else icon = ARROW_DOWNRIGHT;
}
}