Collisions are now completely working, just need to fill the array of all tiles where the player can't go through.

This commit is contained in:
mibi88 2023-07-09 15:45:46 +02:00
parent 6afde39af8
commit 86430b853a
2 changed files with 13 additions and 7 deletions

View file

@ -78,8 +78,8 @@ void render_map(Player *player, Map *map_level) {
ytile = (tile / map_level->tileset_size) * T_HEIGHT;
/* render */
dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my,
map_level->tileset, xtile, ytile, T_WIDTH, T_HEIGHT,
DIMAGE_NONE);
map_level->tileset, xtile, ytile, T_WIDTH,
T_HEIGHT, DIMAGE_NONE);
}
}
}

View file

@ -43,7 +43,8 @@ void player_move(Map *map_level, Player *player, Direction direction) {
/* How this player movement will modify the player x and y. */
const char dx = one_px_mov[direction*2]*SPEED;
const char dy = one_px_mov[direction*2+1]*SPEED;
/* If the player will collide with a hard tile. */
/* If the player will collide with a hard tile or if the will go outside of
* the map. */
if(player_collision(map_level, player, direction, P_CENTER)){
/* If the will collide with the center of the player. */
player_fix_position(player, dx, dy);
@ -79,11 +80,16 @@ bool player_collision(Map *map_level, Player *player, Direction direction,
}else if(!dy){
dy += nomov_axis_check;
}
dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER));
dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER));
dx = dx*(P_WIDTH/2+1);
dy = dy*(P_HEIGHT/2+1);
/* The tile he will go to. */
int player_tile_x = (player->x+dx)/T_WIDTH;
int player_tile_y = (player->y+dy)/T_HEIGHT;
int player_tile_x = player->x+dx;
int player_tile_y = player->y+dy;
/* Handle a negative position diffrently than a positive one. */
if(player_tile_x < 0) player_tile_x = player_tile_x/T_WIDTH-1;
else player_tile_x = player_tile_x/T_WIDTH;
if(player_tile_y < 0) player_tile_y = player_tile_y/T_HEIGHT-1;
else player_tile_y = player_tile_y/T_HEIGHT;
for(i=0;i<map_level->nblayers;i++){
/* if he's on a hard tile */
if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT,