diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/collision.c | 80 | ||||
| -rw-r--r-- | src/map_utils.c | 7 | ||||
| -rw-r--r-- | src/player_process.c | 32 |
3 files changed, 95 insertions, 24 deletions
diff --git a/src/collision.c b/src/collision.c index fee1452..a45f339 100644 --- a/src/collision.c +++ b/src/collision.c @@ -6,30 +6,80 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */ -/* Updated: 2024/05/14 12:26:02 by dkaiser ### ########.fr */ +/* Updated: 2024/05/14 14:59:13 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos, - t_vector b_size) +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile); + +/* + * Checks if a and b are colliding and returns the direction relative to a. + * Example: The top left edge of a and the bottom right of b overlap. + * In this case, check_collision() will return (UP | LEFT). + */ +int check_collision(t_vector a_pos, t_ivector a_size, t_vector b_pos, + t_ivector b_size) { - int result; + int result; - result = 0; + result = 0; if (a_pos.x < b_size.x + b_pos.x && a_pos.x + a_size.x > b_pos.x && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) - { - if (a_pos.x < b_pos.x) - result |= RIGHT; - if (a_pos.x > b_pos.x) - result |= LEFT; - if (a_pos.y < b_pos.y) - result |= DOWN; - if (a_pos.y > b_pos.y) - result |= UP; - } + { + if (a_pos.x < b_pos.x) + result |= RIGHT; + if (a_pos.x > b_pos.x) + result |= LEFT; + if (a_pos.y < b_pos.y) + result |= DOWN; + if (a_pos.y > b_pos.y) + result |= UP; + } return (result); } +int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) +{ + int result; + t_ivector a_tile; + t_ivector check_tile; + int x; + int y; + + result = 0; + a_tile = screen_to_grid_pos(a_pos, map->tile_size); + x = -1; + while (x <= 1) + { + y = -1; + while (y <= 1) + { + check_tile.x = a_tile.x + x; + check_tile.y = a_tile.y + y; + result |= check_wall_collision_with_cell(a_pos, a_size, map, + check_tile); + y++; + } + x++; + } + return (result); +} + +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile) +{ + t_vector wall_pos; + t_ivector wall_size; + + if (get_tile(map, tile.x, tile.y) == WALL) + { + wall_pos = grid_to_screen_pos(tile, map->tile_size); + wall_size.x = map->tile_size.x; + wall_size.y = map->tile_size.y; + return (check_collision(a_pos, a_size, wall_pos, wall_size)); + } + return (0); +} diff --git a/src/map_utils.c b/src/map_utils.c index 54577a8..82acaa9 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 13:19:34 by dkaiser #+# #+# */ -/* Updated: 2024/05/14 13:50:23 by dkaiser ### ########.fr */ +/* Updated: 2024/05/14 14:44:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,3 +29,8 @@ t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size) grid_pos.y = screen_pos.y / tile_size.y; return (grid_pos); } + +enum e_tile get_tile(t_tilemap *map, int x, int y) +{ + return map->tiles[y * map->grid_size.x + x]; +} diff --git a/src/player_process.c b/src/player_process.c index 8cabe9a..5fcf994 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,26 +6,42 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */ -/* Updated: 2024/05/14 12:44:17 by dkaiser ### ########.fr */ +/* Updated: 2024/05/14 15:12:28 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "ft_printf.h" #include "so_long.h" static t_vector get_direction_from_input(t_game *game); void player_process(t_game *game) { - t_player player; + t_player *player; + t_vector collision_pos; + int collision; - player = game->player; - player.direction = get_direction_from_input(game); - player.velocity.x = player.direction.x * PLAYER_MOVE_SPEED + player = &game->player; + player->direction = get_direction_from_input(game); + player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.velocity.y = player.direction.y * PLAYER_MOVE_SPEED + player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.position.x += player.velocity.x; - player.position.y += player.velocity.y; + + collision_pos.x = player->position.x + player->velocity.x; + collision_pos.y = player->position.y + player->velocity.y; + collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); + if (collision & LEFT && player->velocity.x < 0) + player->velocity.x = 0; + if (collision & RIGHT && player->velocity.x > 0) + player->velocity.x = 0; + if (collision & UP && player->velocity.y < 0) + player->velocity.y = 0; + if (collision & DOWN && player->velocity.y > 0) + player->velocity.y = 0; + + player->position.x += player->velocity.x; + player->position.y += player->velocity.y; } static t_vector get_direction_from_input(t_game *game) |
