Interactif.c 6.97 KB
#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;
        }
    }
}