diff --git a/Makefile b/Makefile index 8982a27..f96c6e0 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ SOURCEFILES = \ src/player/player.c \ src/player/move.c \ src/player/move_step.c \ + src/player/register_weapons.c \ src/raycast/barricades.c \ src/raycast/ray.c \ src/raycast/walls.c \ diff --git a/src/cub3d_data.h b/src/cub3d_data.h index 83cd37f..22975f9 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/09/16 15:13:28 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 12:01:18 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # include "map/mapdata.h" # include "draw/img_data.h" +#include "player/weapon.h" # include "sprites/sprite.h" # include "utils/keypresses.h" # include "consts.h" @@ -43,6 +44,7 @@ typedef struct s_cub3d_data int delta; int last_tick; t_sprite **sprite_list; + t_weapon **weaponsregistry; int sprite_counter; double zbuffer[WIDTH]; int sprite_order[MAX_SPRITES]; diff --git a/src/player/player.c b/src/player/player.c index 5b85e3f..5051761 100644 --- a/src/player/player.c +++ b/src/player/player.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/08/06 11:29:14 by kcolin #+# #+# */ -/* Updated: 2025/09/17 16:56:50 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 12:06:58 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,12 +64,7 @@ void init_player(t_cub3d_data *data, t_player *player, t_mapdata *map) player->health = 100; player->points = 500; ft_bzero(player->perk_order, 3); - player->weapon = ft_calloc(sizeof(t_weapon), 1); - player->weapon->clip = 8; - player->weapon->is_auto = true; - player->weapon->remaining_ammos = 35; - player->weapon->texture = load_hud_texture(data, "ressources/weapon.xpm"); - player->weapon->shoot_texture = load_hud_texture(data, "ressources/weapon_shooting.xpm"); + player->weapon = data->weaponsregistry[1]; if (dir == 'N' || dir == 'S') init_lon(player, dir); else diff --git a/src/player/register_weapons.c b/src/player/register_weapons.c new file mode 100644 index 0000000..ac80642 --- /dev/null +++ b/src/player/register_weapons.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* register_weapons.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/18 11:38:37 by tchampio #+# #+# */ +/* Updated: 2025/09/18 11:50:14 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d_data.h" +#include "../../libft/includes/libft.h" + +void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int damages) +{ + if (!weapon) + return ; + ft_strlcpy(weapon->name, name, 255); + weapon->is_auto = is_auto; + weapon->damages = damages; +} + +t_weapon *register_weapon(t_img_data *texture, t_img_data *shoot_tex, int clip, int ammo) +{ + t_weapon *weapon; + + weapon = ft_calloc(sizeof(*weapon), 1); + if (!weapon) + return (NULL); + weapon->texture = texture; + weapon->shoot_texture = shoot_tex; + weapon->base_clip = clip; + weapon->clip = clip; + weapon->base_ammos = ammo; + weapon->remaining_ammos = ammo; + return (weapon); +} + +t_weapon *get_weapon(const char *name, t_weapon **weapons) +{ + t_weapon *weapon; + int i; + + if (!weapons) + return (NULL); + i = 0; + weapon = weapons[i]; + while (weapon) + { + if (ft_strncmp(weapon->name, name, ft_strlen(name) + 1) == 0) + return (weapon); + weapon = weapons[++i]; + } + return (NULL); +} diff --git a/src/player/register_weapons.h b/src/player/register_weapons.h new file mode 100644 index 0000000..5b1eaf0 --- /dev/null +++ b/src/player/register_weapons.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* register_weapons.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/18 11:53:17 by tchampio #+# #+# */ +/* Updated: 2025/09/18 11:54:15 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef REGISTER_WEAPONS_H +# define REGISTER_WEAPONS_H + +# include "weapon.h" +# include "../cub3d_data.h" + +void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int damages); +t_weapon *register_weapon(t_img_data *texture, t_img_data *shoot_tex, int clip, int ammo); +t_weapon *get_weapon(const char *name, t_cub3d_data *data); + +#endif // REGISTER_WEAPONS_H diff --git a/src/player/weapon.h b/src/player/weapon.h index 0317e4b..9062490 100644 --- a/src/player/weapon.h +++ b/src/player/weapon.h @@ -6,7 +6,7 @@ /* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/09/17 16:50:52 by tchampio #+# #+# */ -/* Updated: 2025/09/17 16:52:29 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 11:54:22 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,8 @@ typedef struct s_weapon { t_img_data *texture; t_img_data *shoot_texture; + char name[255]; + int damages; bool is_auto; bool is_shooting; bool reloading; diff --git a/src/utils/destroy_utils.c b/src/utils/destroy_utils.c index 341d5ff..3cdbdf4 100644 --- a/src/utils/destroy_utils.c +++ b/src/utils/destroy_utils.c @@ -6,12 +6,13 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/08/18 13:05:31 by kcolin #+# #+# */ -/* Updated: 2025/09/17 16:54:35 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 12:23:05 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "../cub3d_data.h" #include "../../mlx/mlx.h" +#include "../../libft/includes/libft.h" #include void destroy_texture(t_cub3d_data *data, t_img_data *data_img) @@ -79,6 +80,11 @@ void destroy_hud_textures(t_cub3d_data *data) i = 0; while (i < 3) destroy_texture(data, data->perk_logos[i++]); - destroy_texture(data, data->player.weapon->texture); - destroy_texture(data, data->player.weapon->shoot_texture); + i = 0; + while (i < 15 && data->weaponsregistry[i]) + { + destroy_texture(data, data->weaponsregistry[i]->texture); + destroy_texture(data, data->weaponsregistry[i]->shoot_texture); + i++; + } } diff --git a/src/utils/frees.c b/src/utils/frees.c index 1d5fa68..f8783f5 100644 --- a/src/utils/frees.c +++ b/src/utils/frees.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */ -/* Updated: 2025/09/17 16:57:32 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 12:24:13 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,8 @@ void free_map(t_mapdata *map) int destroy(t_cub3d_data *data, int exit_code) { + int i; + free_map(data->map); if (data->mlx_win) mlx_destroy_window(data->mlx, data->mlx_win); @@ -75,7 +77,10 @@ int destroy(t_cub3d_data *data, int exit_code) free(data->sprite_list); free(data->mlx); free(data->screen_matrix); - free(data->player.weapon); + i = 0; + while (i < 15) + free(data->weaponsregistry[i++]); + free(data->weaponsregistry); exit(exit_code); return (0); } diff --git a/src/utils/inits.c b/src/utils/inits.c index 2b8b486..5cbd646 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,13 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ -/* Updated: 2025/09/10 15:56:38 by tchampio ### ########.fr */ +/* Updated: 2025/09/18 12:06:48 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "inits.h" #include "../cub3d_data.h" #include "../player/init_player.h" +#include "../player/register_weapons.h" #include "../../libft/includes/libft.h" #include "../../mlx/mlx.h" #include "../map/map_checker.h" @@ -73,6 +74,14 @@ void place_base_sprites(t_cub3d_data *data, char **map) } } +void register_weapons(t_cub3d_data *data) +{ + data->weaponsregistry[0] = register_weapon(load_hud_texture(data, "ressources/weapon.xpm"), load_hud_texture(data, "ressources/weapon_shooting.xpm"), 8, 80); + register_weapon_2(data->weaponsregistry[0], "M1911", false, 32); + data->weaponsregistry[1] = register_weapon(load_hud_texture(data, "ressources/weapon.xpm"), load_hud_texture(data, "ressources/weapon_shooting.xpm"), 32, 800); + register_weapon_2(data->weaponsregistry[1], "Galil", true, 32); +} + void init_cub3d_data(t_cub3d_data *data, char **argv) { ft_bzero(data, sizeof(*data)); @@ -91,6 +100,8 @@ void init_cub3d_data(t_cub3d_data *data, char **argv) data->img_data->addr = mlx_get_data_addr(data->img_data->img, &data->img_data->bits_per_pixel, &data->img_data->line_length, &data->img_data->endian); + data->weaponsregistry = ft_calloc(sizeof(t_weapon *), 15); + register_weapons(data); init_player(data, &data->player, data->map); data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); load_textures(data);