summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collision.c105
-rw-r--r--src/init.c10
-rw-r--r--src/loop.c12
-rw-r--r--src/map_utils.c36
-rw-r--r--src/player_process.c41
5 files changed, 190 insertions, 14 deletions
diff --git a/src/collision.c b/src/collision.c
new file mode 100644
index 0000000..d21e321
--- /dev/null
+++ b/src/collision.c
@@ -0,0 +1,105 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* collision.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */
+/* Updated: 2024/05/15 12:06:55 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+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;
+
+ 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;
+ }
+ 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);
+}
+
+void move_and_slide(t_player *player, t_tilemap *map)
+{
+ t_vector move_pos;
+
+ move_pos.x = player->position.x + player->velocity.x;
+ move_pos.y = player->position.y;
+
+ if ((check_wall_collision(move_pos, player->size, map) & (RIGHT | LEFT)) == 0)
+ player->position.x = move_pos.x;
+ else
+ player->velocity.x = 0;
+
+ move_pos.x = player->position.x;
+ move_pos.y = player->position.y + player->velocity.y;
+ if ((check_wall_collision(move_pos, player->size, map) & (UP | DOWN)) == 0)
+ player->position.y = move_pos.y;
+ else
+ player->velocity.y = 0;
+}
diff --git a/src/init.c b/src/init.c
index 5277b5c..9959da6 100644
--- a/src/init.c
+++ b/src/init.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */
-/* Updated: 2024/05/11 16:23:54 by dkaiser ### ########.fr */
+/* Updated: 2024/05/15 12:13:03 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,12 +40,14 @@ static void init_player(t_game *game)
player = &game->player;
texture = mlx_load_png("textures/player.png");
- player->position.x = game->map.player_start_tile.x * 48;
- player->position.y = game->map.player_start_tile.y * 48;
+ player->position.x = game->map.player_start_tile.x * game->map.grid_size.x;
+ player->position.y = game->map.player_start_tile.y * game->map.grid_size.y;
player->velocity.x = 0;
player->velocity.y = 0;
+ player->size.x = 44;
+ player->size.y = 44;
player->img = mlx_texture_to_image(game->mlx, texture);
- mlx_resize_image(player->img, 48, 48);
+ mlx_resize_image(player->img, player->size.x, player->size.y);
mlx_image_to_window(game->mlx, player->img, player->position.x,
player->position.y);
}
diff --git a/src/loop.c b/src/loop.c
index 62b5ed9..c4f95b3 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -6,11 +6,10 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 13:18:51 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 12:45:26 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "so_long.h"
void loop(void *params)
@@ -18,13 +17,6 @@ void loop(void *params)
t_game *game;
game = (t_game *)params;
- game->player.direction.x = ((game->input_direction & RIGHT) != 0)
- - ((game->input_direction & LEFT) != 0);
- game->player.direction.y = ((game->input_direction & DOWN) != 0)
- - ((game->input_direction & UP) != 0);
- game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED
- * game->mlx->delta_time;
- game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED
- * game->mlx->delta_time;
+ player_process(game);
draw(game);
}
diff --git a/src/map_utils.c b/src/map_utils.c
new file mode 100644
index 0000000..82acaa9
--- /dev/null
+++ b/src/map_utils.c
@@ -0,0 +1,36 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* map_utils.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/14 13:19:34 by dkaiser #+# #+# */
+/* Updated: 2024/05/14 14:44:39 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size)
+{
+ t_vector screen_pos;
+
+ screen_pos.x = grid_pos.x * tile_size.x;
+ screen_pos.y = grid_pos.y * tile_size.y;
+ return (screen_pos);
+}
+
+t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size)
+{
+ t_ivector grid_pos;
+
+ grid_pos.x = screen_pos.x / tile_size.x;
+ 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
new file mode 100644
index 0000000..52277f8
--- /dev/null
+++ b/src/player_process.c
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* player_process.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
+/* Updated: 2024/05/15 12:03:52 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;
+
+ 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
+ * game->mlx->delta_time;
+
+ move_and_slide(player, &game->map);
+}
+
+static t_vector get_direction_from_input(t_game *game)
+{
+ t_vector result;
+
+ result.x = ((game->input_direction & RIGHT) != 0)
+ - ((game->input_direction & LEFT) != 0);
+ result.y = ((game->input_direction & DOWN) != 0)
+ - ((game->input_direction & UP) != 0);
+ return (result);
+}