forked from Les-Gningnegnieurs/atariBreakout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.cpp
More file actions
383 lines (357 loc) · 13.8 KB
/
Copy pathLevel.cpp
File metadata and controls
383 lines (357 loc) · 13.8 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
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
#include "Level.h"
Level::Level(LevelInfos I, Score* score, QGraphicsScene* scene): _scene(scene), _score(score){
rows = I.rows;
columns = I.columns;
BrickHeigth = I.Brick_heigth;
BrickLength = I.Brick_length;
maxScore = 0;
}
Level::Level() {
}
Level :: ~Level(){
}
void Level::draw() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
_board[i][j]->draw();
}
}
}
void Level::checkColl_DOWN_RIGHT(Balle* b, std::vector <Powerups*>& p, Controller& c){
// LE CODE EN COMMENTAIRE EST UNE IDÉE POUR OPTIMISER LES CHECKCOLLISIONS: ON CHECK JUSTE LES BRIQUES AUTOUR
// DE LA BALLE AU LIEU DE CHECK L'ENSEMBLE DE L'ARRAY
//for (int i = 0; i < rows / BrickHeigth; i++) //haut en bas
//{
//for (int j = 0; j < columns / BrickLength; j++) //gauche a droite
int i_max = (b->getPos().y / BrickHeigth) + 2;
int j_max = (b->getPos().x / BrickLength) + 2;
if (i_max > rows )
i_max = rows;
if (j_max > columns )
j_max = columns;
int i = i_max - 3;
if (i < 0)
i = 0;
for(i; i < i_max; i++)
{
int j = j_max - 3;
if (j < 0)
j = 0;
for(j;j<j_max;j++)
{
if (!_board[i][j]->est_Detruite())
{
Collision check = _board[i][j]->checkCollision(b);
switch (check) {
case LT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case RT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case DN:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case UP:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case CN:
b->changeVelocity(1, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case NO:
break;
}
}
}
}
}
void Level::checkColl_DOWN_LEFT(Balle* b,std::vector <Powerups*>& p, Controller& c){
int i_max = (b->getPos().y / BrickHeigth) + 2;
int j_max = (b->getPos().x / BrickLength) - 2;
if (i_max > rows )
i_max = rows;
if (j_max < 0)
j_max = 0;
int i = i_max - 3;
if (i < 0)
i = 0;
for (i; i < i_max; i++)
{
int j = j_max + 2;
if (j >= columns-1)
j = columns-1;
for (j; j >= j_max; j--)
{
//for (int i = 0; i < rows / BrickHeigth; i++) //haut en bas
//{
//for (int j = (columns / BrickLength) - 1; j >= 0; j--) //droite a gauche
//{
if (!_board[i][j]->est_Detruite())
{
Collision check = _board[i][j]->checkCollision(b);
switch (check) {
case LT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case RT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case DN:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case UP:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case CN:
b->changeVelocity(1, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case NO:
break;
}
}
}
}
}
void Level::checkColl_UP_RIGHT(Balle* b, std::vector <Powerups*>& p, Controller& c){
int i_max = (b->getPos().y / BrickHeigth) - 2;
int j_max = (b->getPos().x / BrickLength) + 2;
if (i_max < 0)
i_max = 0;
if (j_max > columns)
j_max = columns;
int i = i_max + 2;
if (i >= rows-1)
i = rows-1;
for (i; i >= i_max; i--)
{
int j = j_max - 3;
if (j < 0)
j = 0;
for (j; j < j_max; j++)
{
//for (int i = (rows / BrickHeigth) - 1; i >= 0; i--) //bas vers le haut
//{
//for (int j = 0; j < columns / BrickLength; j++) //gauche a droite
if (!_board[i][j]->est_Detruite())
{
Collision check = _board[i][j]->checkCollision(b);
switch (check) {
case LT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case RT:
b->changeVelocity(1, 0);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case DN:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case UP:
b->changeVelocity(0, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case CN:
b->changeVelocity(1, 1);
if (_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case NO:
break;
}
}
}
}
}
void Level::checkColl_UP_LEFT(Balle* b, std::vector <Powerups*>& p, Controller& c){
int i_max = (b->getPos().y / BrickHeigth) - 2;
int j_max = (b->getPos().x / BrickLength) - 2;
if (i_max < 0)
i_max = 0;
if (j_max < 0)
j_max = 0;
int i = i_max + 2;
if (i >= rows-1)
i = rows -1;
for(i; i>= i_max; i--)
{
int j = j_max + 2;
if (j >= columns-1)
j = columns-1;
for(j; j >= j_max; j--)
{
//for (int i = (rows / BrickHeigth) - 1; i >= 0; i--) //bas vers le haut
//{
//for (int j = (columns / BrickLength) - 1; j >= 0; j--) //droite vers la gauche
if (!_board[i][j]->est_Detruite())
{
Collision check = _board[i][j]->checkCollision(b);
switch (check) {
case LT:
b->changeVelocity(1, 0);
if(_board[i][j]->increase_Damage(p, _scene, c)) //si la brique est peté
_score->increase();
break;
case RT:
b->changeVelocity(1, 0);
if(_board[i][j]->increase_Damage(p, _scene, c))
_score->increase();
break;
case DN:
b->changeVelocity(0, 1);
if(_board[i][j]->increase_Damage(p, _scene, c))
_score->increase();
break;
case UP:
b->changeVelocity(0, 1);
if(_board[i][j]->increase_Damage(p, _scene, c))
_score->increase();
break;
case CN:
b->changeVelocity(1, 1);
if(_board[i][j]->increase_Damage(p, _scene, c))
_score->increase();
break;
case NO:
break;
}
}
}
}
}
void Level :: checkCollision(Balle* b, std::vector<Powerups*>& p, Controller& c){
//On va faire une fonction qui décide les valeurs du for loop pour chaque cas
//pour simplifier le code mais dans le fond g fait 4 facons différentes de checker l'Array qui dépende de la
//vitesse de la balle. Cette methode permet d'empecher qu'une balle qui est supposé frapper le bas d'une brique
//detecte dabord la collision de coin de la prochaine brique et fasse une collision qui fait pas de sens
if (b->getSpeed().y > 0 && b->getSpeed().x >= 0) // si la balle descend et va vers la droite
{
checkColl_DOWN_RIGHT(b, p, c);
}
else if (b->getSpeed().y > 0 && b->getSpeed().x <= 0) // si la balle descend et va vers la gauche
{
checkColl_DOWN_LEFT(b, p, c);
}
else if(b->getSpeed().y <= 0 && b->getSpeed().x >= 0){ //si la balle monte et va vers la droite
checkColl_UP_RIGHT(b, p, c);
}
else if (b->getSpeed().y <= 0 && b->getSpeed().x <= 0) { //si la balle monte et va vers la gauche
checkColl_UP_LEFT(b, p, c);
}
}
Brique* Level::getBrique(int row_idx, int column_idx) { //utile pour set le level au debut
if (row_idx >= rows || column_idx >= columns)
return nullptr;
return _board[row_idx][column_idx];
}
bool Level::setBrique(int row_idx, int column_idx, Brique* brique) { //utile pour set le level au debut
if (row_idx >= rows || column_idx >= columns)
return false;
_board[row_idx][column_idx] = brique;
}
void Level :: afficher(std::ostream& s){
s << "Informations sur le niveau" << std::endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
_board[i][j]->afficher(s);
}
}
}
int Level :: getRows(){
return rows;
}
int Level :: getColumns(){
return columns;
}
int Level :: getHeigth(){
return BrickHeigth;
}
int Level :: getLength(){
return BrickLength;
}
std::istream& operator >> (std::istream& s, Level& I){
//on est rendu aux infos sur le level
int x=0;
//for(int i=0; i< I.getRows()/I.getHeigth(); i++)
for (int i = 0; i < I.getRows() ; i++)
{
//for(int j=0; j<I.getColumns()/I.getLength(); j++)
for(int j=0; j<I.getColumns(); j++)
{
s >> x;
if(x == 1){
//pourrait faire dequoi avec le 128 pour ajuster l'écran
//si == 0 : pas de brique a cette position
I._board[i][j] = new Briquetest(j*I.getLength()+CONTOUR, i*I.getHeigth()+CONTOUR, I.getLength(), I.getHeigth());
I._board[i][j]->setPos(j * I.BrickLength + CONTOUR, i * I.BrickHeigth + CONTOUR);
I._scene->addItem(I._board[i][j]);
}
else if (x == 0) {
I._board[i][j] = new BriqueVoid(j * I.getLength() + CONTOUR, i * I.getHeigth() + CONTOUR, I.getLength(), I.getHeigth());
//I._scene->addItem(I._board[i][j]->getRect());
}
else if (x == 2) {
I._board[i][j] = new BriqueB(j * I.getLength() + CONTOUR, i * I.getHeigth() + CONTOUR, I.getLength(), I.getHeigth());
I._board[i][j]->setPos(j * I.BrickLength + CONTOUR, i * I.BrickHeigth + CONTOUR);
I._scene->addItem(I._board[i][j]);
}
else if (x == 3) {
I._board[i][j] = new BriqueC(j * I.getLength() + CONTOUR, i * I.getHeigth() + CONTOUR, I.getLength(), I.getHeigth());
I._board[i][j]->setPos(j * I.BrickLength + CONTOUR, i * I.BrickHeigth + CONTOUR);
I._scene->addItem(I._board[i][j]);
}
else if (x == 4) {
//pourrait faire dequoi avec le 128 pour ajuster l'écran
//si == 0 : pas de brique a cette position
I._board[i][j] = new BriqueA(j * I.getLength() + CONTOUR, i * I.getHeigth() + CONTOUR, I.getLength(), I.getHeigth());
I._board[i][j]->setPos(j * I.BrickLength + CONTOUR, i * I.BrickHeigth + CONTOUR);
I._scene->addItem(I._board[i][j]);
}
else if (x == 8) {
I._board[i][j] = new BriqueT(j * I.getLength() + CONTOUR, i * I.getHeigth() + CONTOUR, I.getLength(), I.getHeigth());
I._board[i][j]->setPos(j * I.BrickLength + CONTOUR, i * I.BrickHeigth + CONTOUR);
I._scene->addItem(I._board[i][j]);
}
I.maxScore += I._board[i][j]->getPv() * 100;
}
}
// }
return s;
}
//pour les briques de hauteur plus que 1.. comprend pas trop comment ca fonctionne donc on en va pas l'utiliser pour live
/*void Level::levelDrawline(std::ostream& s, int ligne) {
for(int i=0;i< _info.Brick_heigth;i++)
{
for(int j=0;j<_info.columns;j++)
{
_board[ligne][j]->draw(s);
}
s<<std::endl;
}
}
*/