42_cub3d/move.c
2024-10-15 16:03:18 +02:00

78 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/15 16:01:08 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
static int move_forward(void)
{
t_point_double dir;
vector_from_rotation(&dir, g_ray.rot, 1);
if (g_world_map[(int)((g_ray.pos.x + dir.x * MOVE_SPEED))]
[(int)(g_ray.pos.y)] != 1
)
g_ray.pos.x += dir.x * MOVE_SPEED;
if (g_world_map[(int)(g_ray.pos.x)]
[(int)(g_ray.pos.y + dir.y * MOVE_SPEED)] != 1
)
g_ray.pos.y += dir.y * MOVE_SPEED;
return (0);
}
static int move_backward(void)
{
t_point_double dir;
vector_from_rotation(&dir, g_ray.rot, 1);
if (g_world_map[(int)(g_ray.pos.x - dir.x * MOVE_SPEED)]
[(int)(g_ray.pos.y)] != 1
)
g_ray.pos.x -= dir.x * MOVE_SPEED;
if (g_world_map[(int)(g_ray.pos.x)]
[(int)(g_ray.pos.y - dir.y * MOVE_SPEED)] != 1
)
g_ray.pos.y -= dir.y * MOVE_SPEED;
return (0);
}
static int move_right(void)
{
g_ray.rot += PI / ROT_SPEED_DIVIDE_PI;
return (0);
}
static int move_left(void)
{
g_ray.rot -= PI / ROT_SPEED_DIVIDE_PI;
return (0);
}
// TODO leaks when quit?
int keypress(int keycode)
{
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
move_forward();
if (keycode == XK_Down || keycode == XK_s)
move_backward();
if (keycode == XK_Right || keycode == XK_d)
move_right();
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
move_left();
if (keycode == XK_Escape)
{
mlx_destroy_window(g_mlx, g_win);
exit(0);
}
draw_screen();
return (0);
}