-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfield.cpp
364 lines (335 loc) · 11.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "field.h"
#include "packman.h"
#include <cmath>
#include <limits>
#include <cassert>
static const float BLOCK_SIZE = 32.f;
static const float EPSILON = std::numeric_limits<float>::epsilon();
static const float MAX_SHIFT = 0.5f * BLOCK_SIZE;
static const float MIN_COOKIE_OVERLAP_AREA = 400.f;
static const float COOKIE_RADIUS = 2.f;
static const float SUPERCOOKIE_RADIUS = 5.f;
static const char WALL_MARKER = '#';
static const char UNREACHABLE_MARKER = '!';
static const char COOKIE_MARKER = ' ';
static const char SUPERCOOKIE_MARKER = '$';
// Полноценная версия игрового поля.
#if 1
static const size_t FIELD_WIDTH = 25;
static const size_t FIELD_HEIGHT = 25;
static const char FIELD_MAZE[] =
"!#######################!"
"!# # #!"
"!# ## ##### # ##### ## #!"
"!# #!"
"!# ## # ######### # ## #!"
"!# # # # #!"
"!#### ##### # ##### ####!"
"!!!!# # C # #!!!!"
"##### # # ##### # # #####"
"# # #BPI# # #"
"##### # # ##### # # #####"
"!!!!# # # #!!!!"
"!#### # ######### # ####!"
"!# # #!"
"!# ## ##### # ##### ## #!"
"!# # @ # #!"
"!## # # ######### # # #!"
"!# $ # # # #!"
"!# ####### ### ####### #!"
"!# # # # # #!"
"!# # ### ## # ## ### # #!"
"!# # # # # # #!"
"!# # # #### # #### # # #!"
"!# # #!"
"!#######################!";
// Отладочная версия игрового поля.
#else
static const size_t FIELD_WIDTH = 6;
static const size_t FIELD_HEIGHT = 6;
static const char FIELD_MAZE[] =
"######"
"#@ #C#"
"# #B#"
"#$ #P#"
"# #I#"
"######";
#endif
static const sf::Color WALL_COLOR = sf::Color(52, 93, 199);
static const sf::Color ROAD_COLOR = sf::Color(40, 40, 40);
static const sf::Color COOKIE_COLOR = sf::Color(255, 255, 255);
struct FieldGraphics
{
sf::RectangleShape roadShape;
sf::RectangleShape wallShape;
sf::CircleShape cookieShape;
sf::CircleShape superCookieShape;
};
void initFieldGraphics(FieldGraphics &graphics)
{
graphics.wallShape.setFillColor(WALL_COLOR);
graphics.wallShape.setSize({BLOCK_SIZE, BLOCK_SIZE});
graphics.roadShape.setFillColor(ROAD_COLOR);
graphics.roadShape.setSize({BLOCK_SIZE, BLOCK_SIZE});
graphics.cookieShape.setRadius(COOKIE_RADIUS);
graphics.cookieShape.setFillColor(COOKIE_COLOR);
graphics.superCookieShape.setRadius(SUPERCOOKIE_RADIUS);
graphics.superCookieShape.setFillColor(COOKIE_COLOR);
}
static sf::FloatRect moveRect(const sf::FloatRect &rect, sf::Vector2f &offset)
{
return {rect.left + offset.x, rect.top + offset.y, rect.width, rect.height};
}
static float getArea(const sf::FloatRect &rect)
{
return rect.width * rect.height;
}
static float getBottom(const sf::FloatRect &rect)
{
return rect.top + rect.height;
}
static float getRight(const sf::FloatRect &rect)
{
return rect.left + rect.width;
}
static bool isBetween(float value, float minValue, float maxValue)
{
return (value >= minValue) && (value <= maxValue);
}
static Direction selectShiftDirection(float leftShift, float rightShift,
float topShift, float bottomShift,
float minShift, float maxShift)
{
Direction result = Direction::NONE;
float bestShift = FIELD_WIDTH * BLOCK_SIZE;
if (isBetween(leftShift, minShift, maxShift) && (leftShift < bestShift))
{
result = Direction::LEFT;
bestShift = leftShift;
}
if (isBetween(rightShift, minShift, maxShift) && (rightShift < bestShift))
{
result = Direction::RIGHT;
bestShift = rightShift;
}
if (isBetween(topShift, minShift, maxShift) && (topShift < bestShift))
{
result = Direction::UP;
bestShift = topShift;
}
if (isBetween(bottomShift, minShift, maxShift) && bottomShift < bestShift)
{
result = Direction::DOWN;
bestShift = bottomShift;
}
return result;
}
// Находит символ `marker` в исходной карте лабиринта.
static sf::Vector2f getStartPosition(char marker)
{
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] == marker)
{
return { x * BLOCK_SIZE, y * BLOCK_SIZE };
}
}
}
return { 0, 0 };
}
sf::Vector2f getPackmanStartPosition()
{
return getStartPosition('@');
}
sf::Vector2f getGhostStartPosition(GhostId ghostId)
{
switch (ghostId)
{
case GhostId::BLINKY:
return getStartPosition('B');
case GhostId::PINKY:
return getStartPosition('P');
case GhostId::INKY:
return getStartPosition('I');
case GhostId::CLYDE:
return getStartPosition('C');
default:
assert(false);
}
}
unsigned countRemainingCookies(const Field &field)
{
unsigned result = 0;
for (size_t offset = 0; offset < field.width * field.height; offset++)
{
const Cell &cell = field.cells[offset];
switch (cell.category)
{
case CellCategory::COOKIE:
case CellCategory::SUPERCOOKIE:
++result;
break;
default:
break;
}
}
return result;
}
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;
switch (FIELD_MAZE[offset])
{
case UNREACHABLE_MARKER:
category = CellCategory::EMPTY;
break;
case WALL_MARKER:
category = CellCategory::WALL;
break;
case COOKIE_MARKER:
category = CellCategory::COOKIE;
break;
case SUPERCOOKIE_MARKER:
category = CellCategory::SUPERCOOKIE;
break;
default:
category = CellCategory::EMPTY;
break;
}
Cell &cell = field.cells[offset];
cell.category = category;
cell.bounds.left = x * BLOCK_SIZE;
cell.bounds.top = y * BLOCK_SIZE;
cell.bounds.width = BLOCK_SIZE;
cell.bounds.height = BLOCK_SIZE;
}
}
}
void drawField(sf::RenderWindow &window, const Field &field)
{
FieldGraphics graphics;
initFieldGraphics(graphics);
for (size_t i = 0; i < field.width * field.height; i++)
{
const Cell &cell = field.cells[i];
const sf::Vector2f position = { cell.bounds.left, cell.bounds.top };
const sf::Vector2f center = position
+ sf::Vector2f(0.5f * cell.bounds.width, 0.5f * cell.bounds.height);
switch (cell.category)
{
case CellCategory::WALL:
graphics.wallShape.setPosition(position);
window.draw(graphics.wallShape);
break;
case CellCategory::EMPTY:
graphics.roadShape.setPosition(position);
window.draw(graphics.roadShape);
break;
case CellCategory::COOKIE:
graphics.roadShape.setPosition(position);
graphics.cookieShape.setPosition(center.x - COOKIE_RADIUS, center.y - COOKIE_RADIUS);
window.draw(graphics.roadShape);
window.draw(graphics.cookieShape);
break;
case CellCategory::SUPERCOOKIE:
graphics.roadShape.setPosition(position);
graphics.superCookieShape.setPosition(center.x - SUPERCOOKIE_RADIUS, center.y - SUPERCOOKIE_RADIUS);
window.draw(graphics.roadShape);
window.draw(graphics.superCookieShape);
break;
}
}
}
// Модифицирует вектор перемещения, избегая столкновения
// прямоугольника `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::WALL)
{
continue;
}
sf::FloatRect blockBound = cell.bounds;
if (newBounds.intersects(blockBound))
{
const float bottomShift = getBottom(blockBound) - newBounds.top;
const float topShift = getBottom(newBounds) - blockBound.top;
const float rightShift = getRight(blockBound) - newBounds.left;
const float leftShift = getRight(newBounds) - blockBound.left;
const float movementShift = std::max(std::abs(movement.x), std::abs(movement.y));
Direction direction = selectShiftDirection(leftShift, rightShift,
topShift, bottomShift,
movementShift + EPSILON, MAX_SHIFT);
if (direction == Direction::NONE)
{
direction = selectShiftDirection(leftShift, rightShift,
topShift, bottomShift,
0, MAX_SHIFT);
}
switch (direction)
{
case Direction::UP:
movement.y -= topShift;
break;
case Direction::DOWN:
movement.y += bottomShift;
break;
case Direction::LEFT:
movement.x -= leftShift;
break;
case Direction::RIGHT:
movement.x += rightShift;
break;
case Direction::NONE:
break;
}
changed = true;
newBounds = moveRect(oldBounds, movement);
}
}
return changed;
}
unsigned eatAllCookiesInBounds(Field &field, const sf::FloatRect &bounds)
{
unsigned cookiesCount = 0;
for (size_t i = 0, n = field.width * field.height; i < n; i++)
{
Cell &cell = field.cells[i];
if (cell.category != CellCategory::COOKIE
&& cell.category != CellCategory::SUPERCOOKIE)
{
continue;
}
sf::FloatRect intersection;
// Нужно не просто пересекаться с печеньем, но и иметь
// достаточную площадь пересечения.
if (cell.bounds.intersects(bounds, intersection)
&& (getArea(intersection) >= MIN_COOKIE_OVERLAP_AREA))
{
++cookiesCount;
cell.category = CellCategory::EMPTY;
}
}
return cookiesCount;
}
void destroyField(Field &field)
{
delete[] field.cells;
}