2023-04-27 09:13:32 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* manage_keys.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
|
2023-05-04 08:42:39 -04:00
|
|
|
/* Updated: 2023/05/04 14:16:06 by erey-bet ### ########.fr */
|
2023-04-27 09:13:32 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
|
2023-05-03 11:48:36 -04:00
|
|
|
void rotate(t_ply *ply, bool right)
|
2023-04-27 09:13:32 -04:00
|
|
|
{
|
2023-05-03 11:48:36 -04:00
|
|
|
double old_dir_x;
|
2023-05-04 08:42:39 -04:00
|
|
|
double old_dir_y;
|
2023-05-03 11:48:36 -04:00
|
|
|
double old_plane_x;
|
2023-05-04 08:42:39 -04:00
|
|
|
double old_plane_y;
|
2023-05-03 11:48:36 -04:00
|
|
|
double speed;
|
|
|
|
|
|
|
|
old_dir_x = ply->dir_x;
|
2023-05-04 08:42:39 -04:00
|
|
|
old_dir_y = ply->dir_y;
|
2023-05-03 11:48:36 -04:00
|
|
|
old_plane_x = ply->pla_x;
|
2023-05-04 08:42:39 -04:00
|
|
|
old_plane_y = ply->pla_y;
|
2023-05-03 11:48:36 -04:00
|
|
|
speed = ROTATIONSPEED * (M_1_PI / 180.0);
|
|
|
|
if (right)
|
|
|
|
speed = -speed;
|
2023-05-04 08:42:39 -04:00
|
|
|
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);
|
2023-05-03 11:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void movement(t_game *game, t_ply *ply, double x, double y)
|
|
|
|
{
|
2023-05-04 08:42:39 -04:00
|
|
|
(void)game;
|
|
|
|
//if(game->map.map[(int)(ply->pos_x + x)][(int)ply->pos_y] == '0')
|
2023-05-03 11:48:36 -04:00
|
|
|
ply->pos_x += x;
|
2023-05-04 08:42:39 -04:00
|
|
|
//if(game->map.map[(int)ply->pos_x][(int)(ply->pos_y + y)] == '0')
|
2023-05-03 11:48:36 -04:00
|
|
|
ply->pos_y += y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int manage_movement(t_game *game, int key)
|
|
|
|
{
|
|
|
|
t_ply *ply;
|
|
|
|
|
|
|
|
ply = &game->ply;
|
2023-05-04 08:42:39 -04:00
|
|
|
printf("dir_x: %f, dir_y: %f\n", ply->dir_x, ply->dir_y);
|
2023-05-03 06:45:40 -04:00
|
|
|
if (key == MLX_KEY_W)
|
2023-05-04 08:42:39 -04:00
|
|
|
movement(game, ply, (ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
|
2023-05-02 09:39:16 -04:00
|
|
|
else if (key == MLX_KEY_S)
|
2023-05-03 11:48:36 -04:00
|
|
|
movement(game, ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
|
2023-05-02 09:39:16 -04:00
|
|
|
else if (key == MLX_KEY_D)
|
2023-05-04 08:42:39 -04:00
|
|
|
movement(game, ply, -(ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
|
2023-05-02 09:39:16 -04:00
|
|
|
else if (key == MLX_KEY_A)
|
2023-05-04 08:42:39 -04:00
|
|
|
movement(game, ply, (ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
|
|
|
|
else if (key == MLX_KEY_L)
|
|
|
|
rotate(ply, true);
|
|
|
|
else if (key == MLX_KEY_J)
|
2023-05-03 11:48:36 -04:00
|
|
|
rotate(ply, false);
|
2023-04-27 09:13:32 -04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int manage_keys(mlx_key_data_t keys, t_game *game)
|
|
|
|
{
|
|
|
|
int is_moving;
|
|
|
|
|
|
|
|
is_moving = 0;
|
2023-05-04 08:42:39 -04:00
|
|
|
if (keys.key == MLX_KEY_Q)
|
2023-04-27 09:13:32 -04:00
|
|
|
mlx_close_window(game->mlx);
|
2023-05-02 09:39:16 -04:00
|
|
|
else if (keys.key == MLX_KEY_W
|
|
|
|
|| keys.key == MLX_KEY_S
|
|
|
|
|| keys.key == MLX_KEY_D
|
2023-05-04 08:42:39 -04:00
|
|
|
|| keys.key == MLX_KEY_A
|
|
|
|
|| keys.key == MLX_KEY_ESCAPE
|
|
|
|
|| keys.key == MLX_KEY_L
|
|
|
|
|| keys.key == MLX_KEY_J )
|
2023-05-03 11:48:36 -04:00
|
|
|
is_moving = manage_movement(game, keys.key);
|
2023-04-27 09:13:32 -04:00
|
|
|
if (is_moving)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|