42_cub3d/move.c
2024-10-15 14:03:58 +02:00

69 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 13:27:10 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
static int move_forward(void)
{
double dirX, dirY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
if (worldMap[(int)((g_ray.posX + dirX * MOVE_SPEED))][(int)(g_ray.posY)] != 1)
g_ray.posX += dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY + dirY * MOVE_SPEED)] != 1)
g_ray.posY += dirY * MOVE_SPEED;
return(0);
}
static int move_backward(void)
{
double dirX, dirY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
if (worldMap[(int)(g_ray.posX - dirX * MOVE_SPEED)][(int)(g_ray.posY)] != 1)
g_ray.posX -= dirX * MOVE_SPEED;
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY - dirY * MOVE_SPEED)] != 1)
g_ray.posY -= dirY * MOVE_SPEED;
return(0);
}
static int move_right(void)
{
g_ray.rot += ROT_SPEED;
return (0);
}
static int move_left(void)
{
g_ray.rot -= ROT_SPEED;
return (0);
}
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); // TODO leaks?
}
// render the updated frame after key pressd
draw_screen();
return (0);
}