-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breaker.cpp
435 lines (352 loc) · 9.32 KB
/
Breaker.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <iostream>
#include <fstream>
#include <cmath>
#include <SFML/Graphics.hpp>
#include "Breaker.hpp"
#include "Map.hpp"
#include "helpers.hpp"
#include "constants.hpp"
#include "default_files.hpp"
using namespace sf;
using std::abs;
using std::ofstream;
using std::ifstream;
Breaker::Breaker()
: paddle(Vector2f(PADDLE_SIZE_X, PADDLE_SIZE_Y)),
is_finished(false),
stuck(true),
lives(0),
paddle_speed(PADDLE_SPEED),
base_ball_speed(BALL_SPEED)
{
loadMaps();
std::cout << "Breaker: " << maps.size() << " maps loaded" << std::endl;
changeMap(0);
}
void Breaker::handleEvents(const Event& event)
{
Keyboard::Key key;
switch (event.type) {
case Event::Closed:
is_finished = true;
break;
case Event::KeyPressed:
key = event.key.code;
switch (key) {
case Keyboard::Escape:
is_finished = true;
break;
case Keyboard::N:
changeMap(actual_map + 1);
break;
case Keyboard::P:
changeMap(actual_map - 1);
break;
case Keyboard::Space:
if (stuck) {
stuck = false;
balls[0].direction.y = base_ball_speed;
}
break;
default:
break;
}
break;
default:
break;
}
}
void Breaker::update() {
float delta_t = getDeltaTime();
// paddle
if (Keyboard::isKeyPressed(Keyboard::Left)) {
paddle.move(-paddle_speed*delta_t, 0);
} else if (Keyboard::isKeyPressed(Keyboard::Right)) {
paddle.move(paddle_speed*delta_t, 0);
}
// ball
if (stuck) {
balls[0].sprite.setPosition(paddle.getPosition().x+paddle.getSize().x/2-BALL_SIDE/2,
paddle.getPosition().y-BALL_SIDE);
} else {
for (Ball& ball : balls) {
float speed = norm(ball.direction);
ball.direction = (ball.direction/speed) * ball.speed;
ball.sprite.move(ball.direction * delta_t);
}
}
// bonuses
for (Brick& b : bonuses) {
b.setPosition(b.x, b.y + BONUS_SPEED*delta_t);
}
// collisions
handleCollision();
}
void Breaker::render(RenderWindow& window)
{
maps[actual_map].render(window);
for (Brick& b : bonuses) {
b.draw(window);
}
window.draw(paddle);
for (Ball& ball : balls) {
window.draw(ball.sprite);
}
}
bool Breaker::isFinished()
{
return is_finished;
}
/** PRIVATE **/
float Breaker::getDeltaTime()
{
return dt.restart().asSeconds();
}
void Breaker::handleCollision()
{
Vector2u map_size = maps[actual_map].getSize();
for (auto it = balls.begin(); it != balls.end();) {
Ball& b = *it;
Vector2f pos = b.sprite.getPosition();
/* ball/walls collisions */
if (pos.x <= 0) {
b.direction = Vector2f(abs(b.direction.x), b.direction.y);
}
if (pos.x >= map_size.x-BALL_SIDE) {
b.direction = Vector2f(-abs(b.direction.x), b.direction.y);
}
if (pos.y <= 0) {
b.direction = Vector2f(b.direction.x, abs(b.direction.y));
}
if (pos.y+BALL_SIDE/2. >= map_size.y) {
it = balls.erase(it);
if (balls.size() == 0) {
if (lives == 0) {
changeMap(actual_map);
} else {
stuck = true;
addBall();
lives -= 1;
std::cout << "Lost a life, total " << lives << "\n";
}
return;
}
// avoids useless computations and moving `it` twice
continue;
}
/* ball/paddle collisions*/
if (b.sprite.getGlobalBounds().intersects(paddle.getGlobalBounds())) {
float width = paddle.getSize().x;
float p_x = paddle.getPosition().x + width/2.;
float delta_x = (p_x - (pos.x+BALL_SIDE/2.)) / (width/2.);
delta_x = clamp(delta_x, -1, 1);
float angle = getBounceAngle(delta_x);
b.direction.x = std::cos(angle);
b.direction.y = -std::sin(angle);
b.speed = getBounceSpeed(delta_x);
}
/* ball/bricks collisions */
int collided_index = ballBricksCollision(b);
if (collided_index != -1) {
handleBrickDestruction(collided_index);
}
++it;
}
/* paddle/walls collisions*/
float paddle_size = paddle.getSize().x;
if (paddle.getPosition().x < 0) {
paddle.setPosition(0, paddle.getPosition().y);
}
if (paddle.getPosition().x > map_size.x-paddle_size) {
paddle.setPosition(map_size.x-paddle_size, paddle.getPosition().y);
}
/* bonuses/bottom/paddle collisions */
for (auto it = bonuses.begin(); it != bonuses.end();) {
Brick& b = *it;
if (b.y > map_size.y) {
it = bonuses.erase(it);
} else if (b.getRect().intersects(paddle.getGlobalBounds())) {
applyBonus(b.type);
it = bonuses.erase(it);
} else {
++it;
}
}
/* exit measures*/
if (hasWon()) {
std::cout << "You won. Well played.\n";
changeMap((actual_map + 1) % maps.size());
}
}
/* The algorithm is based on
* https://gamedev.stackexchange.com/a/120897
* */
int Breaker::ballBricksCollision(Ball& ball) {
FloatRect bounds = ball.sprite.getGlobalBounds();
std::vector<Brick>& bricks = maps[actual_map].bricks;
const float r = bounds.width/2;
for (unsigned int i = 0; i < bricks.size(); i++) {
FloatRect b(bricks[i].getRect());
FloatRect h_rect(b.left-r, b.top, b.width+2*r, b.height);
FloatRect v_rect(b.left, b.top-r, b.width, b.height+2*r);
Vector2f ball_center(bounds.left+bounds.width/2, bounds.top+bounds.height/2);
if (h_rect.contains(ball_center)) {
if (ball_center.x < h_rect.left+h_rect.width/2) {
ball.direction.x = -abs(ball.direction.x); goto out;
} else {
ball.direction.x = abs(ball.direction.x); goto out;
}
}
if (v_rect.contains(ball_center)) {
if (ball_center.y < v_rect.top+v_rect.height/2) {
ball.direction.y = -abs(ball.direction.y); goto out;
} else {
ball.direction.y = abs(ball.direction.y); goto out;
}
}
if (distance(ball_center, Vector2f(b.left, b.top)) < r) { // top left
if (b.left - ball_center.x < b.top - ball_center.y) {
ball.direction.y = -abs(ball.direction.y); goto out;
} else {
ball.direction.x = -abs(ball.direction.x); goto out;
}
}
if (distance(ball_center, Vector2f(b.left+b.width, b.top)) < r) { // top right
if (ball_center.x - (b.left+b.width) < b.top - ball_center.y) {
ball.direction.y = -abs(ball.direction.y); goto out;
} else {
ball.direction.x = abs(ball.direction.x); goto out;
}
}
if (distance(ball_center, Vector2f(b.left+b.width, b.top+b.height)) < r) { // bottom right
if (ball_center.x - (b.left+b.width) < ball_center.y - (b.top+b.height)) {
ball.direction.y = abs(ball.direction.y); goto out;
} else {
ball.direction.x = abs(ball.direction.x); goto out;
}
}
if (distance(ball_center, Vector2f(b.left, b.top+b.height)) < r) { // bottom left
if (b.left - ball_center.x < ball_center.y - (b.top+b.height)) {
ball.direction.y = abs(ball.direction.y); goto out;
} else {
ball.direction.x = -abs(ball.direction.x); goto out;
}
}
// no collision was found
continue;
// a collision occured
out:
return i;
}
return -1;
}
void Breaker::handleBrickDestruction(int index) {
auto& bricks = maps[actual_map].bricks;
Brick& brick = bricks[index];
// Using a reference here allows the brick to evolve through hits.
// Perhaps it updates an internal counter, or whatever
if (brick.onDestroy()) {
if (brick.isBonus()) {
bonuses.push_back(brick);
}
bricks.erase(bricks.begin()+index);
}
}
void Breaker::applyBonus(Brick::Type type) {
if (type == Brick::EXPAND) {
Vector2f size = paddle.getSize();
paddle.setSize(Vector2f(size.x+PADDLE_EXPAND, size.y));
paddle.move(-PADDLE_EXPAND/2., 0);
}
if (type == Brick::ONE_UP) {
lives += 1;
std::cout << "Gained a life, total " << lives << "\n";
}
if (type == Brick::NEW_BALL) {
addBall();
Ball& b = balls.back();
b.speed = balls[0].speed;
b.direction = balls[0].direction;
b.direction.x = -b.direction.x;
b.sprite.setPosition(balls[0].sprite.getPosition());
}
}
void Breaker::addBall() {
Ball b;
CircleShape sprite(BALL_RADIUS, BALL_POINT_COUNT);
sprite.setFillColor(Color(150, 150, 150));
b.sprite = sprite;
b.speed = base_ball_speed;
balls.push_back(b);
}
// 0 < dx < 1, with 0 the beginning of the paddle
float Breaker::getBounceAngle(float dx) {
return map(dx, -1, 1, M_PI/12, M_PI - M_PI/12);
}
float Breaker::getBounceSpeed(float dx) {
if (dx < 0) {
return base_ball_speed*(1 + map(dx, -1, 0, .5, 0));
}
return base_ball_speed*(1 + map(dx, 0, 1, 0, .5));
}
bool Breaker::hasWon() const {
for (const Brick& b : maps[actual_map].bricks) {
if (b.type != Brick::UNBREAKABLE) {
return false;
}
}
return true;
}
void Breaker::changeMap(unsigned int map) {
if (map >= maps.size()) {
return;
}
std::cout << "Breaker: switching to map number " << map+1 << "\n";
actual_map = map;
stuck = true;
lives = 0;
maps[actual_map].load();
window_size = maps[actual_map].getSize();
paddle.setFillColor(Color::White);
paddle.setSize(Vector2f(PADDLE_SIZE_X, PADDLE_SIZE_Y));
paddle.setPosition(window_size.x/2.-paddle.getSize().x/2.,
window_size.y-PADDLE_SIZE_Y*1.2);
paddle_speed = PADDLE_SPEED;
base_ball_speed = BALL_SPEED;
balls.clear();
addBall();
bonuses.clear();
}
void Breaker::loadMaps() {
ifstream map_list("map.list");
std::string map_name;
if (!map_list) {
createDefaultMap();
map_list.open("map.list");
if (!map_list) {
is_finished = true;
return;
}
}
while (!map_list.eof()) {
map_list >> map_name;
if (map_list.fail()) {
break;
}
Map m(map_name);
maps.push_back(m);
}
}
void Breaker::createDefaultMap() {
ofstream map_list("map.list", ofstream::out | ofstream::trunc);
if (!map_list) {
is_finished = true;
return;
}
map_list << "M.map";
ifstream default_map("M.map");
if (!default_map) {
ofstream map("M.map", ofstream::out | ofstream::trunc);
map << default_map_str;
}
}