Skip to content
Snippets Groups Projects
Commit 07938c3a authored by SmallIshMink's avatar SmallIshMink
Browse files

Modification des collisions

Lecture du fichier via char et non int (plus de possibilité d'element)
Suppression handle_sprites_collide, remplacer par collide
parent dbe8dbb5
Branches
No related tags found
No related merge requests found
#include "sprites.h"
#include "../../constante.c"
#include <string.h>
int isOverScreen(sprite_t *sprite){
if(sprite->x < 0 || sprite->x + sprite->w > SCREEN_WIDTH || sprite->y < 0 || sprite->y + sprite->h > SCREEN_HEIGHT){
return 1;
......@@ -12,12 +13,15 @@ void print_sprite(sprite_t *sprite){
}
sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, int id){
sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, char id){
sprite = malloc(sizeof(sprite_t));
sprite->x = x;
sprite->y = y;
sprite->w = w;
sprite->h = h;
sprite->id = malloc(sizeof(char) * 10);
strcpy(sprite->id, &id);
return sprite;
}
......
......@@ -28,7 +28,7 @@ struct sprite_s{
int y;
int w;
int h;
int id;
char * id;
};
typedef struct sprite_s sprite_t;
......@@ -58,10 +58,10 @@ void print_sprite(sprite_t *sprite);
* \param h
* \return sprite_t*
*/
sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, int id);
sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, char id);
/**
* \brief La fonction libère les données du sprite
* \brief La fonction indique si les sprites se touchent
*
* \param sp1
* \param sp2
......
No preview for this file type
......@@ -11,17 +11,9 @@ void update_data(world_t *world){
if (world->vaisseau->y + world->vaisseau->h > SCREEN_HEIGHT) world->vaisseau->y = SCREEN_HEIGHT - world->vaisseau->h;
}
for(int i = 0; i < world->nb_murs; i++){
if (handle_sprite_collide(world->vaisseau, world->murs[i], world, 0) == 1){
break;
}else if (handle_sprite_collide(world->vaisseau, world->murs[i], world, 0) == 2){
printf("CHangement de sens\n");
break;
}
}
if (handle_sprite_collide(world->vaisseau, world->ligneArriver, world, 0) == 1){ // si le vaisseau touche la ligne d'arrivée
world->gameover = 1;
collide(world->vaisseau, world->murs[i], world, 0);
}
collide(world->vaisseau, world->ligneArriver, world, 1);
world->timer = SDL_GetTicks();
}
......@@ -36,9 +28,9 @@ void init_data(world_t * world){
world->gameover = 0;
world->speed_h = (float)INITIAL_SPEED;
// Initialisation du vaisseau
world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE, SHIP_SIZE, SHIP_SIZE, 0);
world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE, SHIP_SIZE, SHIP_SIZE, '0');
init_walls(world);
world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 0);
world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z');
print_sprite(world->vaisseau);
world->startTimer = SDL_GetTicks();
......@@ -57,19 +49,19 @@ void clean_data(world_t *world){
printf("clean_data");
}
int handle_sprite_collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make_disappear){
void collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make_disappear){
if (sprites_collide(sp1, sp2)){
world->gameover = 1;
printf("collision");
return 1;
}else{
return 0;
printf("%s\n", sp2->id);
if (strcmp(sp2->id, "1") == 0){
world->gameover = 1;
}else if(strcmp(sp2->id, "2") == 0){
printf("Changement de sens");
}else if(strcmp(sp2->id, "z") == 0){
world->gameover = 1;
}
}
}
void init_walls(world_t *world){
world->nb_murs = 0;
world->murs = malloc(sizeof(sprite_t) * MAX_LENGTH*MAX_LINES);
......@@ -78,25 +70,13 @@ void init_walls(world_t *world){
for (int i = 0; i < world->nb_lines_murs; i++) {
for (int j = 0; j < MAX_LENGTH; j++) {
switch (txt[i][j])
{
case '1':
world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, (i*METEORITE_SIZE)-(METEORITE_SIZE*world->nb_lines_murs), METEORITE_SIZE, METEORITE_SIZE, 1);
world->nb_murs++;
break;
case '2':
world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, (i*METEORITE_SIZE)-(METEORITE_SIZE*world->nb_lines_murs), METEORITE_SIZE, METEORITE_SIZE, 2);
if (txt[i][j] != '0'){
world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, (i*METEORITE_SIZE)-(METEORITE_SIZE*world->nb_lines_murs), METEORITE_SIZE, METEORITE_SIZE, txt[i][j]);
world->nb_murs++;
break;
default:
break;
}
}
}
printf("aaaa");
}
void update_walls(world_t *world){
......
......@@ -59,7 +59,7 @@ void update_data(world_t *world);
int is_game_over(world_t *world);
/**
/**
* \brief La fonction initialise les données du monde du jeu
......@@ -74,17 +74,18 @@ void init_data(world_t * world);
* \param world les données du monde
*/
void clean_data(world_t * world);
/**
* \brief La fonction indique si le sprite est en collision avec un autre sprite
* \brief La fonction fais une action en fonction de la collision entre deux sprites
*
* \param sp1
* \param sp2
* \param world
* \param make_disappear
*
* \return int 0 si les sprites ne se touchent pas, 1 sinon
* \param sp1 Généralement le vaisseau
* \param sp2 Généralement la météorite
* \param world Le monde
* \param make_disappear Indique si l'on doit faire disparaître les sprites en cas de collision
*/
int handle_sprite_collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make_disappear);
void collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make_disappear);
/**
* \brief La fonction initialise les murs du jeu
......
No preview for this file type
......@@ -7,8 +7,7 @@
001001111100011100000110
001001111100011100000110
001001111100011100000110
001001111100011100000110
000000000000000000000000
001001111100011100000110
001001111100011100000110
001001111100011100000110
......@@ -45,5 +44,4 @@
001001111100011100000110
001001111100011100000110
001201111100011100000110
001001111100011100000110
001001111100011100000110
\ No newline at end of file
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment