-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabyrinth.cpp
147 lines (130 loc) · 4.04 KB
/
Labyrinth.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "Labyrinth.h"
void Labyrinth::updateTileTypes()
{
std::array<bool, 4> walls{ 0, 0, 0, 0 };
for (unsigned int row = 0; row < height; row++)
for (unsigned int column = 0; column < width; column++)
{
walls = { 0, 0, 0, 0 };
Tile& tile = tiles[index(row, column)];
if (tile.makeCollision)
{
if (row == 0 || !tiles[index(row - 1, column)].makeCollision)
walls[static_cast<unsigned int>(Directions::Up)] = true;
if (column == width - 1 || !tiles[index(row, column + 1)].makeCollision)
walls[static_cast<unsigned int>(Directions::Right)] = true;
if (row == height - 1 || !tiles[index(row + 1, column)].makeCollision)
walls[static_cast<unsigned int>(Directions::Down)] = true;
if (column == 0 || !tiles[index(row, column - 1)].makeCollision)
walls[static_cast<unsigned int>(Directions::Left)] = true;
if (walls[0] && walls[1] && walls[2] && walls[3])
tile.orientation = 0;
else if (walls[1] && walls[2] && walls[3])
tile.orientation = 1;
else if (walls[0] && walls[2] && walls[3])
tile.orientation = 2;
else if (walls[0] && walls[1] && walls[3])
tile.orientation = 3;
else if (walls[0] && walls[1] && walls[2])
tile.orientation = 4;
else if (walls[2] && walls[3])
tile.orientation = 5;
else if (walls[0] && walls[3])
tile.orientation = 6;
else if (walls[0] && walls[1])
tile.orientation = 7;
else if (walls[1] && walls[2])
tile.orientation = 8;
else if (walls[1] && walls[3])
tile.orientation = 9;
else if (walls[0] && walls[2])
tile.orientation = 10;
else if (walls[3])
tile.orientation = 11;
else if (walls[0])
tile.orientation = 12;
else if (walls[1])
tile.orientation = 13;
else if (walls[2])
tile.orientation = 14;
else
tile.orientation = 15;
}
else tile.orientation = 0;
}
}
void Labyrinth::solveMaze()
{
MazeSolver solver(width, height);
solver.loadLabyrinth(*this);
std::pair<unsigned int, std::vector<Coordinates>> solution = solver.solveMaze(Coordinates(width / 2, height /2), exit);
shortestPathLength = solution.first + 1;
}
void Labyrinth::draw(sf::RenderWindow & window, float deltaTime)
{
for (unsigned int y = 0; y < height; y++)
{
for (unsigned int x = 0; x < width; x++)
{
sf::Vector2f position;
position.x = static_cast<float>(x * Tile::frameSize);
position.y = static_cast<float>(y * Tile::frameSize);
tiles[y * width + x].sprite.setPosition(position);
tiles[y * width + x].draw(window, deltaTime);
}
}
}
Labyrinth::Labyrinth(std::string filename, TextureManager & textureManager, std::map<std::string, Tile>& tileAtlas)
{
std::ifstream file(filename, std::ios::in);
unsigned int newWidth, newHeight;
file >> std::string() >> newWidth >> newHeight >> std::string() >> newWidth >> newHeight;
width = newWidth;
height = newHeight;
unsigned int tileType, orientation;
tiles.clear();
for (unsigned int row = 0; row < height; row++)
{
for (unsigned int column = 0; column < width; column++)
{
file >> tileType >> orientation;
unsigned int id = index(row, column);
switch (tileType)
{
case (0) :
tiles.emplace_back(tileAtlas.at("tunnel"));
tiles[id].orientation = 0;
break;
case (1) :
tiles.emplace_back(tileAtlas.at("wall"));
tiles[id].orientation = orientation;
break;
case (2) :
tiles.emplace_back(tileAtlas.at("exit"));
tiles[id].orientation = 0;
exit = sf::Vector2u(row, column);
break;
}
}
}
solveMaze();
}
Labyrinth::Labyrinth(unsigned int width, unsigned int height, TextureManager& textureManager,
std::map<std::string, Tile>& tileAtlas)
: MazePrimitive(width, height)
{
MazeGenerator generator(width, height);
auto maze = generator.generateMaze();
tiles.clear();
for (auto tile : maze)
{
if (tile == 0)
tiles.emplace_back(tileAtlas.at("tunnel"));
else if (tile == 1)
tiles.emplace_back(tileAtlas.at("wall"));
}
exit = generator.getRandomExit();
tiles.at(index(exit.x, exit.y)) = Tile(tileAtlas.at("exit"));
updateTileTypes();
solveMaze();
}