liste.c 3.13 KB
#include <stdio.h>
#include "Graphique/libgraph.h"
#include <stdlib.h>
#include "liste.h"

#define TAILLE_X     700
#define TAILLE_DESC  40
#define LARGEUR_MONS 30
#define HAUTEUR_MONS 30
#define LARGEUR_VAIS 35
#define SPEED        1
#define MISS_SPEED   5
#define LARGEUR_MISS 5
#define HAUTEUR_MISS 10

extern int score;



l_entite* create_l_entite(int x, int y, int speed, int image) {
  l_entite* monster_1 = (l_entite*)malloc(sizeof(l_entite));
  if (!monster_1) {
    fprintf(stderr, "Memory allocation failed!\n");
    exit(1);
  }
  monster_1->ent.image = image;
  monster_1->ent.x = x;
  monster_1->ent.y = y;
  monster_1->ent.speed = speed;
  monster_1->next = NULL;
  return monster_1;
}

entite create_entite(entite ent, int x, int y, int speed, int image) {
  ent.image = image;
  ent.x = x;
  ent.y = y;
  ent.speed = speed;
  return ent;
}

void add_entite(l_entite** head, int x, int y, int speed, int image) {
  l_entite* new_monster = create_l_entite(x, y, speed, image);
  if (*head == NULL) {
    *head = new_monster;
  } else {
    l_entite* current = *head;
    while (current->next != NULL) {
      current = current->next;
    }
    current->next = new_monster;
  }
}




void remove_entite(l_entite** head, l_entite* target) {
  if (head == NULL || *head == NULL || target == NULL) {
    return;
  }

  if (*head == target) {
    l_entite* temp = *head;
    *head = (*head)->next;
    free(temp);
    return;
  }

  l_entite* current = *head;
  while (current->next != NULL && current->next != target) {
    current = current->next;
  }

  if (current->next == target) {
    l_entite* temp = current->next;
    current->next = current->next->next;
    free(temp);
  }
}






void display_l_entite(l_entite* mons) {
  l_entite* ptr = mons;
  while (ptr != NULL) {
    afficherLutin(ptr->ent.image, ptr->ent.x, ptr->ent.y);
    ptr = ptr->next;
  }
}





void display_entite(entite ent) {
  afficherLutin(ent.image, ent.x, ent.y);
}




void moveMissile(l_entite* msl) {
  l_entite* ptr = msl;
  while (ptr != NULL) {
    ptr->ent.y -= MISS_SPEED;
    ptr = ptr->next;
  }
}





void addMissile(l_entite** head, entite vaisseau, int image) {
  add_entite(head, vaisseau.x + (LARGEUR_VAIS / 2), vaisseau.y - HAUTEUR_MISS, MISS_SPEED, image);
}



void updateScore() {
  score += 10;
  printf("Score: %d\n", score);

  char scoreText[20];
  sprintf(scoreText, "Score: %d", score);
  lutinTexte(scoreText, COULEUR_BLANC);
}





void detecterCollisions(l_entite* l_missile, l_entite* l_monstre) {
  l_entite *msl_ptr = l_missile, *msl_prev = NULL;
  l_entite *mon_ptr = l_monstre, *mon_prev = NULL;

  while (msl_ptr != NULL) {
    mon_ptr = l_monstre;
    mon_prev = NULL;

    while (mon_ptr != NULL) {
      if (msl_ptr->ent.x >= mon_ptr->ent.x &&
        msl_ptr->ent.x <= (mon_ptr->ent.x + LARGEUR_MONS) &&
        msl_ptr->ent.y >= mon_ptr->ent.y &&
        msl_ptr->ent.y <= (mon_ptr->ent.y + HAUTEUR_MONS)) {


      remove_entite(&l_missile, msl_ptr);
      remove_entite(&l_monstre, mon_ptr);


      updateScore();


      break;
        }
        mon_prev = mon_ptr;
        mon_ptr = mon_ptr->next;
    }
    msl_prev = msl_ptr;
    msl_ptr = msl_ptr->next;
  }
}