-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfield.cpp
140 lines (130 loc) · 5.12 KB
/
field.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
#include "field.h"
static const float BLOCK_SIZE = 25.f;
static const size_t FIELD_WIDTH = 32;
static const size_t FIELD_HEIGHT = 24;
static const char FIELD_MAZE[] = "################################"
"# # #"
"# # #"
"# # #"
"# @ # #"
"# #"
"# ####################"
"# #"
"# #"
"# # #"
"# # #"
"############ # #"
"# # #"
"# # #"
"# # #"
"# # #"
"# # #"
"# # #"
"# ############ #"
"# #"
"# #"
"# #"
"# #"
"################################";
static const sf::Color WALL_COLOR = sf::Color(52, 93, 199);
static const sf::Color ROAD_COLOR = sf::Color(40, 40, 40);
static sf::FloatRect moveRect(const sf::FloatRect& rect, sf::Vector2f& offset)
{
return { rect.left + offset.x, rect.top + offset.y, rect.width, rect.height };
}
// Находит символ '@' в исходной карте лабиринта.
sf::Vector2f getPackmanStartPosition()
{
for (size_t y = 0; y < FIELD_HEIGHT; y++)
{
for (size_t x = 0; x < FIELD_WIDTH; x++)
{
const size_t offset = x + y * FIELD_WIDTH;
if (FIELD_MAZE[offset] == '@')
{
return { x * BLOCK_SIZE, y * BLOCK_SIZE };
}
}
}
return { 0, 0 };
}
void initializeField(Field& field)
{
field.width = FIELD_WIDTH;
field.height = FIELD_HEIGHT;
field.cells = new Cell[field.width * field.height];
for (size_t y = 0; y < field.height; y++)
{
for (size_t x = 0; x < field.width; x++)
{
const size_t offset = x + y * field.width;
CellCategory category;
sf::Color color;
if (FIELD_MAZE[offset] == '#')
{
category = CellCategory::WALL;
color = WALL_COLOR;
}
else
{
category = CellCategory::ROAD;
color = ROAD_COLOR;
}
Cell& cell = field.cells[offset];
cell.category = category;
cell.bounds.setPosition(x * BLOCK_SIZE, y * BLOCK_SIZE);
cell.bounds.setSize(sf::Vector2f(BLOCK_SIZE, BLOCK_SIZE));
cell.bounds.setFillColor(color);
}
}
}
void drawField(sf::RenderWindow& window, const Field& field)
{
for (size_t i = 0; i < field.width * field.height; i++)
{
window.draw(field.cells[i].bounds);
}
}
// Модифицирует вектор перемещения, избегая столкновения
// прямоугольника `rect` со стенами лабиринта в поле `field`.
// Возвращает `true`, если вектор перемещения изменён.
bool checkFieldWallsCollision(const Field& field, const sf::FloatRect& oldBounds, sf::Vector2f& movement)
{
sf::FloatRect newBounds = moveRect(oldBounds, movement);
bool changed = false;
for (size_t i = 0, n = field.width * field.height; i < n; i++)
{
const Cell& cell = field.cells[i];
if (cell.category == CellCategory::ROAD)
{
continue;
}
sf::FloatRect blockBound = cell.bounds.getGlobalBounds();
if (newBounds.intersects(blockBound))
{
if (movement.y < 0)
{
movement.y += blockBound.top + blockBound.height - newBounds.top;
}
else if (movement.y > 0)
{
movement.y -= newBounds.top + newBounds.height - blockBound.top;
}
if (movement.x < 0)
{
movement.x += blockBound.left + blockBound.width - newBounds.left;
}
else if (movement.x > 0)
{
movement.x -= newBounds.left + newBounds.width - blockBound.left;
}
changed = true;
newBounds = moveRect(oldBounds, movement);
}
}
return changed;
}
void destroyField(Field& field)
{
delete[] field.cells;
}