norme: Fixed norme issues

This commit is contained in:
Theo Champion 2025-09-18 12:52:18 +02:00
parent 82664c5d13
commit 43f1ab6090
7 changed files with 147 additions and 71 deletions

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/18 12:01:18 by tchampio ### ########.fr */ /* Updated: 2025/09/18 12:51:43 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +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 "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"

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/18 12:28:33 by tchampio ### ########.fr */ /* Updated: 2025/09/18 12:51:25 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,74 +33,34 @@
#include "utils/time.h" #include "utils/time.h"
#include "sprites/move_sprites.h" #include "sprites/move_sprites.h"
#include "hud/hud.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 < 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;
}
}
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 -= 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);
}
}
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); move_player(data);
handle_shooting(data); handle_shooting(data);
move_sprites(data); move_sprites(data);
data->player.aimed_zombie = NULL; data->player.aimed_zombie = NULL;
}
void casters(t_cub3d_data *data)
{
t_ray ray;
reset_matrix(data);
raycaster(data, &ray); raycaster(data, &ray);
sprite_caster(data); sprite_caster(data);
create_hud(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 (data->player.weapon->is_shooting)
{ {
if (get_milliseconds() - data->last_since_shoot > 7000) if (get_milliseconds() - data->last_since_shoot > 7000)

View file

@ -6,14 +6,15 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 11:38:37 by tchampio #+# #+# */ /* Created: 2025/09/18 11:38:37 by tchampio #+# #+# */
/* Updated: 2025/09/18 11:50:14 by tchampio ### ########.fr */ /* Updated: 2025/09/18 12:36:19 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../cub3d_data.h" #include "../cub3d_data.h"
#include "../../libft/includes/libft.h" #include "../../libft/includes/libft.h"
void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int damages) void register_weapon_2(t_weapon *weapon, const char *name,
bool is_auto, int damages)
{ {
if (!weapon) if (!weapon)
return ; return ;
@ -22,7 +23,8 @@ void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int dam
weapon->damages = damages; weapon->damages = damages;
} }
t_weapon *register_weapon(t_img_data *texture, t_img_data *shoot_tex, int clip, int ammo) t_weapon *register_weapon(t_img_data *texture, t_img_data *shoot_tex,
int clip, int ammo)
{ {
t_weapon *weapon; t_weapon *weapon;

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 11:53:17 by tchampio #+# #+# */ /* Created: 2025/09/18 11:53:17 by tchampio #+# #+# */
/* Updated: 2025/09/18 11:54:15 by tchampio ### ########.fr */ /* Updated: 2025/09/18 12:35:49 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,10 @@
# include "weapon.h" # include "weapon.h"
# include "../cub3d_data.h" # include "../cub3d_data.h"
void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int damages); void register_weapon_2(t_weapon *weapon, const char *name,
t_weapon *register_weapon(t_img_data *texture, t_img_data *shoot_tex, int clip, int ammo); 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); t_weapon *get_weapon(const char *name, t_cub3d_data *data);
#endif // REGISTER_WEAPONS_H #endif // REGISTER_WEAPONS_H

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,7 +6,7 @@
/* 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/18 12:06:48 by tchampio ### ########.fr */ /* Updated: 2025/09/18 12:37:57 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -76,9 +76,15 @@ void place_base_sprites(t_cub3d_data *data, char **map)
void register_weapons(t_cub3d_data *data) 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); 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); 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); 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); register_weapon_2(data->weaponsregistry[1], "Galil", true, 32);
} }