mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
100 lines
3.5 KiB
C
100 lines
3.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* move.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/08/05 12:53:06 by kcolin #+# #+# */
|
|
/* Updated: 2025/08/06 11:46:04 by kcolin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../cub3d_data.h"
|
|
#include "angle.h"
|
|
#include "../consts.h"
|
|
#include "../map/collision.h"
|
|
|
|
void move_player_forward(t_cub3d_data *data)
|
|
{
|
|
t_mapdata *map;
|
|
double next_x;
|
|
double next_y;
|
|
float movspeed;
|
|
|
|
movspeed = MOVEMENT_SPEED * data->delta;
|
|
map = data->map;
|
|
next_x = data->player.x + data->player.dir_x * movspeed;
|
|
next_y = data->player.y + data->player.dir_y * movspeed;
|
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
|
data->player.x += data->player.dir_x * movspeed;
|
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
|
data->player.y += data->player.dir_y * movspeed;
|
|
}
|
|
|
|
void move_player_backward(t_cub3d_data *data)
|
|
{
|
|
t_mapdata *map;
|
|
double next_x;
|
|
double next_y;
|
|
float movspeed;
|
|
|
|
movspeed = MOVEMENT_SPEED * data->delta;
|
|
map = data->map;
|
|
next_x = data->player.x - data->player.dir_x * movspeed;
|
|
next_y = data->player.y - data->player.dir_y * movspeed;
|
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
|
data->player.x -= data->player.dir_x * movspeed;
|
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
|
data->player.y -= data->player.dir_y * movspeed;
|
|
}
|
|
|
|
void move_player_strafe_left(t_cub3d_data *data)
|
|
{
|
|
t_mapdata *map;
|
|
double next_x;
|
|
double next_y;
|
|
float movspeed;
|
|
|
|
movspeed = MOVEMENT_SPEED * data->delta;
|
|
map = data->map;
|
|
next_x = data->player.x - data->player.plane_x * movspeed;
|
|
next_y = data->player.y - data->player.plane_y * movspeed;
|
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
|
data->player.x -= data->player.plane_x * movspeed;
|
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
|
data->player.y -= data->player.plane_y * movspeed;
|
|
}
|
|
|
|
void move_player_strafe_right(t_cub3d_data *data)
|
|
{
|
|
t_mapdata *map;
|
|
double next_x;
|
|
double next_y;
|
|
float movspeed;
|
|
|
|
movspeed = MOVEMENT_SPEED * data->delta;
|
|
map = data->map;
|
|
next_x = data->player.x + data->player.plane_x * movspeed;
|
|
next_y = data->player.y + data->player.plane_y * movspeed;
|
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
|
data->player.x += data->player.plane_x * movspeed;
|
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
|
data->player.y += data->player.plane_y * movspeed;
|
|
}
|
|
|
|
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);
|
|
}
|