80 lines
2.6 KiB
C
80 lines
2.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* manage_keys.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
|
|
/* Updated: 2023/06/15 13:00:31 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "game.h"
|
|
|
|
static int rotate(t_ply *ply, bool right)
|
|
{
|
|
double old_dir_x;
|
|
double old_dir_y;
|
|
double old_plane_x;
|
|
double old_plane_y;
|
|
double speed;
|
|
|
|
old_dir_x = ply->dir_x;
|
|
old_dir_y = ply->dir_y;
|
|
old_plane_x = ply->pla_x;
|
|
old_plane_y = ply->pla_y;
|
|
speed = ROTATIONSPEED * (M_1_PI / 180.0);
|
|
if (!right)
|
|
speed = -speed;
|
|
ply->dir_x = old_dir_x * cos(speed) - old_dir_y * sin(speed);
|
|
ply->dir_y = old_dir_x * sin(speed) + old_dir_y * cos(speed);
|
|
ply->pla_x = old_plane_x * cos(speed) - old_plane_y * sin(speed);
|
|
ply->pla_y = old_plane_x * sin(speed) + old_plane_y * cos(speed);
|
|
return (1);
|
|
}
|
|
|
|
static int movement(t_ply *ply, double x, double y)
|
|
{
|
|
ply->pos_x += x;
|
|
ply->pos_y += y;
|
|
return (1);
|
|
}
|
|
|
|
int manage_movement(t_game *game)
|
|
{
|
|
t_ply *ply;
|
|
int is_moving;
|
|
|
|
ply = &game->ply;
|
|
is_moving = 0;
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_W))
|
|
is_moving = movement(ply, (ply->dir_x * MOVESPEED),
|
|
(ply->dir_y * MOVESPEED));
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_S))
|
|
is_moving = movement(ply, -(ply->dir_x * MOVESPEED),
|
|
-(ply->dir_y * MOVESPEED));
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_D))
|
|
is_moving = movement(ply, (ply->pla_x * MOVESPEED),
|
|
(ply->pla_y * MOVESPEED));
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_A))
|
|
is_moving = movement(ply, -(ply->pla_x * MOVESPEED),
|
|
-(ply->pla_y * MOVESPEED));
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT)
|
|
|| mlx_is_key_down(game->mlx, MLX_KEY_L))
|
|
is_moving = rotate(ply, true);
|
|
else if (mlx_is_key_down(game->mlx, MLX_KEY_LEFT)
|
|
|| mlx_is_key_down(game->mlx, MLX_KEY_J))
|
|
is_moving = rotate(ply, false);
|
|
return (is_moving);
|
|
}
|
|
|
|
int manage_keys(t_game *game)
|
|
{
|
|
if (mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE))
|
|
mlx_close_window(game->mlx);
|
|
if (manage_movement(game))
|
|
return (1);
|
|
return (0);
|
|
}
|