Compare commits

..

3 commits

Author SHA1 Message Date
Theo Champion
43f1ab6090 norme: Fixed norme issues 2025-09-18 12:52:18 +02:00
Theo Champion
82664c5d13 fix: replaced magic values for their variables counterparts 2025-09-18 12:30:41 +02:00
Theo Champion
66c3bda4df dev: Added a registry for weapons
The registry is for storing weapons structs as they are closer to actual
objects than simple data.
2025-09-18 12:25:10 +02:00
12 changed files with 253 additions and 75 deletions

View file

@ -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 \

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/09/16 15:13:28 by tchampio ### ########.fr */
/* Updated: 2025/09/18 12:51:43 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];

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/17 16:55:39 by tchampio ### ########.fr */
/* Updated: 2025/09/18 12:51:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,74 +33,34 @@
#include "utils/time.h"
#include "sprites/move_sprites.h"
#include "hud/hud.h"
#include "player/weapons.h"
void kill_zombie(t_cub3d_data *data, t_sprite *zombie_ptr)
void handle_player_sprites(t_cub3d_data *data)
{
mlx_destroy_image(data->mlx, zombie_ptr->image->img);
free(zombie_ptr->image);
zombie_ptr->sprite_type = DEAD_ZOMBIE;
data->player.points += 60;
}
void handle_shooting(t_cub3d_data *data)
{
if (data->keypresses.is_space_pressed)
{
if (!data->player.weapon->is_auto)
data->keypresses.is_space_pressed = false;
if (data->last_since_shoot != 0 && data->player.weapon->is_auto)
{
if (get_milliseconds() - data->last_since_shoot < 50000)
return ;
}
if (data->player.weapon->clip <= 0 && data->player.weapon->remaining_ammos > 0)
{
ft_printf("reloading\n");
if (data->player.weapon->remaining_ammos < 8)
{
data->player.weapon->clip = data->player.weapon->remaining_ammos;
data->player.weapon->remaining_ammos = 0;
}
else
{
data->player.weapon->clip = 8;
data->player.weapon->remaining_ammos -= 8;
}
}
if (data->player.weapon->clip <= 0)
return ;
data->player.weapon->is_shooting = true;
data->last_since_shoot = get_milliseconds();
data->player.weapon->clip--;
if (data->player.aimed_zombie)
{
data->player.aimed_zombie->health -= 32;
data->player.points += 10;
ft_printf("Shooting %p, now at %d HP %d/%d\n", data->player.aimed_zombie,
data->player.aimed_zombie->health, data->player.weapon->clip, data->player.weapon->remaining_ammos);
if (data->player.aimed_zombie->health <= 0)
kill_zombie(data, data->player.aimed_zombie);
}
else
ft_printf("Shoot! %d/%d\n", data->player.weapon->clip, data->player.weapon->remaining_ammos);
}
}
int game_loop(t_cub3d_data *data)
{
t_ray ray;
int fps;
char fps_string[4];
data->last_tick = get_milliseconds();
reset_matrix(data);
move_player(data);
handle_shooting(data);
move_sprites(data);
data->player.aimed_zombie = NULL;
}
void casters(t_cub3d_data *data)
{
t_ray ray;
reset_matrix(data);
raycaster(data, &ray);
sprite_caster(data);
create_hud(data);
}
int game_loop(t_cub3d_data *data)
{
int fps;
char fps_string[4];
data->last_tick = get_milliseconds();
handle_player_sprites(data);
casters(data);
if (data->player.weapon->is_shooting)
{
if (get_milliseconds() - data->last_since_shoot > 7000)

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* register_weapons.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 11:38:37 by tchampio #+# #+# */
/* Updated: 2025/09/18 12:36:19 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,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* register_weapons.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 11:53:17 by tchampio #+# #+# */
/* Updated: 2025/09/18 12:35:49 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

86
src/player/weapons.c Normal file
View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* weapons.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 12:48:49 by tchampio #+# #+# */
/* Updated: 2025/09/18 12:50:08 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../utils/time.h"
#include "../../mlx/mlx.h"
#include "../../libft/includes/libft.h"
#include <stdlib.h>
void kill_zombie(t_cub3d_data *data, t_sprite *zombie_ptr)
{
mlx_destroy_image(data->mlx, zombie_ptr->image->img);
free(zombie_ptr->image);
zombie_ptr->sprite_type = DEAD_ZOMBIE;
data->player.points += 60;
}
void handle_clip(t_cub3d_data *data)
{
if (data->player.weapon->clip <= 0
&& data->player.weapon->remaining_ammos > 0)
{
ft_printf("reloading\n");
if (data->player.weapon->remaining_ammos
< data->player.weapon->base_clip)
{
data->player.weapon->clip
= data->player.weapon->remaining_ammos;
data->player.weapon->remaining_ammos = 0;
}
else
{
data->player.weapon->clip = data->player.weapon->base_clip;
data->player.weapon->remaining_ammos
-= data->player.weapon->base_clip;
}
}
}
void shoot(t_cub3d_data *data)
{
if (data->player.aimed_zombie)
{
data->player.aimed_zombie->health -= data->player.weapon->damages;
data->player.points += 10;
ft_printf("Shooting %p, now at %d HP %d/%d\n",
data->player.aimed_zombie,
data->player.aimed_zombie->health, data->player.weapon->clip,
data->player.weapon->remaining_ammos);
if (data->player.aimed_zombie->health <= 0)
kill_zombie(data, data->player.aimed_zombie);
}
else
ft_printf("Shoot! %d/%d\n", data->player.weapon->clip,
data->player.weapon->remaining_ammos);
}
void handle_shooting(t_cub3d_data *data)
{
if (data->keypresses.is_space_pressed)
{
if (!data->player.weapon->is_auto)
data->keypresses.is_space_pressed = false;
if (data->last_since_shoot != 0 && data->player.weapon->is_auto)
{
if (get_milliseconds() - data->last_since_shoot < 50000)
return ;
}
handle_clip(data);
if (data->player.weapon->clip <= 0)
return ;
data->player.weapon->is_shooting = true;
data->last_since_shoot = get_milliseconds();
data->player.weapon->clip--;
shoot(data);
}
}

20
src/player/weapons.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* weapons.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 12:50:43 by tchampio #+# #+# */
/* Updated: 2025/09/18 12:51:14 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WEAPONS_H
# define WEAPONS_H
# include "../cub3d_data.h"
void handle_shooting(t_cub3d_data *data);
#endif // WEAPONS_H

View file

@ -6,12 +6,13 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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++;
}
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,13 +6,14 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/09/10 15:56:38 by tchampio ### ########.fr */
/* Updated: 2025/09/18 12:37:57 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,20 @@ 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 +106,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);