-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevent.c
302 lines (267 loc) · 7.39 KB
/
event.c
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
/*
Super Mario Bros
Menu
event.c
Antoine Drabble
Création: 07.01.13
*/
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <fmod.h>
#include "event.h"
void UpdateEvents(Input* in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
in->key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym]=0;
break;
case SDL_QUIT:
in->quit = 1;
break;
default:
break;
}
}
}
int CollisionDecor(Map* m, SDL_Rect* perso, Sprites* S, Chars* mario, int vx, Object* shroom)
{
int xmin,xmax,ymin,ymax,i,j,indicetile;
xmin = perso->x / Size_Sprite;
ymin = perso->y / Size_Sprite;
xmax = (perso->x + perso->w -1) / Size_Sprite;
ymax = (perso->y + perso->h -1) / Size_Sprite;
mario->G=1;
if (mario->lvlperdu>0){
mario->lvlperdu--;
if(mario->invisible==1)
mario->invisible=0;
else
mario->invisible=1;
}
else{
mario->invisible=0;
}
if (perso->h+perso->y<m->Nb_Block_H*Size_Sprite-10){
j = (perso->y + perso->h +1) / Size_Sprite;
for(i=xmin;i<=xmax;i++) //Si il y a un bloc en dessous
{
indicetile = m->LoadedMap[i][j];
if (S[indicetile].traverser){
mario->G=0;
mario->jump = 1;
}
if (indicetile==10){
if (mario->lvlperdu<1)
{
if(mario->lvl==1)
mario->lose=1;
if(mario->lvl==2)
audio_play(6);
mario->lvl=1;
mario->position.y+=10;
mario->lvlperdu=400;
}
}
else{ //Si on ne touche pas le sol et que le temps de saut est de plus de 1000, on retombe.
if(mario->jumptime>1000)
mario->jump = 0;
}
}
}
j = (perso->y - 1) / Size_Sprite;
for(i=xmin;i<=xmax;i++) //Si il y a un bloc en dessus
{
indicetile = m->LoadedMap[i][j];
if (S[indicetile].traverser)
mario->jumptime = 1001;
if (indicetile==3){
if(S[m->LoadedMap[i][j-1]].traverser==0){
if (m->Used[i][j]==0){
shroom->nb_objet++; //Creation d'un champignon!
shroom->used[shroom->nb_objet]= 0;
shroom->position[shroom->nb_objet].x = i*Size_Sprite;
shroom->position[shroom->nb_objet].y = (j-1)*Size_Sprite;
m->Used[i][j]=1;
}
}
}
}
i= (perso->x + perso->w) / Size_Sprite; //Si on touche un bloc à notre droite,
for(j=ymin;j<=ymax;j++)
{
indicetile = m->LoadedMap[i][j];
if (S[indicetile].traverser){
perso->x-=vx;
j=ymax+1;
}
}
i = (perso->x -1) / Size_Sprite; //Si on touche un bloc à note gauche
for(j=ymin;j<=ymax;j++)
{
indicetile = m->LoadedMap[i][j];
if (S[indicetile].traverser){
perso->x-=vx;
j=ymax+1;
}
}
for(i=xmin;i<=xmin;i++){
for(j=ymin;j<=ymax;j++)
{
indicetile = m->LoadedMap[i][j];
if (indicetile==8 || indicetile==9){ //Si on touche le drapeau de la fin
mario->win = 1;
}
}
}
return 0; //Si pas de collision
}
int EssaiDeplacement(Map* m, Chars* mario, int vx, int vy, Sprites* S, Object* shroom)
{
SDL_Rect test;
test = mario->position;
test.x+=vx;
test.y+=vy;
int temp;
temp = CollisionDecor(m, &test, S, mario, vx, shroom);
if(test.y+test.h>(Size_Sprite*m->Nb_Block_H)+100)
mario->lose=1;
if (mario->G==1) // Gravité activée si on ne touche pas le sol
test.y += 2;
else
mario->jumptime = 1;
if (test.x-1<0) //Si on touche le bord gauche de la map
test.x-=vx;
if (test.x + test.w>=m->Nb_Block_W*Size_Sprite-2) //Si on touche le bord droite de la map
test.x-=vx;
if (test.y<0){ //Si on touche le haut de la map
mario->jumptime = 1001;
test.y+=vy;
}
if (temp==0) //Si pas de colision on modifie la position du personnage
{
mario->position = test;
return 1;
}
return 0;
}
void Deplace(Map* m, Chars* mario,int vx,int vy, Sprites* S, Object* shroom)
{
if (vx>=Size_Sprite || vy>=Size_Sprite) //si jamais le vercteur est plus grand qu'un sprite, pour pas qu'on traverse.
{
Deplace(m,mario,vx/2,vy/2,S,shroom);
Deplace(m,mario,vx/3,vy/3,S,shroom);
Deplace(m,mario,vx/4,vy/4,S,shroom);
return;
}
if (EssaiDeplacement(m,mario,vx,vy,S,shroom)==1) //Si pas de collision
return;
}
void Evolue(Input* in, Map *m, Chars *mario, Sprites *S, Object* shroom)
{
int vx,vy;
int vitesse = 2;
vx = vy = 0;
if (!in->key[SDLK_UP]){
mario->jumptime=1001;
}
if (in->key[SDLK_UP]){
if (mario->jump==1){
if (mario->jumptime<=1)
audio_play(2);
mario->jumptime +=10;
vy -= 5;
if(mario->jumptime>1000)
in->key[SDLK_UP]=0; //Ne pas sautez tout le temps si on laisse la touche haut activée
}
}
if (in->key[SDLK_LEFT]){
vx = -vitesse;
mario->directionmario = 1;
}
if (in->key[SDLK_RIGHT]){
vx = vitesse;
mario->directionmario = 2;
}
Deplace(m,mario,vx,vy, S, shroom);
}
void MapScroll(Map* m, Chars* mario)
{
m->xscroll = mario->position.x + mario->position.w/2 - WindowW/2;
m->yscroll = mario->position.y + mario->position.h/2 - WindowH/2;
if (m->xscroll<0)
m->xscroll=0;
if (m->yscroll<0)
m->yscroll=0;
if (m->xscroll>m->Nb_Block_W*Size_Sprite-WindowW-1)
m->xscroll=m->Nb_Block_W*Size_Sprite-WindowW-1;
if (m->yscroll>m->Nb_Block_H*Size_Sprite-WindowH-1)
m->yscroll=m->Nb_Block_H*Size_Sprite-WindowH-1;
}
int NiveauFini (SDL_Surface* screen, Chars* mario, Map* m)
{
SDL_Event event;
int continuer;
if (mario->win==1)
{
audio_play(4);
SDL_Surface* reussi = IMG_Load("images/win.png");
SDL_Rect positionreussi ;
positionreussi.x = WindowW/2-reussi->w/2;
positionreussi.y = WindowH/2-reussi->h/2;
SDL_BlitSurface(reussi,NULL,screen,&positionreussi);
SDL_Flip(screen);
SDL_Delay(1000);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
continuer = 0;
break;
}
}
mario->win=0;
SDL_FreeSurface(reussi);
return 1;
}
if (mario->lose==1)
{
audio_play(3);
SDL_Surface* perdu = IMG_Load("images/lose.png");
SDL_Rect positionperdu;
positionperdu.x = WindowW/2-perdu->w/2;
positionperdu.y = WindowH/2-perdu->h/2;
SDL_BlitSurface(perdu,NULL,screen,&positionperdu);
SDL_Flip(screen);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
continuer = 0;
break;
}
}
mario->lose=0;
SDL_FreeSurface(perdu);
return 1;
}
return 0;
}