diff --git a/Makefile b/Makefile index 726bec9..5280891 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,8 @@ SOURCEFILES = \ src/utils/hooks.c \ src/map/map_checker.c \ src/player/angle.c \ + src/player/player.c \ + src/player/move.c \ OBJECTS = $(SOURCEFILES:.c=.o) NAME = cub3d diff --git a/src/consts.h b/src/consts.h index 59fd961..d9b1792 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/07/29 13:29:11 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 14:28:45 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ extern const float g_southwest; extern const float g_southeast; # define RESSOURCE_DIR "ressources" -# define MOVEMENT_SPEED 6.4 +# define MOVEMENT_SPEED 0.1 # define ROTATION_SPEED 0.1 # define PLANE_VALUE 0.66 # ifdef BONUS diff --git a/src/cub3d_data.h b/src/cub3d_data.h index e1ad270..28c3859 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:26:32 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:43:40 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ # include "map/mapdata.h" # include "draw/img_data.h" # include "player/player.h" -#include "utils/hooks.h" +# include "utils/keypresses.h" typedef struct s_cub3d_data { diff --git a/src/draw/draw_map.c b/src/draw/draw_map.c index b174e80..ca0740f 100644 --- a/src/draw/draw_map.c +++ b/src/draw/draw_map.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:20:00 by kcolin #+# #+# */ -/* Updated: 2025/07/29 11:58:34 by kcolin ### ########.fr */ +/* Updated: 2025/07/29 14:16:07 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,5 @@ void draw_map(t_mapdata *map, t_player *player, t_img_data *data) } i++; } - draw_2d_wall(0x00FF0000, data, MAP_SIZE * ((player->x - (float)SIZE / 2)) / SIZE, MAP_SIZE * ((player->y - (float)SIZE / 2) / SIZE)); + draw_2d_wall(0x00FF0000, data, MAP_SIZE * player->x, MAP_SIZE * player->y); } diff --git a/src/main.c b/src/main.c index 79eb323..77d1693 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/29 13:28:35 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 14:00:36 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include "consts.h" #include "cub3d_data.h" #include "draw/drawutils.h" +#include "player/move.h" #include "map/map_checker.h" #include "draw/draw_map.h" #include "map/mapdata.h" @@ -57,21 +58,21 @@ bool touch(float px, float py, t_mapdata *map) // } // } -t_cardinal_dir angle_cardinal_dir(float angle) -{ - if (angle >= g_southwest || (angle > g_south && angle < g_southeast)) - return (SOUTH); - if (angle >= g_southeast && angle < g_northeast) - return (EAST); - if (angle >= g_northeast && angle < g_northwest) - return (NORTH); - return (WEST); -} - -t_cardinal_dir player_cardinal_dir(t_player player) -{ - return (angle_cardinal_dir(player.yaw)); -} +// t_cardinal_dir angle_cardinal_dir(float angle) +// { +// if (angle >= g_southwest || (angle > g_south && angle < g_southeast)) +// return (SOUTH); +// if (angle >= g_southeast && angle < g_northeast) +// return (EAST); +// if (angle >= g_northeast && angle < g_northwest) +// return (NORTH); +// return (WEST); +// } +// +// t_cardinal_dir player_cardinal_dir(t_player player) +// { +// return (angle_cardinal_dir(player.yaw)); +// } t_intercardinal_dir angle_intercardinal_dir(float angle) { @@ -84,10 +85,10 @@ t_intercardinal_dir angle_intercardinal_dir(float angle) return (SOUTHWEST); } -t_intercardinal_dir player_intercardinal_dir(t_player player) -{ - return (angle_intercardinal_dir(player.yaw)); -} +// t_intercardinal_dir player_intercardinal_dir(t_player player) +// { +// return (angle_intercardinal_dir(player.yaw)); +// } /* ** Converts a coordinate from global map space to local tile space. @@ -104,6 +105,7 @@ int game_loop(t_cub3d_data *data) { mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + move_player(data); // if (player_intercardinal_dir(data->player) == SOUTHEAST) // ft_printf("southeast "); diff --git a/src/map/map_checker.c b/src/map/map_checker.c index 65205f5..e4abafd 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ -/* Updated: 2025/07/23 13:33:05 by kcolin ### ########.fr */ +/* Updated: 2025/07/29 14:43:32 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/player/move.c b/src/player/move.c new file mode 100644 index 0000000..e1cadd6 --- /dev/null +++ b/src/player/move.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio // bad + +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; + next_y = data->player.y + data->player.dir_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x += data->player.dir_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y += data->player.dir_y * MOVEMENT_SPEED; +} + +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; + next_y = data->player.y - data->player.dir_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x -= data->player.dir_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y -= data->player.dir_y * MOVEMENT_SPEED; +} + +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; + next_y = data->player.y - data->player.plane_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x -= data->player.plane_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y -= data->player.plane_y * MOVEMENT_SPEED; +} + +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; + next_y = data->player.y + data->player.plane_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x += data->player.plane_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y += data->player.plane_y * MOVEMENT_SPEED; +} + +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); +} diff --git a/src/player/move.h b/src/player/move.h new file mode 100644 index 0000000..14e9490 --- /dev/null +++ b/src/player/move.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio map[map->starty][map->startx]; player->x = map->startx + 0.5; player->y = map->starty + 0.5; + map->map[map->starty][map->startx] = '0'; player->health = 100; player->points = 500; if (dir == 'N' || dir == 'S') diff --git a/src/utils/hooks.c b/src/utils/hooks.c index dd940a2..9ec9356 100644 --- a/src/utils/hooks.c +++ b/src/utils/hooks.c @@ -6,11 +6,12 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:28:54 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:35:29 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "../consts.h" +#include "../cub3d_data.h" #include "../player/angle.h" #include "frees.h" #include diff --git a/src/utils/hooks.h b/src/utils/hooks.h index fee72ef..c6b78af 100644 --- a/src/utils/hooks.h +++ b/src/utils/hooks.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:23:06 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:26:04 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:42:22 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,6 @@ # include "../cub3d_data.h" -typedef struct s_keypresses -{ - bool is_w_pressed; - bool is_a_pressed; - bool is_s_pressed; - bool is_d_pressed; - bool is_left_pressed; - bool is_right_pressed; -} t_keypresses; - int keypress_handler(int keycode, t_cub3d_data *data); int keyrelease_handler(int keycode, t_cub3d_data *data);