From e6edad24c9fa56538d66067dcff6bde39f746239 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 May 2024 14:04:28 +0200 Subject: [PATCH] Add gravity and jumping --- include/so_long.h | 6 ++++-- src/collision.c | 16 ++++++++++++---- src/player_process.c | 14 +++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/so_long.h b/include/so_long.h index 97ef5c7..48c34aa 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser position.x + player->velocity.x; + move_pos.x = player->position.x + (player->velocity.x * delta_time); move_pos.y = player->position.y; if ((check_wall_collision(move_pos, player->size, map) & (RIGHT | LEFT)) == 0) @@ -97,9 +97,17 @@ void move_and_slide(t_player *player, t_tilemap *map) player->velocity.x = 0; move_pos.x = player->position.x; - move_pos.y = player->position.y + player->velocity.y; + move_pos.y = player->position.y + (player->velocity.y * delta_time); if ((check_wall_collision(move_pos, player->size, map) & (UP | DOWN)) == 0) player->position.y = move_pos.y; else player->velocity.y = 0; } + +int is_on_floor(t_vector pos, t_ivector size, t_tilemap *map) +{ + pos.y += 5; + if (check_wall_collision(pos, size, map)) + return (1); + return (0); +} diff --git a/src/player_process.c b/src/player_process.c index 52277f8..7e27530 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser 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 - * game->mlx->delta_time; - - move_and_slide(player, &game->map); + player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED; + player->velocity.y += 50; + if (player->direction.y == -1 && is_on_floor(player->position, player->size, + &game->map)) + player->velocity.y = -1000; + move_and_slide(player, &game->map, game->mlx->delta_time); } static t_vector get_direction_from_input(t_game *game) -- 2.47.2