feat: added angle support

This commit is contained in:
Theo Champion 2025-07-24 14:37:18 +02:00
parent 10b2d727de
commit bec6b27879
4 changed files with 41 additions and 13 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
/* Updated: 2025/07/17 14:57:09 by kcolin ### ########.fr */ /* Updated: 2025/07/24 14:17:52 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,8 @@
# define HEIGHT 800 # define HEIGHT 800
# define WIDTH 800 # define WIDTH 800
# define RESSOURCE_DIR "ressources" # define RESSOURCE_DIR "ressources"
# define MOVEMENT_SPEED 0.01 # define MOVEMENT_SPEED 0.1
# define ANGLE_SPEED 0.1
# ifdef BONUS # ifdef BONUS
# define COMPILED_TEXT "Compiled with bonuses" # define COMPILED_TEXT "Compiled with bonuses"
# else # else

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/07/22 12:50:14 by kcolin ### ########.fr */ /* Updated: 2025/07/24 14:31:07 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,18 +22,18 @@
#include <X11/X.h> #include <X11/X.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <math.h>
void init_player(t_mapdata *mapdata, t_player *player) void init_player(t_mapdata *mapdata, t_player *player)
{ {
player->health = 100; player->health = 100;
player->x = mapdata->startx; player->x = mapdata->startx;
player->y = mapdata->starty; player->y = mapdata->starty;
player->yaw = M_PI;
} }
int game_loop(t_cub3d_data *data) int game_loop(t_cub3d_data *data)
{ {
data->player.x += data->player.movement.x;
data->player.y += data->player.movement.y;
mlx_destroy_image(data->mlx, data->img_data->img); mlx_destroy_image(data->mlx, data->img_data->img);
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
draw_map(data->map, &data->player, data->img_data); draw_map(data->map, &data->player, data->img_data);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */ /* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */
/* Updated: 2025/07/17 15:51:42 by kcolin ### ########.fr */ /* Updated: 2025/07/24 14:07:09 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */
/* Updated: 2025/07/17 15:53:49 by kcolin ### ########.fr */ /* Updated: 2025/07/24 14:20:15 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,19 +14,46 @@
#include "frees.h" #include "frees.h"
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/X.h> #include <X11/X.h>
#include <math.h>
#include <stdio.h>
int keypress_handler(int keycode, t_cub3d_data *data) int keypress_handler(int keycode, t_cub3d_data *data)
{ {
double cos_angle;
double sin_angle;
cos_angle = cos(data->player.yaw);
sin_angle = sin(data->player.yaw);
if (keycode == XK_Escape) if (keycode == XK_Escape)
destroy(data); destroy(data);
if (keycode == XK_Left)
data->player.yaw += ANGLE_SPEED;
else if (keycode == XK_Right)
data->player.yaw -= ANGLE_SPEED;
if (data->player.yaw > 2 * M_PI)
data->player.yaw -= 2 * M_PI;
if (data->player.yaw < 0)
data->player.yaw += 2 * M_PI;
if (keycode == XK_a) if (keycode == XK_a)
data->player.movement.x = -MOVEMENT_SPEED; {
else if (keycode == XK_d) data->player.x += cos_angle * MOVEMENT_SPEED;
data->player.movement.x = MOVEMENT_SPEED; data->player.y -= sin_angle * MOVEMENT_SPEED;
}
if (keycode == XK_d)
{
data->player.x -= cos_angle * MOVEMENT_SPEED;
data->player.y += sin_angle * MOVEMENT_SPEED;
}
if (keycode == XK_w) if (keycode == XK_w)
data->player.movement.y = -MOVEMENT_SPEED; {
else if (keycode == XK_s) data->player.x += sin_angle * MOVEMENT_SPEED;
data->player.movement.y = MOVEMENT_SPEED; data->player.y += cos_angle * MOVEMENT_SPEED;
}
if (keycode == XK_s)
{
data->player.x -= sin_angle * MOVEMENT_SPEED;
data->player.y -= cos_angle * MOVEMENT_SPEED;
}
return (0); return (0);
} }