-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.cpp
More file actions
134 lines (107 loc) · 2.8 KB
/
PathFinder.cpp
File metadata and controls
134 lines (107 loc) · 2.8 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
129
130
131
132
133
134
#include "../Include/defGameEngine.hpp"
#define DGE_PATHFINDER
#include "../Extensions/DGE_PathFinder.hpp"
using namespace def;
class AStar : public def::GameEngine
{
public:
AStar()
{
GetWindow()->SetTitle("AStar algorithm");
}
private:
def::PathFinder pathFinder;
static float Distance(Node* n1, Node* n2)
{
return (n1->pos - n2->pos).Length();
}
static float Heuristic(Node* n1, Node* n2)
{
return Distance(n1, n2);
}
protected:
bool OnUserCreate() override
{
pathFinder.ConstructMap({ 16, 16 });
pathFinder.SetNodes({ 1, 8 }, { 14, 8 });
pathFinder.ClearMap();
pathFinder.FindPath(&Distance, &Heuristic);
return true;
}
bool OnUserUpdate(float fDeltaTime) override
{
int nodeSize = 9;
int nodeBorder = 2;
def::Vector2i selectedNode = GetInput()->GetMousePosition() / nodeSize;
Node* nodes = pathFinder.GetNodes();
if (GetInput()->GetButtonState(def::Button::LEFT).released)
{
if (selectedNode >= def::Vector2i(0, 0) && selectedNode < pathFinder.GetMapSize())
{
int p = selectedNode.y * pathFinder.GetMapWidth() + selectedNode.x;
if (GetInput()->GetKeyState(def::Key::S).held)
pathFinder.SetNodes(&nodes[p], nullptr);
else if (GetInput()->GetKeyState(def::Key::G).held)
pathFinder.SetNodes(nullptr, &nodes[p]);
else
{
if (&nodes[p] != pathFinder.GetStartNode() && &nodes[p] != pathFinder.GetGoalNode())
nodes[p].isObstacle = !nodes[p].isObstacle;
}
pathFinder.ClearMap();
pathFinder.FindPath(&Distance, &Heuristic);
}
}
Clear(def::BLACK);
def::Vector2i i;
for (i.x = 0; i.x < pathFinder.GetMapWidth(); i.x++)
for (i.y = 0; i.y < pathFinder.GetMapHeight(); i.y++)
{
for (auto n : nodes[i.y * pathFinder.GetMapWidth() + i.x].neighbours)
{
DrawLine(
i * nodeSize + nodeSize / 2,
n->pos * nodeSize + nodeSize / 2,
def::DARK_BLUE);
}
}
for (i.x = 0; i.x < pathFinder.GetMapWidth(); i.x++)
for (i.y = 0; i.y < pathFinder.GetMapHeight(); i.y++)
{
def::Node& n = nodes[i.y * pathFinder.GetMapWidth() + i.x];
def::Pixel col;
if (n.isObstacle)
col = def::WHITE;
else if (&n == pathFinder.GetStartNode())
col = def::GREEN;
else if (&n == pathFinder.GetGoalNode())
col = def::RED;
else if (n.isVisited)
col = def::BLUE;
else
col = def::DARK_BLUE;
int s = nodeSize - 2 * nodeBorder;
FillRectangle(i * nodeSize + nodeBorder, { s, s }, col);
}
if (pathFinder.GetGoalNode() != nullptr)
{
Node* n = pathFinder.GetGoalNode();
while (n->parent != nullptr)
{
DrawLine(
n->pos * nodeSize + nodeSize / 2,
n->parent->pos * nodeSize + nodeSize / 2,
def::YELLOW);
n = n->parent;
}
}
return true;
}
};
int main()
{
AStar demo;
demo.Construct(256, 240, 4, 4);
demo.Run();
return 0;
}