diff --git a/Interactif/Interactif.c b/Interactif/Interactif.c index 6226aa9..ae7ce61 100644 --- a/Interactif/Interactif.c +++ b/Interactif/Interactif.c @@ -1,5 +1,6 @@ #include #include +#include #include "../Graphique/libgraph.h" #include "../ListeC/Liste.h" #include "Interactif.h" @@ -10,65 +11,64 @@ #define ErreurHitbox 2 -int CheckCollisionEntiteEntite(struct entite enti1,int L1,int H1,struct entite enti2 ,int L2, int H2) +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; - } - + int gauche1 = enti1.posx + ErreurHitbox; + int droite1 = enti1.posx + L1 - ErreurHitbox; + int gauche2 = enti2.posx + ErreurHitbox; + int droite2 = enti2.posx + L2 - ErreurHitbox; + int CheckX = (gauche1 >= gauche2 && gauche1 <= droite2) || (droite1 >= gauche2 && droite1 <= droite2); + //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 haut1 = enti1.posy + ErreurHitbox; + int bas1 = enti1.posy + H1 - ErreurHitbox; + int haut2 = enti2.posy + ErreurHitbox; + int bas2 = enti2.posy + H2 - ErreurHitbox; + int CheckY = (haut1 <= bas2 && haut1 >= haut2) || (bas1 <= bas2 && bas1 >= haut2); + + return CheckX && CheckY; } -int CheckCollisionListeEntite(struct liste_entite *Liste1,int L1,int H1,struct entite enti2, int L2, int H2) +struct entite* 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; + return &pL1->enti; } pL1=pL1->suivant; } - return 0; + 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) { - if(CheckCollisionListeEntite(Liste1,L1,H1,pL2->enti,L2,H2) == 1) + struct entite* collision = CheckCollisionListeEntite(Liste1,L1,H1,pL2->enti,L2,H2); + if (collision != NULL) { - return pL2; + // Création des nœuds pour les deux entités + struct liste_entite* Enti1 = malloc(sizeof(struct liste_entite)); + struct liste_entite* Enti2 = malloc(sizeof(struct liste_entite)); + + // Remplissage des nœuds avec les entités correspondantes + Enti1->enti = *collision; + Enti2->enti = pL2->enti; + + // Relier les nœuds entre eux + Enti1->suivant = Enti2; + Enti2->suivant = NULL; + + return Enti1; } else pL2=pL2->suivant; @@ -77,26 +77,98 @@ struct liste_entite* CheckCollisionListeListe(struct liste_entite *Liste1,int L1 } + + +void 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 enti1 = collision->enti; + struct entite enti2 = collision->suivant->enti; + + //Suppression de l'entité 1 de la liste 1 + + //precedant1 garde en memoire l'element precedant de la liste + struct liste_entite* precedant1 = NULL; + //courant1 garde en memoire l'element courant de la liste + struct liste_entite* courant1 = *Liste1; + + while (courant1 != NULL) + { + //Comparaison des entites avec memcmp + if (memcmp(&courant1->enti, &enti1, sizeof(struct entite)) == 0) + { + //Si l'element à supprimer est le 1er de la liste + if (precedant1 == NULL) + { + *Liste1 = courant1->suivant; + } + else + { + precedant1->suivant = courant1->suivant; + } + free(courant1); + break; + } + + precedant1 = courant1; + courant1 = courant1->suivant; + } + + // Suppression de l'entité 2 de la liste 2 + struct liste_entite* precedant2 = NULL; + struct liste_entite* courant2 = *Liste2; + + while (courant2 != NULL) + { + if (memcmp(&courant2->enti, &enti2, sizeof(struct entite)) == 0) + { + if (precedant2 == NULL) + { + *Liste2 = courant2->suivant; + } else + { + precedant2->suivant = courant2->suivant; + } + free(courant2); + break; + } + + precedant2 = courant2; + courant2 = courant2->suivant; + } + afficherLutin(bouillie, enti2.posx - L1/2 - ErreurHitbox, enti2.posy); + } +} + + + + + void Tirer(struct entite joueur, struct liste_entite **pl) { - struct liste_entite *ml=*pl; - if (ml==NULL) + if (*pl==NULL) { - ajout_tete(pl,creer_entite(joueur.posx+18,joueur.posy-5,0)); + ajout_tete(pl,creer_entite(joueur.posx+hitboxcanonL/2-2*ErreurHitbox,joueur.posy,0)); } } + + + -void DeplacementTire(int tire,struct liste_entite **l) -{ +void DeplacementTire(int tire, struct liste_entite **l) { struct liste_entite *ml = *l; - while (ml != NULL) + while (ml != NULL) { - if (ml->enti.posy <= 0) + if (ml->enti.posy <= 0) { - *l = NULL; - afficherLutin(bouillie, ml->enti.posx-hitboxbouillieL/2, ml->enti.posy); - break; + *l = ml->suivant; + afficherLutin(bouillie, ml->enti.posx - hitboxbouillieL / 2, ml->enti.posy); + free(ml); + ml = *l; } else { @@ -107,22 +179,6 @@ void DeplacementTire(int tire,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() { @@ -132,15 +188,29 @@ char touche() return touche; } -void action(struct entite *joueur,char c,struct liste_entite **tires) + + +void action(struct entite *joueur, char c, struct liste_entite **tires) { - if(c=='d') + switch (c) { - if (joueur->posx<=9*TailleX/10) {joueur->posx+=3;} - } - if(c=='q') - { - if (joueur->posx>=TailleX/10) {joueur->posx-=3;} + 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; } - if(c=='t'){Tirer(*joueur,tires);} } + diff --git a/Interactif/Interactif.h b/Interactif/Interactif.h index 87a8bb4..7612d0c 100644 --- a/Interactif/Interactif.h +++ b/Interactif/Interactif.h @@ -4,7 +4,7 @@ int CheckCollisionEntiteEntite(struct entite,int,int,struct entite,int,int); -int CheckCollisionListeEntite(struct liste_entite*,int,int,struct entite,int,int); +struct entite* CheckCollisionListeEntite(struct liste_entite*,int,int,struct entite,int,int); struct liste_entite* CheckCollisionListeListe(struct liste_entite*,int,int,struct liste_entite*,int,int); @@ -12,7 +12,7 @@ void Tirer(struct entite, struct liste_entite**); void DeplacementTire(int,struct liste_entite**); -void SupprIfTouch(struct liste_entite**,int,int,struct liste_entite**,int,int); +void SupprimerEntitesEnCollision(struct liste_entite**,int,int,struct liste_entite**,int,int); char touche(); diff --git a/ListeC/Liste.c b/ListeC/Liste.c index 47b7f79..8659742 100644 --- a/ListeC/Liste.c +++ b/ListeC/Liste.c @@ -2,17 +2,13 @@ #include #include "Liste.h" -void creer_liste(struct liste_entite *l) -{ - l = malloc(sizeof(struct liste_entite)); -} -struct entite creer_entite(int x, int y, int idd) +struct entite creer_entite(int x, int y, int etage) { struct entite e; e.posx=x; e.posy=y; - e.id=idd; + e.etage=etage; return e; } diff --git a/ListeC/Liste.h b/ListeC/Liste.h index aa0d7fa..3e07065 100644 --- a/ListeC/Liste.h +++ b/ListeC/Liste.h @@ -5,7 +5,7 @@ struct entite { int posx; int posy; - int id; + int etage; }; @@ -18,8 +18,6 @@ struct liste_entite void ajout_tete(struct liste_entite**, struct entite); -void creer_liste(struct liste_entite*); - struct entite creer_entite(int,int,int); void imprimer_liste(struct liste_entite*); diff --git a/Main/init.c b/Main/init.c index 4aafdf6..892b637 100644 --- a/Main/init.c +++ b/Main/init.c @@ -2,6 +2,7 @@ #include #include "../Graphique/libgraph.h" #include "../ListeC/Liste.h" +#include "../Interactif/Interactif.h" #include "init.h" #define TailleX 500 @@ -12,11 +13,9 @@ int missile; int sbire; int bouillie; -struct liste_entite *enemies = NULL; -struct liste_entite *tires = NULL; struct entite joueur; -char Nom[20]="Space_Invader"; +char Nom[15]="Space Invader"; char input='\0'; int hitboxcanonL; @@ -28,6 +27,8 @@ int hitboxsbireH; int hitboxbouillieL; int hitboxbouillieH; + + void initialiser() { canon = chargerLutin("../../Lutins/invader_canon.bmp",COULEUR_NOIR); @@ -47,3 +48,36 @@ void initialiser() joueur.posy = JoueurY ; } + +char pagedemarrage() +{ + static const char policeDefaut[]="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + int Largeur, Hauteur; + + rectanglePlein(0,0,TailleX,TailleY,COULEUR_NOIR); + char jouer[26]="Appuyer sur j pour Jouer"; + char quitter[30]="Appuyer ailleurs pour Quitter"; + + choisirPolice(policeDefaut,50); + int Bienvenue = lutinTexte(Nom,COULEUR_VERT); + tailleLutin(Bienvenue,&Largeur,&Hauteur); + afficherLutin(Bienvenue,TailleX/2-Largeur/2,TailleY/4+Hauteur/2); + + choisirPolice(policeDefaut,20); + + int J = lutinTexte(jouer,COULEUR_BLANC); + tailleLutin(J,&Largeur,&Hauteur); + afficherLutin(J,TailleX/2-Largeur/2,TailleY/2-Hauteur/2); + + int Q = lutinTexte(quitter,COULEUR_BLANC); + tailleLutin(Q,&Largeur,&Hauteur); + afficherLutin(Q,TailleX/2-Largeur/2,TailleY/2+Hauteur/2); + + attendreEvenement (); + input = touche(); + while (input=='\0') + { + input = touche(); + } + return input; +} diff --git a/Main/init.h b/Main/init.h index 4975b82..6243096 100644 --- a/Main/init.h +++ b/Main/init.h @@ -5,11 +5,11 @@ extern int canon; extern int missile; extern int sbire; extern int bouillie; -extern struct liste_entite *enemies; -extern struct liste_entite *tires; + extern struct entite joueur; -extern char Nom[20]; +extern char Nom[15]; extern char input; + extern int hitboxcanonL; extern int hitboxcanonH; extern int hitboxmissileL; @@ -20,4 +20,4 @@ extern int hitboxbouillieL; extern int hitboxbouillieH; void initialiser(); - +char pagedemarrage(); diff --git a/Main/main.c b/Main/main.c index d1a345e..19ed303 100644 --- a/Main/main.c +++ b/Main/main.c @@ -18,39 +18,54 @@ int main() creerSurface(TailleX,TailleY,Nom); initialiser(); + + + struct liste_entite *enemies = NULL; + struct liste_entite *tires = NULL; char texte[15]="SCORE : "; - int Score = lutinTexte(texte,COULEUR_ROUGE); + int Score = lutinTexte(texte,COULEUR_BLANC); - creer_liste(enemies); - creer_liste(tires); - Ligne_Monstre(&enemies,5); + LigneSbire(&enemies,3,1); int SensVague=1; - int *psens=&SensVague; + int compt=0; + + if (pagedemarrage() != 'j') + { + return 0; + } + + //Bouble principale while(input!='m') { rectanglePlein(0,0,TailleX,TailleY,COULEUR_NOIR); rectanglePlein(0,Sol,TailleX,2,COULEUR_VERT); + afficherLutin(Score,0,Sol+ErreurHitbox); afficherLutin(canon,joueur.posx,joueur.posy); - DeplacementLutin(sbire,enemies,psens,1); - + if(compt==10) + { + DeplacementSbire(enemies,&SensVague,1); + compt=0; + } + AfficherSbire(sbire,enemies); + input = touche(); action(&joueur,input,&tires); DeplacementTire(missile,&tires); - SupprIfTouch(&tires,hitboxmissileL,hitboxmissileH,&enemies,hitboxsbireL,hitboxsbireH); - - afficherLutin(Score,0,Sol); + SupprimerEntitesEnCollision(&tires,hitboxmissileL,hitboxmissileH,&enemies,hitboxsbireL,hitboxsbireH); majSurface(); SDL_Delay(20); + + compt+=1; } return 0; } diff --git a/Monstre/Monstre.c b/Monstre/Monstre.c index b87d6cf..e477000 100644 --- a/Monstre/Monstre.c +++ b/Monstre/Monstre.c @@ -8,7 +8,7 @@ #define TailleY 500 //sens 1 = Va vers la droite -void DeplacementLutin(int lutin,struct liste_entite *l, int *psens, int speed) +void DeplacementSbire(struct liste_entite *l, int *psens, int speed) { int ind=0; struct liste_entite *ml=l; @@ -18,13 +18,11 @@ void DeplacementLutin(int lutin,struct liste_entite *l, int *psens, int speed) { ml->enti.posx+=speed; if(ml->enti.posx>=9*TailleX/10)ind=1; - afficherLutin(lutin,ml->enti.posx,ml->enti.posy); } else { ml->enti.posx-=speed; if(ml->enti.posx<=TailleX/10)ind=2; - afficherLutin(lutin,ml->enti.posx,ml->enti.posy); } ml=ml->suivant; } @@ -51,13 +49,29 @@ void DeplacementLutin(int lutin,struct liste_entite *l, int *psens, int speed) } -void Ligne_Monstre(struct liste_entite **enemies,int nbr_enemies) + +void AfficherSbire(int lutin,struct liste_entite *l) +{ + struct liste_entite *ml=l; + while(ml != NULL) + { + afficherLutin(lutin,ml->enti.posx,ml->enti.posy); + ml=ml->suivant; + } +} + + + +void LigneSbire(struct liste_entite **enemies,int nbr_enemies, int nbr_rangee) { - int compteurY=TailleY/10; - int compteurX=TailleX/nbr_enemies; - for (int i=1; i<=nbr_enemies; i++) + for (int j=1; j<=nbr_rangee; j++) { - ajout_tete(enemies,creer_entite(compteurX,compteurY,0)); - compteurX +=2*TailleX/(3*nbr_enemies); + int compteurY=j*TailleY/10; + int compteurX=TailleX/nbr_enemies; + for (int i=0; i #include -void DeplacementLutin(int,struct liste_entite*,int*,int); +void DeplacementSbire(struct liste_entite*,int*,int); -void Ligne_Monstre(struct liste_entite**,int); +void AfficherSbire(int,struct liste_entite*); + +void LigneSbire(struct liste_entite**,int,int); -- libgit2 0.21.2