-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
160 lines (134 loc) · 4.08 KB
/
Grid.cpp
File metadata and controls
160 lines (134 loc) · 4.08 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "Grid.hpp"
#include <iostream>
#include <queue>
#include <unordered_set>
Grid::Grid(std::vector<std::string> gridLayout) : gridBackground(ResourceManager::instance().getTexture("resources/sprites.png"))
{
rowCnt = gridLayout.size();
colCnt = gridLayout[0].size();
if (rowCnt != GRID_ROW_COUNT || colCnt != GRID_COL_COUNT) {
std::cout << "ERROR: Wrong grid dimensions" << std::endl;
std::exit(1);
}
grid = std::vector<std::vector<Tile>>(31, std::vector<Tile>(28, Tile()));
for (int i = 0; i < rowCnt; i++) {
for (int j = 0; j < colCnt; j++) {
auto tileType = TileType::LEGAL;
auto foodType = FoodType::NONE;
if (gridLayout[i][j] == 'X')
tileType = TileType::WALL;
if (gridLayout[i][j] == 'O')
tileType = TileType::GHOST_START;
if (gridLayout[i][j] == '.')
foodType = FoodType::BASIC;
if (gridLayout[i][j] == '@')
foodType = FoodType::BIG;
Tile tile = Tile(
i,
j,
sf::Vector2f(
static_cast<float>(j * TILE_SIZE),
static_cast<float>(GRID_TOP_OFFSET * TILE_SIZE + i * TILE_SIZE)
),
sf::Vector2f(
static_cast<float>(TILE_SIZE),
static_cast<float>(TILE_SIZE)
),
tileType,
foodType
);
grid[i][j] = tile;
}
}
gridBackground.setTextureRect(sf::IntRect({ 228, 0 }, { 224, 248 }));
gridBackground.setPosition({ 0.f, TILE_SIZE * GRID_TOP_OFFSET * 1.f });
gridBackground.setScale({ SCALE * 1.f, SCALE * 1.f });
}
bool Grid::isShapeInTile(const sf::Shape& shape, const Tile& tile)
{
sf::FloatRect bounds = shape.getGlobalBounds();
sf::Vector2f shapeCenter = sf::Vector2f(bounds.position.x + bounds.size.x / 2.f, bounds.position.y + bounds.size.y / 2.f);
return tile.getRectangle().getGlobalBounds().contains(shapeCenter);
}
bool Grid::isShapeInTile(const sf::Sprite& shape, const Tile& tile)
{
sf::FloatRect bounds = shape.getGlobalBounds();
sf::Vector2f shapeCenter = sf::Vector2f(bounds.position.x + bounds.size.x / 2.f, bounds.position.y + bounds.size.y / 2.f);
return tile.getRectangle().getGlobalBounds().contains(shapeCenter);
}
void Grid::render(sf::RenderWindow& window, bool showGrid) {
window.draw(gridBackground);
for (int i = 0; i < rowCnt; i++) {
for (int j = 0; j < colCnt; j++) {
if (grid[i][j].hasFood())
window.draw(grid[i][j].getFoodSprite());
if(showGrid)
window.draw(grid[i][j].getRectangle());
}
}
}
Tile* Grid::nextTile(Tile* srcTile, DirectionEnum direction)
{
int currentCol = srcTile->getCol();
int currentRow = srcTile->getRow();
int newCol = 0, newRow = 0;
switch (direction) {
case DirectionEnum::NONE:
return srcTile;
break;
case DirectionEnum::UP:
newCol = currentCol;
newRow = currentRow - 1;
break;
case DirectionEnum::DOWN:
newCol = currentCol;
newRow = currentRow + 1;
break;
case DirectionEnum::LEFT:
newCol = currentCol - 1;
newRow = currentRow;
break;
case DirectionEnum::RIGHT:
newCol = currentCol + 1;
newRow = currentRow;
break;
}
newCol = ((newCol % colCnt) + colCnt) % colCnt;
newRow = ((newRow % rowCnt) + rowCnt) % rowCnt;
return &grid[newRow][newCol];
}
float Grid::tileDistance(Tile* tile1, Tile* tile2, Tile* currentTile)
{
std::unordered_set<Tile*> visited;
std::vector<DirectionEnum> allDirections = { DirectionEnum::UP, DirectionEnum::DOWN, DirectionEnum::LEFT, DirectionEnum::RIGHT };
int distance = 0;
std::queue<Tile*> que;
que.push(tile1);
// Add this tile since ghost can't go backwards
visited.insert(currentTile);
while (!que.empty()) {
int n = que.size();
for (int i = 0; i < n; i++) {
Tile* curr = que.front();
que.pop();
visited.insert(curr);
for (auto x : allDirections) {
Tile* nextTile = this->nextTile(curr, x);
if (nextTile->isLegal() && visited.count(nextTile) == 0) {
que.push(nextTile);
}
}
// Found it
if (curr == tile2) {
return distance;
}
}
distance++;
}
// In case target is in wall, calculate eucilidan distance instead
if (distance == 0) {
float x1 = tile1->getRow(), x2 = tile2->getRow(), y1 = tile1->getCol(), y2 = tile2->getCol();
distance = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
return distance;
}