-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
379 lines (308 loc) · 9.86 KB
/
main.cpp
File metadata and controls
379 lines (308 loc) · 9.86 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
#include <Imagine/Graphics.h>
using namespace Imagine;
#include <Imagine/Images.h>
using namespace Imagine;
#include <iostream>
#include <fstream>
#include <string>
//---Constantes---//
#include "constantes.h"
//---Prototypes---//
#include "personnage.h"
#include "affichage.h"
#include "objets.h"
float minf(float a, float b);
void load_jumping(Personnage & player,Event e);
void start_cinematic(Personnage& player, Img player_right[10], Img player_left[10], const Img & cave, const Img & background, Img shadows[nb_shadow], std::string score);
void end_cinematic(Personnage& player, const Img & cave, const Img & background, Img shadows[nb_shadow], Img player_right[5], Img player_left[5], int Timer);
//---Main---//
int main(int argc, char** argv)
{
//Affichage score
std::ifstream fileScoreRead(stringSrcPath(score_path));
if (!fileScoreRead.is_open()) {
std::cerr << "Erreur lors de l'ouverture du fichier de lecture du score"<< std::endl;
return 1;
}
std::string Score;
fileScoreRead >> Score;
fileScoreRead.close();
srand(time(NULL));
openWindow(w,h,"CaveScroll");
Partie partie;
Personnage player;
int nbr_obstacle = partie.get_nbr_obstacle();
Obstacle* obstacle = new Obstacle[nbr_obstacle];
float spacebar_timer = 1.5;
int type_scrolling = 0;
Event e;
// Chargement des textures
Img left[2], right[2], up[2], down[2]; load_arrow(left,right,up,down);
Img cave; load_cave(cave);
Img background; load_background(background);
Img shadows[nb_shadow]; load_shadow(shadows);
Img heart[2]; load_heart(heart);
Img player_right[10], player_left[10]; load_charac(player_right, player_left);
Img shield_bonus;Img light_bonus[2];Img heart_bonus[2]; load_bonus(shield_bonus,light_bonus,heart_bonus);
start_cinematic(player,player_right, player_left,cave,background,shadows,Score);
player.setLighForce(lightforce_init);
while(player.getHp() > 0)
{
getEvent(0,e);
player.update_jump();
player.update_walk();
player.update_dash();
player.update_status();
partie.Timer += 1;
// AFFICHAGE
noRefreshBegin();
clearWindow();
draw_background(background);
draw_cave(cave,partie.Timer);
draw_charac(player_right, player_left, player, partie.Timer);
draw_shadow(shadows, player);
draw_heart(heart, player);
draw_timer(partie.Timer);
draw_score(Score);
draw_scrolling(type_scrolling,left,right,up,down);
noRefreshEnd();
// JOUEUR :
load_jumping(player,e);
player.walk(e);
player.dash(e);
player.crouch(e);
if(partie.Timer%100 ==0)
{
player.setLighForce(player.getLighForce() - 1);
}
// OBSTACLES :
bool outOfBounds = true;
for(int i = 0 ; i<nbr_obstacle;i++)
{
if(obstacle[i].outOfBounds() == false)
{
outOfBounds= false;
}
}
if(outOfBounds)
{
delete[] obstacle;
nbr_obstacle = partie.get_nbr_obstacle();
obstacle = new Obstacle[nbr_obstacle];
partie.init(obstacle);
type_scrolling = partie.get_scrolling_type();
}
for(int i = 0 ; i<nbr_obstacle;i++)
{
obstacle[i].move(type_scrolling);
obstacle[i].draw(shield_bonus,light_bonus,heart_bonus, partie.Timer);
}
//Le joueur se fait touché
for (int i = 0; i < nbr_obstacle; ++i)
{
player.getHit(obstacle[i]);
}
//Changement de scrolling quand le timer atteint 1000
if(partie.Timer % 70 == 0)
{
if(partie.Timer% 100 == 0)
{
partie.update_scrolling(4);//Bonus
}
else
{
partie.update_scrolling(rand()%4) ;//Scrolling d'obstacles
}
}
milliSleep(msleep);
}
end_cinematic(player, cave, background, shadows, player_right, player_left, partie.Timer);
delete[] obstacle;
if(partie.Timer > std::stoi(Score))
{
std::ofstream fileScoreWrite(stringSrcPath(score_path));
if (!fileScoreWrite.is_open()) {
std::cerr << "Erreur lors de l'ouverture du fichier de modification du score" << std::endl;
return 1;
}
fileScoreWrite << partie.Timer << std::endl;
fileScoreWrite.close();
}
endGraphics();
return 0;
}
//---Cinematique---//
void start_cinematic(Personnage& player, Img player_right[5], Img player_left[5], const Img & cave, const Img & background, Img shadows[nb_shadow], std::string score)
{
int crd = 0;
int cinematic_lenght_1 = 80;
int cinematic_lenght_2 = 50;
int cinematic_speed_x = w/(cinematic_lenght_1*(5.6/4));
int cinematic_speed_y = crds_y_init/(cinematic_lenght_2);
int crdx = 0;int crdy = crds_y_init;
int fall_lenght = 50;
Img keyboard; load_keyboard(keyboard);
Img title; load_title(title);
Img startbackground; load_startback(startbackground);
Img rock; load_rock(rock);
Img click; load_click(click);
for(int i = 0 ; i < cinematic_lenght_1 - 1; i++)
{
crdx += cinematic_speed_x;
player.setCoords(crdx,crds_y_init);
noRefreshBegin();
clearWindow();
draw_startback(startbackground);
if(i < cinematic_lenght_1 /2)
{
drawString(w/2 - 150,h/3,"De retour...",BLACK,20,0,false,true);
}
else
{
drawString(w/2 - 760/2,h/3,"Déjà " + score +" mètres explorés...",BLACK,20,0,false,true);
}
draw_charac(player_right, player_left, player, 0);
draw_rock(rock);
draw_title(title);
noRefreshEnd();
milliSleep(msleep);
}
//Attente avant de tomber
for(int i = 0; i < fall_lenght;i++)
{
if(i > cinematic_lenght_1 /4)
{
noRefreshBegin();
clearWindow();
draw_startback(startbackground);
// Pour ralentir le rafraichissement de la direction du personnage
int k = i/7;
player.set_facing(k%2);
draw_charac(player_right, player_left, player, 0);
draw_rock(rock);
draw_title(title);
noRefreshEnd();
}
milliSleep(msleep);
}
//Deuxième partie : le personnage tombe
for(int i = 0; i<cinematic_lenght_2;i++)
{
crdy += cinematic_speed_y;
player.setCoords(crdx,crdy);
noRefreshBegin();
clearWindow();
draw_startback(startbackground);
draw_charac(player_right, player_left, player, 0);
draw_rock(rock);
draw_title(title);
noRefreshEnd();
milliSleep(msleep);
}
//Affichage titre + touche
// Fondu noir
int fondu_duree = 50;
AlphaColor noir(0,0,0,0);
for (int i = 0; i < fondu_duree; i++)
{
noRefreshBegin();
clearWindow();
draw_startback(startbackground);
draw_title(title);
noir.a() = 255 * i/fondu_duree;
fillRect(0,0,w,h,noir);
noRefreshEnd();
milliSleep(msleep);
}
for (int i = 0; i <= fondu_duree; i++)
{
noRefreshBegin();
clearWindow();
draw_background(background);
draw_cave(cave,0);
draw_shadow(shadows);
draw_click(click);
draw_keyboard(keyboard);
noir.a() = 255 - 255 * i/fondu_duree;
fillRect(0,0,w,h,noir);
noRefreshEnd();
milliSleep(msleep);
}
noRefreshBegin();
clearWindow();
draw_background(background);
draw_cave(cave,0);
draw_shadow(shadows);
draw_click(click);
draw_keyboard(keyboard);
noRefreshEnd();
anyClick();
// chute du personnage par le haut
player.setCoords(crds_x_init,0);
player.setFalling(0);
player.set_jumping(true);
while(player.is_jumping())
{
player.update_jump();
player.update_walk();
player.update_dash();
noRefreshBegin();
clearWindow();
draw_background(background);
draw_cave(cave,0);
draw_charac(player_right, player_left, player, 0);
draw_shadow(shadows, player);
noRefreshEnd();
milliSleep(msleep);
}
}
void end_cinematic(Personnage& player, const Img & cave, const Img & background, Img shadows[nb_shadow], Img player_right[5], Img player_left[5], int Timers)
{
Img gameover; load_gameover(gameover);
int cinematic_lenght = 35;
player.set_crouching(true);
player.setCoords(player.getPos().x(),player.getPos().y() + size_y/2);
for(int i = 0 ; i < cinematic_lenght ; i++)
{
noRefreshBegin();
clearWindow();
draw_background(background);
draw_cave(cave,0);
draw_charac(player_right, player_left, player, 0);
std::vector<int> radius;
for (int i = 2; i < 4; i++) radius.push_back(player.getLighForce()*i*fac);
std::vector<double> alphas(radius.size(), 0.75 + 0.25*i/cinematic_lenght);
Img shadow_mask = applyMaskCircle(shadows[rand()%nb_shadow],radius,player.getCenter() - size_y/4,alphas);
display(shadow_mask,0,0,false,fac);
draw_timer(Timers);
noRefreshEnd();
milliSleep(msleep);
}
noRefreshBegin();
draw_gameover(gameover);
draw_timer(Timers);
noRefreshEnd();
}
//---Fonctions---//
float minf(float a, float b)
{
return (a < b) ? a : b;
}
void load_jumping(Personnage & player,Event e)
{
if (not player.is_jumping())
{
if (e.type == EVT_KEY_ON and e.key == KEY_UP)
{
player.setFalling(1);
player.jump();
}
}
else
{
if (e.type == EVT_KEY_OFF and e.key == KEY_UP and player.getFalling() == 1)
{
player.setFalling(0);
}
}
}