From 37b31d1e7062f226fd4082bcfc0c6da852e59a85 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 5 Nov 2023 03:22:41 +0100 Subject: [PATCH] jumping and crouching works fine now --- cub3d.h | 5 +++-- src/draw.c | 6 +++--- src/init.c | 5 +++-- src/keys.c | 20 ++++++++++++-------- src/raycast.c | 6 +++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cub3d.h b/cub3d.h index a4ae012..391eabf 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/04 04:36:12 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 02:59:11 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,7 +62,8 @@ typedef struct s_varlist double planey; double camerax; double cameray; - int hoffset; + int vaim; + int jump; double raydirx; double raydiry; double movespeed; diff --git a/src/draw.c b/src/draw.c index a166da8..f1f7dc9 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/04 05:33:09 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 03:05:46 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -90,7 +90,7 @@ void ft_drawsprites(t_varlist *vl) transformx = invdet * (vl->diry * spritex - vl->dirx * spritey); transformy = invdet * (-vl->planey * spritex + vl->planex * spritey); spritescreenx = (vl->w / 2) * (1 + transformx / transformy); - vmovescreen = vl->hoffset + 0 / transformy; + vmovescreen = vl->vaim + vl->jump / transformy; spriteheight = fabs((vl->h / transformy)); drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen; if (drawstarty < 0) @@ -182,7 +182,7 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) double step; step = 64.0 / vl->lineheight; double textpos; - textpos = (drawstart - vl->hoffset - vl->h / 2 + vl->lineheight / 2) * step; + textpos = (drawstart - vl->vaim - (vl->jump / vl->perpwalldist) - vl->h / 2 + vl->lineheight / 2) * step; while (y < drawend) { int texty; diff --git a/src/init.c b/src/init.c index 58da96d..654c096 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/04 04:08:23 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 02:59:50 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,7 +21,8 @@ t_varlist initvarlist(void) vl.sprite = ft_calloc(4096, 8); vl.zbuffer = ft_calloc(4096, 8); vl.spritecount = 0; - vl.hoffset = 0; + vl.vaim = 0; + vl.jump = 0; vl.treasure = 0; vl.tottreasure = 0; vl.enemies = 0; diff --git a/src/keys.c b/src/keys.c index 187ac48..3992ff7 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/11/04 03:51:16 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 03:15:39 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,10 +23,14 @@ void ft_movementkeys(t_varlist *vl) 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_SPACE)) - ft_putendl("jump"); + if (mlx_is_key_down(vl->mlx, MLX_KEY_SPACE) && vl->jump < 10) + vl->jump = 200; + else if (vl->jump > 0) + vl->jump = vl->jump - 150 * vl->movespeed; if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_CONTROL)) - ft_putendl("crouch"); + vl->jump = -200; + else if (vl->jump < 0) + vl->jump = vl->jump + 150 * vl->movespeed; if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) { if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) @@ -163,11 +167,11 @@ void cursorhook(double xpos, double ypos, void *param) } vl->oldmouseposx = xpos; if (ypos < vl->oldmouseposy) - if (vl->hoffset < 300) - vl->hoffset = vl->hoffset + 3; + if (vl->vaim < 300) + vl->vaim = vl->vaim + 3; if (ypos > vl->oldmouseposy) - if (vl->hoffset > -300) - vl->hoffset = vl->hoffset - 3; + if (vl->vaim > -300) + vl->vaim = vl->vaim - 3; vl->oldmouseposy = ypos; } diff --git a/src/raycast.c b/src/raycast.c index 2db7f16..31c3bdb 100644 --- a/src/raycast.c +++ b/src/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/11/04 00:09:58 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 03:03:31 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -71,10 +71,10 @@ void ft_raycast(t_varlist *vl) else vl->perpwalldist = (vl->sidedisty - vl->deltadisty); vl->lineheight = vl->h / vl->perpwalldist; - drawstart = -vl->lineheight / 2 + vl->h / 2 + vl->hoffset; + drawstart = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->perpwalldist); if (drawstart < 0) drawstart = 0; - drawend = vl->lineheight / 2 + vl->h / 2 + vl->hoffset; + drawend = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->perpwalldist); if (drawend >= vl->h) drawend = vl->h - 1; ft_drawline(x, vl, drawstart, drawend);