diff --git a/src/cub3d_data.h b/src/cub3d_data.h index 22975f9..0e6b7a7 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/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 "draw/img_data.h" -#include "player/weapon.h" +# include "player/weapon.h" # include "sprites/sprite.h" # include "utils/keypresses.h" # include "consts.h" diff --git a/src/main.c b/src/main.c index 865b4bb..56a7f50 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* 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 "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 < 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); 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) diff --git a/src/player/register_weapons.c b/src/player/register_weapons.c index ac80642..ba2e54c 100644 --- a/src/player/register_weapons.c +++ b/src/player/register_weapons.c @@ -6,14 +6,15 @@ /* 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 "../../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) return ; @@ -22,7 +23,8 @@ void register_weapon_2(t_weapon *weapon, const char *name, bool is_auto, int dam 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; diff --git a/src/player/register_weapons.h b/src/player/register_weapons.h index 5b1eaf0..cb7934e 100644 --- a/src/player/register_weapons.h +++ b/src/player/register_weapons.h @@ -6,7 +6,7 @@ /* 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 "../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); +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/weapons.c b/src/player/weapons.c new file mode 100644 index 0000000..7fd50ec --- /dev/null +++ b/src/player/weapons.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* weapons.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); + } +} diff --git a/src/player/weapons.h b/src/player/weapons.h new file mode 100644 index 0000000..5621df0 --- /dev/null +++ b/src/player/weapons.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* weapons.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/src/utils/inits.c b/src/utils/inits.c index 5cbd646..865299c 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,7 +6,7 @@ /* 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) { - 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); - 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); }