42_cub3d/move.c
2024-10-17 15:09:07 +02:00

80 lines
2.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/17 15:06:43 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include "input.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_forward(int factor)
{
t_point_double dir;
vector_from_rotation(&dir, g_map.player.rot, 1);
g_map.player.pos.x += dir.x * MOVE_SPEED * factor;
g_map.player.pos.y += dir.y * MOVE_SPEED * factor;
push_from_walls();
return (0);
}
static int rotate(int factor)
{
g_map.player.rot += PI / ROT_SPEED_DIVIDE_PI * factor;
return (0);
}
// TODO leaks when quit?
void move(void)
{
if (g_input_actions.up && !g_input_actions.down)
move_forward(+1);
if (g_input_actions.down && !g_input_actions.up)
move_forward(-1);
if (g_input_actions.right && !g_input_actions.left)
rotate(+1);
if (g_input_actions.left && !g_input_actions.right)
rotate(-1);
}