diff --git a/Interactif/Interactif.c b/Interactif/Interactif.c index 260f35c..6226aa9 100644 --- a/Interactif/Interactif.c +++ b/Interactif/Interactif.c @@ -3,9 +3,79 @@ #include "../Graphique/libgraph.h" #include "../ListeC/Liste.h" #include "Interactif.h" +#include "../Main/init.h" #define TailleX 500 #define TailleY 500 +#define ErreurHitbox 2 + + +int CheckCollisionEntiteEntite(struct entite enti1,int L1,int H1,struct entite enti2 ,int L2, int H2) +{ + //CheckX + int gauche1 = enti1.posx+ErreurHitbox; + int droite1 = enti1.posx+L1-ErreurHitbox; + int gauche2 = enti2.posx+ErreurHitbox; + int droite2 = enti2.posx+L2-ErreurHitbox; + int CheckX=0; + if(gauche1 >= gauche2 && gauche1 <= droite2) + { + CheckX=1; + } + else if(droite1 >= gauche2 && droite1 <= droite2) + { + CheckX=1; + } + + //CheckY + int haut1 = enti1.posy+ErreurHitbox; + int bas1 = enti1.posy+H1-ErreurHitbox; + int haut2 = enti2.posy+ErreurHitbox; + int bas2 = enti2.posy+H2-ErreurHitbox; + int CheckY=0; + if(haut1 <= bas2 && haut1 >= haut2) + { + CheckY=1; + } + else if(bas1 <= bas2 && bas1 >= haut2) + { + CheckY=1; + } + if(CheckX+CheckY==2){return 1;} + else return 0; +} + + +int CheckCollisionListeEntite(struct liste_entite *Liste1,int L1,int H1,struct entite enti2, int L2, int H2) +{ + struct liste_entite *pL1=Liste1; + while (pL1 != NULL) + { + if(CheckCollisionEntiteEntite(pL1->enti,L1,H1,enti2,L2,H2) == 1) + { + return 1; + } + pL1=pL1->suivant; + } + return 0; +} + + +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) + { + if(CheckCollisionListeEntite(Liste1,L1,H1,pL2->enti,L2,H2) == 1) + { + return pL2; + } + else + pL2=pL2->suivant; + } + return NULL; +} + void Tirer(struct entite joueur, struct liste_entite **pl) { @@ -17,7 +87,7 @@ void Tirer(struct entite joueur, struct liste_entite **pl) } -void DeplacementTire(int tire, int explo, struct liste_entite **l) +void DeplacementTire(int tire,struct liste_entite **l) { struct liste_entite *ml = *l; while (ml != NULL) @@ -25,7 +95,7 @@ void DeplacementTire(int tire, int explo, struct liste_entite **l) if (ml->enti.posy <= 0) { *l = NULL; - afficherLutin(explo, ml->enti.posx-20, ml->enti.posy); + afficherLutin(bouillie, ml->enti.posx-hitboxbouillieL/2, ml->enti.posy); break; } else @@ -37,6 +107,22 @@ void DeplacementTire(int tire, int explo, struct liste_entite **l) } } + + +void SupprIfTouch(struct liste_entite **Liste1,int L1,int H1,struct liste_entite **Liste2,int L2, int H2) +{ + struct liste_entite *suppr = CheckCollisionListeListe(*Liste1,L1,H1,*Liste2,L2,H2); + if (suppr != NULL) + { + int x = suppr->enti.posx-ErreurHitbox; + int y = suppr->enti.posy; + Supprimerentite(Liste2,suppr); + majSurface(); + afficherLutin(bouillie,x,y); + *Liste1=NULL; + } +} + char touche() { diff --git a/Interactif/Interactif.h b/Interactif/Interactif.h index 0e25130..87a8bb4 100644 --- a/Interactif/Interactif.h +++ b/Interactif/Interactif.h @@ -1,9 +1,18 @@ #include #include + +int CheckCollisionEntiteEntite(struct entite,int,int,struct entite,int,int); + +int CheckCollisionListeEntite(struct liste_entite*,int,int,struct entite,int,int); + +struct liste_entite* CheckCollisionListeListe(struct liste_entite*,int,int,struct liste_entite*,int,int); + void Tirer(struct entite, struct liste_entite**); -void DeplacementTire(int,int,struct liste_entite**); +void DeplacementTire(int,struct liste_entite**); + +void SupprIfTouch(struct liste_entite**,int,int,struct liste_entite**,int,int); char touche(); diff --git a/ListeC/Liste.c b/ListeC/Liste.c index fc20058..47b7f79 100644 --- a/ListeC/Liste.c +++ b/ListeC/Liste.c @@ -38,4 +38,41 @@ void imprimer_liste(struct liste_entite *l) printf("\n"); } - + +void Supprimerentite(struct liste_entite** l, struct liste_entite* suppr) +{ + //Liste ou Element NULL + if(*l == NULL || suppr == NULL) + { + return; + } + + //Dernier et Seul élément + if (*l == suppr && suppr->suivant == NULL) + { + *l = NULL; + return; + } + + //1er element + if(*l == suppr) + { + *l=suppr->suivant; + } + + else + { + struct liste_entite* precedent = *l; + while(precedent->suivant != NULL && precedent->suivant != suppr) + { + precedent=precedent->suivant; + } + + if(precedent->suivant == NULL) + { + return ; + } + precedent->suivant = suppr->suivant; + } + free(suppr); +} diff --git a/ListeC/Liste.h b/ListeC/Liste.h index d2599fc..aa0d7fa 100644 --- a/ListeC/Liste.h +++ b/ListeC/Liste.h @@ -23,3 +23,5 @@ void creer_liste(struct liste_entite*); struct entite creer_entite(int,int,int); void imprimer_liste(struct liste_entite*); + +void Supprimerentite(struct liste_entite**, struct liste_entite*); diff --git a/Main/init.c b/Main/init.c index 7ca21bf..4aafdf6 100644 --- a/Main/init.c +++ b/Main/init.c @@ -25,6 +25,8 @@ int hitboxmissileL; int hitboxmissileH; int hitboxsbireL; int hitboxsbireH; +int hitboxbouillieL; +int hitboxbouillieH; void initialiser() { @@ -33,15 +35,10 @@ void initialiser() sbire = chargerLutin("../../Lutins/invader_monstre1_1.bmp",COULEUR_NOIR); bouillie = chargerLutin("../../Lutins/invader_monstre_bouillie.bmp",COULEUR_NOIR); - int* hitboxL=&hitboxcanonL; - int* hitboxH=&hitboxcanonH; - tailleLutin(canon,hitboxL,hitboxH); - hitboxL=&hitboxmissileL; - hitboxH=&hitboxmissileH; - tailleLutin(canon,hitboxL,hitboxH); - hitboxL=&hitboxsbireL; - hitboxH=&hitboxsbireH; - tailleLutin(sbire,hitboxL,hitboxH); + tailleLutin(canon,&hitboxcanonL,&hitboxcanonH); + tailleLutin(missile,&hitboxmissileL,&hitboxmissileH); + tailleLutin(sbire,&hitboxsbireL,&hitboxsbireH); + tailleLutin(bouillie,&hitboxbouillieL,&hitboxbouillieH); #define JoueurX TailleX/2-hitboxcanonL/2 #define JoueurY 9*TailleY/10 diff --git a/Main/init.h b/Main/init.h index 40df281..4975b82 100644 --- a/Main/init.h +++ b/Main/init.h @@ -16,6 +16,8 @@ extern int hitboxmissileL; extern int hitboxmissileH; extern int hitboxsbireL; extern int hitboxsbireH; +extern int hitboxbouillieL; +extern int hitboxbouillieH; void initialiser(); diff --git a/Main/main.c b/Main/main.c index 65a562a..d1a345e 100644 --- a/Main/main.c +++ b/Main/main.c @@ -11,42 +11,7 @@ #define TailleX 500 #define TailleY 500 #define Sol 475 - -int CheckCollision(struct entite enti1,int L1,int H1,struct entite enti2 ,int L2, int H2) -{ - //CheckX - int gauche1 = enti1.posx-L1; - int droite1 = enti1.posx+L1; - int gauche2 = enti2.posx-L2; - int droite2 = enti2.posx+L2; - int CheckX=0; - if(gauche1 >= gauche2 && gauche1 <= droite2) - { - CheckX=1; - } - else if(droite1 >= gauche2 && droite1 <= droite2) - { - CheckX=1; - } - - //CheckY - int haut1 = enti1.posy-H1; - int bas1 = enti1.posy+H1; - int haut2 = enti2.posy-H2; - int bas2 = enti2.posy+H2; - int CheckY=0; - if(haut1 >= bas2 && haut1 <= haut2) - { - CheckY=1; - } - else if(bas1 >= bas2 && bas1 <= haut2) - { - CheckY=1; - } - - if(CheckX+CheckY==2){return 1;} - return 0; -} +#define ErreurHitbox 2 int main() { @@ -63,11 +28,8 @@ int main() Ligne_Monstre(&enemies,5); int SensVague=1; int *psens=&SensVague; - - printf("%d",hitboxcanonL); - printf("%d",hitboxsbireL); - printf("%d",hitboxmissileL); + while(input!='m') { rectanglePlein(0,0,TailleX,TailleY,COULEUR_NOIR); @@ -80,20 +42,10 @@ int main() input = touche(); action(&joueur,input,&tires); - DeplacementTire(missile,bouillie,&tires); + DeplacementTire(missile,&tires); - /* Test - struct entite missile1 = &tires->enti; + SupprIfTouch(&tires,hitboxmissileL,hitboxmissileH,&enemies,hitboxsbireL,hitboxsbireH); - struct liste_entite *p; - p = enemies; - while (p != NULL) - { - int ok = CheckCollision(missile1,hitboxmissileL,hitboxmissileH,p->enti,hitboxsbireL,hitboxsbireH); - printf("%d \n",ok); - p=p->suivant; - } - */ afficherLutin(Score,0,Sol); majSurface(); diff --git a/Missile/Missile.c b/Missile/Missile.c index c66e2fe..8eb2b48 100644 --- a/Missile/Missile.c +++ b/Missile/Missile.c @@ -3,6 +3,74 @@ #include "../Graphique/libgraph.h" #include "../ListeC/Liste.h" #include "Missile.h" +#define ErreurHitbox 2 + + +int CheckCollisionEntiteEntite(struct entite enti1,int L1,int H1,struct entite enti2 ,int L2, int H2) +{ + //CheckX + int gauche1 = enti1.posx+ErreurHitbox; + int droite1 = enti1.posx+L1-ErreurHitbox; + int gauche2 = enti2.posx+ErreurHitbox; + int droite2 = enti2.posx+L2-ErreurHitbox; + int CheckX=0; + if(gauche1 >= gauche2 && gauche1 <= droite2) + { + CheckX=1; + } + else if(droite1 >= gauche2 && droite1 <= droite2) + { + CheckX=1; + } + + //CheckY + int haut1 = enti1.posy+ErreurHitbox; + int bas1 = enti1.posy+H1-ErreurHitbox; + int haut2 = enti2.posy+ErreurHitbox; + int bas2 = enti2.posy+H2-ErreurHitbox; + int CheckY=0; + if(haut1 <= bas2 && haut1 >= haut2) + { + CheckY=1; + } + else if(bas1 <= bas2 && bas1 >= haut2) + { + CheckY=1; + } + if(CheckX+CheckY==2){return 1;} + else return 0; +} + + +int CheckCollisionListeEntite(struct liste_entite *Liste1,int L1,int H1,struct entite enti2, int L2, int H2) +{ + struct liste_entite *pL1=Liste1; + while (pL1 != NULL) + { + if(CheckCollisionEntiteEntite(pL1->enti,L1,H1,enti2,L2,H2) == 1) + { + return 1; + } + pL1=pL1->suivant; + } + return 0; +} + + +int 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) + { + if(CheckCollisionListeEntite(Liste1,L1,H1,pL2->enti,L2,H2) == 1) + { + return 1; + } + else + pL2=pL2->suivant; + } + return 0; +} void Tirer(struct entite joueur, struct liste_entite **pl) diff --git a/Missile/Missile.h b/Missile/Missile.h index 070415a..2ce96e3 100644 --- a/Missile/Missile.h +++ b/Missile/Missile.h @@ -1,6 +1,12 @@ #include #include +int CheckCollisionEntiteEntite(struct entite,int,int,struct entite,int,int); + +int CheckCollisionListeEntite(struct liste_entite*,int,int,struct entite,int,int); + +int CheckCollisionListeListe(struct liste_entite*,int,int,struct liste_entite*,int,int); + void Tirer(struct entite, struct liste_entite**); void DeplacementTire(int,struct liste_entite*); -- libgit2 0.21.2