Interactif.c
6.97 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../Graphique/libgraph.h"
#include "../ListeC/Liste.h"
#include "Interactif.h"
#include "../Main/init.h"
#define TailleX 500
#define TailleY 500
#define Sol 475
#define ErreurHitbox 2
int CheckCollisionEntiteEntite(struct entite entite1, int L1, int H1, struct entite entite2, int L2, int H2)
{
//CheckX
int gauche1 = entite1.posx - L1/2 + ErreurHitbox;
int droite1 = entite1.posx + L1/2 - ErreurHitbox;
int gauche2 = entite2.posx - L2/2 + ErreurHitbox;
int droite2 = entite2.posx + L2/2 - ErreurHitbox;
int CheckX = (gauche1 >= gauche2 && gauche1 <= droite2) || (droite1 >= gauche2 && droite1 <= droite2);
//CheckY
int haut1 = entite1.posy - H1/2 + ErreurHitbox;
int bas1 = entite1.posy + H1/2 - ErreurHitbox;
int haut2 = entite2.posy - H2/2 + ErreurHitbox;
int bas2 = entite2.posy + H2/2 - ErreurHitbox;
int CheckY = (haut1 <= bas2 && haut1 >= haut2) || (bas1 <= bas2 && bas1 >= haut2);
return CheckX && CheckY;
}
struct entite* CheckCollisionListeEntite(struct liste_entite *Liste1,int L1,int H1,struct entite entite2, int L2, int H2)
{
struct liste_entite *pL1=Liste1;
while (pL1 != NULL)
{
if(CheckCollisionEntiteEntite(pL1->entite,L1,H1,entite2,L2,H2) == 1)
{
return &pL1->entite;
}
pL1=pL1->suivant;
}
return NULL;
}
struct liste_entite* CheckCollisionListeListe(struct liste_entite *Liste1,int L1,int H1,struct liste_entite *Liste2,int L2, int H2)
{
struct liste_entite *pL2=Liste2;
while (pL2 != NULL)
{
struct entite* collision = CheckCollisionListeEntite(Liste1,L1,H1,pL2->entite,L2,H2);
if (collision != NULL)
{
// Création des nœuds pour les deux entités
struct liste_entite* Entite1 = malloc(sizeof(struct liste_entite));
struct liste_entite* Entite2 = malloc(sizeof(struct liste_entite));
// Remplissage des nœuds avec les entités correspondantes
Entite1->entite = *collision;
Entite2->entite = pL2->entite;
// Relier les nœuds entre eux
Entite1->suivant = Entite2;
Entite2->suivant = NULL;
return Entite1;
}
else
pL2=pL2->suivant;
}
return NULL;
}
void NouveauDroppeurBombe(struct liste_entite** liste, struct entite* ent)
{
int posx = ent->posx;
int posy = ent->posy;
struct liste_entite* pListe = *liste;
struct entite* ent_bas = NULL;
// On parcourt la liste et on cherche l'entité la plus basse ayant la même position x
while (pListe != NULL)
{
if (pListe->entite.posy != posy)
{
if (pListe->entite.posx == posx && ent_bas == NULL)
{
ent_bas = &pListe->entite;
}
else if (pListe->entite.posx == posx && pListe->entite.posy > ent_bas->posy)
{
ent_bas = &pListe->entite;
}
}
pListe = pListe->suivant;
}
// Si aucune entité n'est située plus bas que l'entité en question, on ne peut pas dropper la bombe
if (ent_bas == NULL)
{
return;
}
ent_bas->dropbombe = 1;
}
int SupprimerEntitesEnCollision(struct liste_entite** Liste1, int L1, int H1, struct liste_entite** Liste2, int L2, int H2)
{
struct liste_entite* collision = CheckCollisionListeListe(*Liste1, L1, H1, *Liste2, L2, H2);
if (collision != NULL) {
// Récupération des entités impliquées
struct entite* entite1 = &collision->entite;
struct entite* entite2 = &collision->suivant->entite;
if (entite1->dropbombe == 1)
{
NouveauDroppeurBombe(Liste1,entite1);
}
if (entite2->dropbombe == 1)
{
NouveauDroppeurBombe(Liste2,entite2);
}
// Suppression de l'entité 1 de la liste 1
SupprimerEntite(Liste1, entite1);
// Suppression de l'entité 2 de la liste 2
SupprimerEntite(Liste2, entite2);
afficherLutin(bouillie, entite2->posx - hitboxbouillieL/2 + ErreurHitbox, entite2->posy - hitboxbouillieH/2 + ErreurHitbox);
return 1;
}
return 0;
}
void Tirer(struct entite joueur, struct liste_entite **pl)
{
if (*pl==NULL)
{
ajout_tete(pl,creer_entite(joueur.posx,joueur.posy,-1));
}
}
void DeplacementTire(int tire, struct liste_entite** l)
{
struct entite* ml = &(*l)->entite;
if (ml != NULL)
{
if (ml->posy <= 0)
{
afficherLutin(bouillie, ml->posx - hitboxbouillieL/2 + ErreurHitbox, ml->posy);
SupprimerEntite(l, ml);
}
else
{
ml->posy -= 5;
//Je divise ErreurHitbox par 2 car l'erreur du missile est plus petite que pour les autres images
afficherLutin(tire, ml->posx - hitboxmissileL/2 + ErreurHitbox/2, ml->posy - hitboxmissileH/2 + ErreurHitbox/2);
}
}
}
char touche()
{
char touche;
evenement even;
lireEvenement (&even,&touche,NULL);
return touche;
}
void action(struct entite *joueur, char c, struct liste_entite **tires)
{
switch (c)
{
case 'd':
if (joueur->posx <= 9*TailleX/10)
{
joueur->posx += 3;
}
break;
case 'q':
if (joueur->posx >= TailleX/10)
{
joueur->posx -= 3;
}
break;
case 't':
Tirer(*joueur, tires);
break;
default:
break;
}
}
void MakeBombeDroppable(struct liste_entite* enemies, struct liste_entite** bombes)
{
struct liste_entite* pL = enemies;
struct liste_entite* Dropable = NULL;
int taille = 0;
while (pL != NULL)
{
if (pL->entite.dropbombe == 1)
{
ajout_tete(&Dropable,pL->entite);
taille += 1;
}
pL=pL->suivant;
}
if(Dropable == NULL)
{
return;
}
int randomIndex = rand() % taille-1;
struct liste_entite* pLDropable = Dropable;
for (int i = 0; i <= randomIndex; i++)
{
pLDropable = pLDropable->suivant;
}
ajout_tete(bombes,creer_entite(pLDropable->entite.posx,pLDropable->entite.posy,-1));
}
void DeplacementBombe(int bombe, struct liste_entite** l)
{
struct liste_entite* ml = *l;
struct liste_entite* precedent = NULL;
while (ml != NULL)
{
if (ml->entite.posy + hitboxbombeH/2 - ErreurHitbox >= Sol)
{
struct entite* a_supprimer = &ml->entite;
ml = ml->suivant;
SupprimerEntite(l, a_supprimer);
}
else
{
ml->entite.posy += 2;
afficherLutin(bombe, ml->entite.posx - hitboxbombeL/2 + ErreurHitbox, ml->entite.posy - hitboxbombeH/2 + ErreurHitbox);
precedent = ml;
ml = ml->suivant;
}
}
}