diff --git a/algo.h b/algo.h index 55be665..cec8e7b 100644 --- a/algo.h +++ b/algo.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/15 16:57:48 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 14:19:12 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ # define SCREEN_WIDTH 640 // px # define SCREEN_HEIGHT 480 // px # define FOV 0.66 // ? TODO unit +# define HITBOX 0.25 // cases. should be more than MOVE_SPEED extern void *g_mlx; extern void *g_win; diff --git a/move.c b/move.c index 00edc79..405faaf 100644 --- a/move.c +++ b/move.c @@ -6,29 +6,56 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */ -/* Updated: 2024/10/17 12:29:23 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 14:20:10 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" +static void push_from_wall(t_point_int c) +{ + if (map_get_case(&g_map, c.x, c.y)->wall == EMPTY) + return ; + if (g_map.player.pos.x < c.x + 1 + HITBOX && g_map.player.pos.x > c.x + 1) + g_map.player.pos.x = c.x + 1 + HITBOX; + if (g_map.player.pos.x > c.x - HITBOX && g_map.player.pos.x < c.x) + g_map.player.pos.x = c.x - HITBOX; + if (g_map.player.pos.y < c.y + 1 + HITBOX && g_map.player.pos.y > c.y + 1) + g_map.player.pos.y = c.y + 1 + HITBOX; + if (g_map.player.pos.y > c.y - HITBOX && g_map.player.pos.y < c.y) + g_map.player.pos.y = c.y - HITBOX; +} + +static void push_from_walls(void) +{ + t_point_int c; + t_point_int p; + + p.x = g_map.player.pos.x; + p.y = g_map.player.pos.y; + c.x = p.x - 1; + c.y = p.y; + push_from_wall(c); + c.x = p.x + 1; + c.y = p.y; + push_from_wall(c); + c.x = p.x; + c.y = p.y - 1; + push_from_wall(c); + c.x = p.x; + c.y = p.y + 1; + push_from_wall(c); +} + static int move(int factor) { t_point_double dir; vector_from_rotation(&dir, g_map.player.rot, 1); - if (map_get_case(&g_map, - (int)(g_map.player.pos.x + dir.x * MOVE_SPEED * factor), - (int)(g_map.player.pos.y) - )->wall == EMPTY - ) - g_map.player.pos.x += dir.x * MOVE_SPEED * factor; - if (map_get_case(&g_map, - (int)(g_map.player.pos.x), - (int)(g_map.player.pos.y + dir.y * MOVE_SPEED * factor) - )->wall == EMPTY - ) - g_map.player.pos.y += dir.y * MOVE_SPEED * factor; + g_map.player.pos.x += dir.x * MOVE_SPEED * factor; + g_map.player.pos.y += dir.y * MOVE_SPEED * factor; + push_from_walls(); + printf("%f %f\n", g_map.player.pos.x, g_map.player.pos.y); return (0); } diff --git a/testmaps/good_big.cub b/testmaps/good_big.cub new file mode 100644 index 0000000..c118d77 --- /dev/null +++ b/testmaps/good_big.cub @@ -0,0 +1,15 @@ +F 255,127,0 +EA eastimage +NO theimageforthenorthwall +C 0,2,67 +SO SOUTH!!!!!!1 +WE weeeee +111111111 +100000001 +100000001 +100000001 +1000N0001 +100000001 +100000001 +100000001 +111111111