cub3d/src/player/move.c
2025-07-31 14:45:11 +02:00

91 lines
3.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:58:00 by tchampio #+# #+# */
/* Updated: 2025/07/31 14:43:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "angle.h"
#include "../consts.h"
void move_player_forward(t_cub3d_data *data)
{
char **map;
double next_x;
double next_y;
map = data->map->map;
next_x = data->player.x + data->player.dir_x * MOVEMENT_SPEED * data->delta;
next_y = data->player.y + data->player.dir_y * MOVEMENT_SPEED * data->delta;
if (map[(int)data->player.y][(int)next_x] == '0')
data->player.x += data->player.dir_x * MOVEMENT_SPEED * data->delta;
if (map[(int)next_y][(int)data->player.x] == '0')
data->player.y += data->player.dir_y * MOVEMENT_SPEED * data->delta;
}
void move_player_backward(t_cub3d_data *data)
{
char **map;
double next_x;
double next_y;
map = data->map->map;
next_x = data->player.x - data->player.dir_x * MOVEMENT_SPEED * data->delta;
next_y = data->player.y - data->player.dir_y * MOVEMENT_SPEED * data->delta;
if (map[(int)data->player.y][(int)next_x] == '0')
data->player.x -= data->player.dir_x * MOVEMENT_SPEED * data->delta;
if (map[(int)next_y][(int)data->player.x] == '0')
data->player.y -= data->player.dir_y * MOVEMENT_SPEED * data->delta;
}
void move_player_strafe_left(t_cub3d_data *data)
{
char **map;
double next_x;
double next_y;
map = data->map->map;
next_x = data->player.x - data->player.plane_x * MOVEMENT_SPEED * data->delta;
next_y = data->player.y - data->player.plane_y * MOVEMENT_SPEED * data->delta;
if (map[(int)data->player.y][(int)next_x] == '0')
data->player.x -= data->player.plane_x * MOVEMENT_SPEED * data->delta;
if (map[(int)next_y][(int)data->player.x] == '0')
data->player.y -= data->player.plane_y * MOVEMENT_SPEED * data->delta;
}
void move_player_strafe_right(t_cub3d_data *data)
{
char **map;
double next_x;
double next_y;
map = data->map->map;
next_x = data->player.x + data->player.plane_x * MOVEMENT_SPEED * data->delta;
next_y = data->player.y + data->player.plane_y * MOVEMENT_SPEED * data->delta;
if (map[(int)data->player.y][(int)next_x] == '0')
data->player.x += data->player.plane_x * MOVEMENT_SPEED * data->delta;
if (map[(int)next_y][(int)data->player.x] == '0')
data->player.y += data->player.plane_y * MOVEMENT_SPEED * data->delta;
}
void move_player(t_cub3d_data *data)
{
if (data->keypresses.is_w_pressed)
move_player_forward(data);
if (data->keypresses.is_s_pressed)
move_player_backward(data);
if (data->keypresses.is_a_pressed)
move_player_strafe_left(data);
if (data->keypresses.is_d_pressed)
move_player_strafe_right(data);
if (data->keypresses.is_left_pressed)
turn_left_angle(data);
if (data->keypresses.is_right_pressed)
turn_right_angle(data);
}