Programmation en C/SDL2
Gestion des collisions provoquées par le changement des terrains
Dans les fichiers map.h et map.c nous avons rajouté une fonction qui renvoie 0 si le terrain est inaccessible et 1 si le terrain est accessible. Son prototype dans map.h est le suivant:
int Map_access_ground(Map *this, int pos_x, int pos_y);
Dans la carte correspondant à l'ensemble des tuiles, on s'est débrouillé pour que le code ASCII des terrains correspondants à l'eau soit supérieur à la lettre 'f'. Voici le code source dans map.c :
int Map_access_ground(Map *this, int pos_x, int pos_y) {
int div_x = (int) pos_x/SIZE_BMP_MAP;
int div_y = (int) pos_y/SIZE_BMP_MAP;
if ( this->arrayMap[div_x][div_y] > 'f' ) {
return 0;
} else {
return 1;
}
}
Nous avons ensuite modifié la fonction Scene_move_entity(Scene *this, Direction direction) du fichier scene.c
Voici le nouveau code de la fonction :
void Scene_move_entity(Scene *this, Direction direction) {
int stepX, stepY;
int new_x, new_y;
switch (direction) {
case W:
stepX = -SIZE_BMP_HERO/4;
stepY = 0;
new_x = this->hero->pos.x - SIZE_BMP_MAP;
new_y = this->hero->pos.y;
break;
case N:
stepX = 0;
stepY = -SIZE_BMP_HERO/4;
new_x = this->hero->pos.x ;
new_y = this->hero->pos.y - SIZE_BMP_MAP;
break;
case E:
stepX = SIZE_BMP_HERO/4;
stepY = 0;
new_x = this->hero->pos.x + SIZE_BMP_MAP;
new_y = this->hero->pos.y;
break;
case S:
stepX = 0;
stepY = SIZE_BMP_HERO/4;
new_x = this->hero->pos.x ;
new_y = this->hero->pos.y + SIZE_BMP_MAP;
break;
}
// Verify if future position is accessible
int can_move = Map_access_ground(this->map, new_x, new_y);
if ( can_move == 1 ) {
display_map_sdl(this->map, this->renderer);
display_hero_sdl(this->hero, this->renderer, stepX, stepY, direction, POS1);
SDL_Delay(TIME);
display_map_sdl(this->map, this->renderer);
display_hero_sdl(this->hero, this->renderer, stepX, stepY, direction, POS0);
display_map_sdl(this->map, this->renderer);
display_hero_sdl(this->hero, this->renderer, stepX, stepY, direction, POS1);
SDL_Delay(TIME);
display_map_sdl(this->map, this->renderer);
display_hero_sdl(this->hero, this->renderer, stepX, stepY, direction, POS0);
}//end if can_move
}//end Scene_move_entity
Capture d'écran
Téléchargement
À noter que la version 03 du map_editor en python, dont le code a été revu et amélioré par Axel, est également inclu dans les sources.
j27.tar.gz