dev: Added a registry for weapons

The registry is for storing weapons structs as they are closer to actual
objects than simple data.
This commit is contained in:
Theo Champion 2025-09-18 12:10:25 +02:00
parent 4048f5d1e3
commit 66c3bda4df
9 changed files with 117 additions and 15 deletions

View file

@ -29,6 +29,7 @@ SOURCEFILES = \
src/player/player.c \ src/player/player.c \
src/player/move.c \ src/player/move.c \
src/player/move_step.c \ src/player/move_step.c \
src/player/register_weapons.c \
src/raycast/barricades.c \ src/raycast/barricades.c \
src/raycast/ray.c \ src/raycast/ray.c \
src/raycast/walls.c \ src/raycast/walls.c \

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 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 "map/mapdata.h"
# include "draw/img_data.h" # include "draw/img_data.h"
#include "player/weapon.h"
# include "sprites/sprite.h" # include "sprites/sprite.h"
# include "utils/keypresses.h" # include "utils/keypresses.h"
# include "consts.h" # include "consts.h"
@ -43,6 +44,7 @@ typedef struct s_cub3d_data
int delta; int delta;
int last_tick; int last_tick;
t_sprite **sprite_list; t_sprite **sprite_list;
t_weapon **weaponsregistry;
int sprite_counter; int sprite_counter;
double zbuffer[WIDTH]; double zbuffer[WIDTH];
int sprite_order[MAX_SPRITES]; int sprite_order[MAX_SPRITES];

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/06 11:29:14 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->health = 100;
player->points = 500; player->points = 500;
ft_bzero(player->perk_order, 3); ft_bzero(player->perk_order, 3);
player->weapon = ft_calloc(sizeof(t_weapon), 1); player->weapon = data->weaponsregistry[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");
if (dir == 'N' || dir == 'S') if (dir == 'N' || dir == 'S')
init_lon(player, dir); init_lon(player, dir);
else else

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* register_weapons.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* register_weapons.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/17 16:50:52 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 *texture;
t_img_data *shoot_texture; t_img_data *shoot_texture;
char name[255];
int damages;
bool is_auto; bool is_auto;
bool is_shooting; bool is_shooting;
bool reloading; bool reloading;

View file

@ -6,12 +6,13 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/18 13:05:31 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 "../cub3d_data.h"
#include "../../mlx/mlx.h" #include "../../mlx/mlx.h"
#include "../../libft/includes/libft.h"
#include <stdlib.h> #include <stdlib.h>
void destroy_texture(t_cub3d_data *data, t_img_data *data_img) 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; i = 0;
while (i < 3) while (i < 3)
destroy_texture(data, data->perk_logos[i++]); destroy_texture(data, data->perk_logos[i++]);
destroy_texture(data, data->player.weapon->texture); i = 0;
destroy_texture(data, data->player.weapon->shoot_texture); while (i < 15 && data->weaponsregistry[i])
{
destroy_texture(data, data->weaponsregistry[i]->texture);
destroy_texture(data, data->weaponsregistry[i]->shoot_texture);
i++;
}
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 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 destroy(t_cub3d_data *data, int exit_code)
{ {
int i;
free_map(data->map); free_map(data->map);
if (data->mlx_win) if (data->mlx_win)
mlx_destroy_window(data->mlx, 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->sprite_list);
free(data->mlx); free(data->mlx);
free(data->screen_matrix); free(data->screen_matrix);
free(data->player.weapon); i = 0;
while (i < 15)
free(data->weaponsregistry[i++]);
free(data->weaponsregistry);
exit(exit_code); exit(exit_code);
return (0); return (0);
} }

View file

@ -6,13 +6,14 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 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 "inits.h"
#include "../cub3d_data.h" #include "../cub3d_data.h"
#include "../player/init_player.h" #include "../player/init_player.h"
#include "../player/register_weapons.h"
#include "../../libft/includes/libft.h" #include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h" #include "../../mlx/mlx.h"
#include "../map/map_checker.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) void init_cub3d_data(t_cub3d_data *data, char **argv)
{ {
ft_bzero(data, sizeof(*data)); 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->addr = mlx_get_data_addr(data->img_data->img,
&data->img_data->bits_per_pixel, &data->img_data->line_length, &data->img_data->bits_per_pixel, &data->img_data->line_length,
&data->img_data->endian); &data->img_data->endian);
data->weaponsregistry = ft_calloc(sizeof(t_weapon *), 15);
register_weapons(data);
init_player(data, &data->player, data->map); init_player(data, &data->player, data->map);
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
load_textures(data); load_textures(data);