#include #include #include #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 = (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 = (haut1 <= bas2 && haut1 >= haut2) || (bas1 <= bas2 && bas1 >= haut2); return CheckX && CheckY; } 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 &pL1->enti; } 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->enti,L2,H2); if (collision != NULL) { // 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; } return NULL; } 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) { if (*pl==NULL) { ajout_tete(pl,creer_entite(joueur.posx+hitboxcanonL/2-2*ErreurHitbox,joueur.posy,0)); } } void DeplacementTire(int tire, struct liste_entite **l) { struct liste_entite *ml = *l; while (ml != NULL) { if (ml->enti.posy <= 0) { *l = ml->suivant; afficherLutin(bouillie, ml->enti.posx - hitboxbouillieL / 2, ml->enti.posy); free(ml); ml = *l; } else { ml->enti.posy -= 5; afficherLutin(tire, ml->enti.posx, ml->enti.posy); ml = ml->suivant; } } } 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; } }