diff --git a/Makefile b/Makefile index 1d4dcb0..695f727 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ SOURCEFILES = \ src/draw/drawutils.c \ src/main.c \ src/map/checkers.c \ + src/map/collision.c \ src/map/forbidden_characters.c \ src/map/populate_map.c \ src/map/setters.c \ diff --git a/src/draw/drawutils.c b/src/draw/drawutils.c index 85d3537..c5950a3 100644 --- a/src/draw/drawutils.c +++ b/src/draw/drawutils.c @@ -6,16 +6,19 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */ -/* Updated: 2025/07/17 15:48:56 by kcolin ### ########.fr */ +/* Updated: 2025/08/06 11:43:12 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "img_data.h" +#include "../consts.h" void my_mlx_pixel_put(t_img_data *data, int x, int y, int color) { char *dst; + if (x < 0 || y < 0 || x >= HEIGHT || y >= WIDTH) + return ; dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8)); *(unsigned int *)dst = color; } diff --git a/src/map/collision.c b/src/map/collision.c new file mode 100644 index 0000000..cb84493 --- /dev/null +++ b/src/map/collision.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/08/06 11:25:05 by kcolin #+# #+# */ +/* Updated: 2025/08/06 11:49:00 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "collision.h" +#include "../../libft/includes/libft.h" + +static bool out_of_bounds(t_mapdata *data, int x, int y) +{ + if (x < 0 + || y < 0 + || x >= data->mapheight + || y > (int)ft_strlen(data->map[x])) + return (true); + return (false); +} + +bool blocks_movement(t_mapdata *data, int x, int y) +{ + if (out_of_bounds(data, x, y) || data->map[x][y] == '1') + return (true); + return (false); +} + +bool blocks_view(t_mapdata *data, int x, int y) +{ + if (out_of_bounds(data, x, y) || data->map[x][y] == '1') + return (true); + return (false); +} diff --git a/src/map/collision.h b/src/map/collision.h new file mode 100644 index 0000000..91d1eb4 --- /dev/null +++ b/src/map/collision.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/08/06 11:24:10 by kcolin #+# #+# */ +/* Updated: 2025/08/06 11:28:57 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLLISION_H +# define COLLISION_H + +# include "mapdata.h" + +bool blocks_movement(t_mapdata *data, int x, int y); +bool blocks_view(t_mapdata *data, int x, int y); + +#endif // COLLISION_H diff --git a/src/player/move.c b/src/player/move.c index 0119966..cb9afee 100644 --- a/src/player/move.c +++ b/src/player/move.c @@ -6,79 +6,80 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/08/05 12:53:06 by kcolin #+# #+# */ -/* Updated: 2025/08/05 12:53:06 by kcolin ### ########.fr */ +/* 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) { - char **map; + t_mapdata *map; double next_x; double next_y; float movspeed; movspeed = MOVEMENT_SPEED * data->delta; - map = data->map->map; + map = data->map; next_x = data->player.x + data->player.dir_x * movspeed; next_y = data->player.y + data->player.dir_y * movspeed; - if (map[(int)data->player.y][(int)next_x] == '0') + if (!blocks_movement(map, (int)data->player.y, (int)next_x)) data->player.x += data->player.dir_x * movspeed; - if (map[(int)next_y][(int)data->player.x] == '0') + 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) { - char **map; + t_mapdata *map; double next_x; double next_y; float movspeed; movspeed = MOVEMENT_SPEED * data->delta; - map = data->map->map; + map = data->map; next_x = data->player.x - data->player.dir_x * movspeed; next_y = data->player.y - data->player.dir_y * movspeed; - if (map[(int)data->player.y][(int)next_x] == '0') + if (!blocks_movement(map, (int)data->player.y, (int)next_x)) data->player.x -= data->player.dir_x * movspeed; - if (map[(int)next_y][(int)data->player.x] == '0') + 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) { - char **map; + t_mapdata *map; double next_x; double next_y; float movspeed; movspeed = MOVEMENT_SPEED * data->delta; - map = data->map->map; + map = data->map; next_x = data->player.x - data->player.plane_x * movspeed; next_y = data->player.y - data->player.plane_y * movspeed; - if (map[(int)data->player.y][(int)next_x] == '0') + if (!blocks_movement(map, (int)data->player.y, (int)next_x)) data->player.x -= data->player.plane_x * movspeed; - if (map[(int)next_y][(int)data->player.x] == '0') + 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) { - char **map; + t_mapdata *map; double next_x; double next_y; float movspeed; movspeed = MOVEMENT_SPEED * data->delta; - map = data->map->map; + map = data->map; next_x = data->player.x + data->player.plane_x * movspeed; next_y = data->player.y + data->player.plane_y * movspeed; - if (map[(int)data->player.y][(int)next_x] == '0') + if (!blocks_movement(map, (int)data->player.y, (int)next_x)) data->player.x += data->player.plane_x * movspeed; - if (map[(int)next_y][(int)data->player.x] == '0') + if (!blocks_movement(map, (int)next_y, (int)data->player.x)) data->player.y += data->player.plane_y * movspeed; } diff --git a/src/player/player.c b/src/player/player.c index d8fef99..3130add 100644 --- a/src/player/player.c +++ b/src/player/player.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* player.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/07/29 11:42:44 by tchampio #+# #+# */ -/* Updated: 2025/07/29 20:13:01 by tchampio ### ########.fr */ +/* Created: 2025/08/06 11:29:14 by kcolin #+# #+# */ +/* Updated: 2025/08/06 11:29:14 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,6 @@ void init_player(t_player *player, t_mapdata *map) dir = map->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/raycast/ray.c b/src/raycast/ray.c index 79b01c0..25ebaad 100644 --- a/src/raycast/ray.c +++ b/src/raycast/ray.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */ -/* Updated: 2025/07/31 11:58:02 by kcolin ### ########.fr */ +/* Updated: 2025/08/06 11:33:27 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "../cub3d_data.h" #include #include +#include "../map/collision.h" void init_ray(t_ray *ray, int pos_x, t_player *player) { @@ -53,7 +54,7 @@ void ray_calculate_step(t_ray *ray, t_player *player) } } -void calculate_wall_dist(t_ray *ray, char **map) +void calculate_wall_dist(t_ray *ray, t_mapdata *map) { while (true) { @@ -69,7 +70,7 @@ void calculate_wall_dist(t_ray *ray, char **map) ray->map_y += ray->step_y; ray->side = SOUTH; } - if (map[ray->map_y][ray->map_x] != '0') + if (blocks_view(map, ray->map_y, ray->map_x)) break ; } if (ray->side == NORTH) @@ -105,7 +106,7 @@ void raycaster(t_cub3d_data *data, t_ray *ray) { init_ray(ray, x, &data->player); ray_calculate_step(ray, &data->player); - calculate_wall_dist(ray, data->map->map); + calculate_wall_dist(ray, data->map); calculate_wall_height(ray, &data->player); render_walls(data, ray, x); x++;