From 663de33fc01ce0fb41c1cc1bcfe198dc1ab0f579 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 29 Oct 2023 19:00:25 +0100 Subject: [PATCH] fixed segfault when walking into outer wall --- config.cub | 2 +- src/keys.c | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/config.cub b/config.cub index 7f92ffc..689bdcf 100644 --- a/config.cub +++ b/config.cub @@ -8,7 +8,7 @@ WE ./assets/wood.png 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1 B1 1 1 1 1 1 -1 K D K K 1 C D D X 1 +1 K D K K 1 C D D K K X 1 1 1 1 1 1 1 1 1 11111111111 11111111111 111111111111111111111 1 1111111111 1111111111 1 1B C B1 1 1 1 1 1 D K K 1 1 diff --git a/src/keys.c b/src/keys.c index 3ff2cab..e107225 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/10/29 17:35:38 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 18:57:04 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,17 +16,22 @@ void ft_movementkeys(t_varlist *vl) { double olddirx; double oldplanex; + double distance; + if (vl->side) + distance = vl->sidedisty - vl->deltadisty; + else + distance = vl->sidedistx - vl->deltadistx; if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) ft_putendl("shoot"); if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) { - if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0') + if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.5) { vl->posx += vl->dirx * vl->movespeed * vl->run; vl->posy += vl->diry * vl->movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0') + if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0' && distance > 0.5) { vl->posx += vl->dirx * vl->movespeed * vl->run; vl->posy += vl->diry * vl->movespeed * vl->run; @@ -34,12 +39,12 @@ void ft_movementkeys(t_varlist *vl) } if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) { - if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0') + if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.5) { vl->posx -= vl->diry * vl->movespeed * vl->run; vl->posy += vl->dirx * vl->movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0') + if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0' && distance > 0.5) { vl->posx -= vl->diry * vl->movespeed * vl->run; vl->posy += vl->dirx * vl->movespeed * vl->run; @@ -47,12 +52,12 @@ void ft_movementkeys(t_varlist *vl) } if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) { - if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0') + if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.5) { vl->posx -= vl->dirx * vl->movespeed * vl->run; vl->posy -= vl->diry * vl->movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0') + if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0' && distance > 0.5) { vl->posx -= vl->dirx * vl->movespeed * vl->run; vl->posy -= vl->diry * vl->movespeed * vl->run; @@ -60,12 +65,12 @@ void ft_movementkeys(t_varlist *vl) } if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) { - if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0') + if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.5) { vl->posx += vl->diry * vl->movespeed * vl->run; vl->posy -= vl->dirx * vl->movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0') + if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0' && distance > 0.5) { vl->posx += vl->diry * vl->movespeed * vl->run; vl->posy -= vl->dirx * vl->movespeed * vl->run;