From 784962c79d4076c0f01634c458e366615c3026b2 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 17 Sep 2025 12:52:30 +0200 Subject: [PATCH 1/4] feat: Added a cooldown for auto weapons --- src/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index c053bd9..3199ab7 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/16 16:07:09 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 12:51:34 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,6 +50,11 @@ void handle_shooting(t_cub3d_data *data) data->keypresses.is_space_pressed = false; if (data->player.weapon.clip <= 0) return ; + if (data->player.weapon.is_auto) + { + if (get_milliseconds() - data->last_since_shoot < 50000) + return ; + } data->player.weapon.is_shooting = true; data->last_since_shoot = get_milliseconds(); data->player.weapon.clip--; From c339bb733492b2ebee82a7dfb8bed9002a7594f8 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 17 Sep 2025 16:05:41 +0200 Subject: [PATCH 2/4] fix: Fixed a bug preventing auto shoot --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 3199ab7..bbe76bc 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/17 12:51:34 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 13:36:22 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ void handle_shooting(t_cub3d_data *data) data->keypresses.is_space_pressed = false; if (data->player.weapon.clip <= 0) return ; - if (data->player.weapon.is_auto) + if (data->last_since_shoot != 0 && data->player.weapon.is_auto) { if (get_milliseconds() - data->last_since_shoot < 50000) return ; From 0b00f5608c8f1dac8ebf2579531ccf0cb6d3e91d Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 17 Sep 2025 16:46:47 +0200 Subject: [PATCH 3/4] feat: Added a cheat for ammos --- src/main.c | 14 +++++++------- src/utils/hooks.c | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index bbe76bc..045fa6c 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/17 13:36:22 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:20:34 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,17 +48,12 @@ void handle_shooting(t_cub3d_data *data) { if (!data->player.weapon.is_auto) data->keypresses.is_space_pressed = false; - if (data->player.weapon.clip <= 0) - return ; if (data->last_since_shoot != 0 && data->player.weapon.is_auto) { if (get_milliseconds() - data->last_since_shoot < 50000) return ; } - data->player.weapon.is_shooting = true; - data->last_since_shoot = get_milliseconds(); - data->player.weapon.clip--; - if (data->player.weapon.clip == 0) + if (data->player.weapon.clip <= 0 && data->player.weapon.remaining_ammos > 0) { ft_printf("reloading\n"); if (data->player.weapon.remaining_ammos < 8) @@ -72,6 +67,11 @@ void handle_shooting(t_cub3d_data *data) 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; diff --git a/src/utils/hooks.c b/src/utils/hooks.c index 3be647e..4638dea 100644 --- a/src/utils/hooks.c +++ b/src/utils/hooks.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ -/* Updated: 2025/09/14 15:57:54 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:12:06 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,8 @@ int keypress_handler(int keycode, t_cub3d_data *data) data->player.points += 500; if (keycode == XK_u) data->round++; + if (keycode == XK_m) + data->player.weapon.remaining_ammos = 1337; return (0); } From 4048f5d1e3e8f06e2831d1203810e7c9a0643795 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 17 Sep 2025 16:59:32 +0200 Subject: [PATCH 4/4] dev: allocated weapon on the heap --- src/hud/hud.c | 8 ++++---- src/main.c | 32 ++++++++++++++++---------------- src/player/player.c | 13 +++++++------ src/player/player.h | 16 +++------------- src/player/weapon.h | 31 +++++++++++++++++++++++++++++++ src/utils/destroy_utils.c | 6 +++--- src/utils/frees.c | 3 ++- src/utils/hooks.c | 4 ++-- 8 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 src/player/weapon.h diff --git a/src/hud/hud.c b/src/hud/hud.c index a67a813..c8debef 100644 --- a/src/hud/hud.c +++ b/src/hud/hud.c @@ -6,7 +6,7 @@ /* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/09/08 17:27:12 by tchampio #+# #+# */ -/* Updated: 2025/09/16 15:03:09 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:55:05 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,11 +84,11 @@ void create_hud(t_cub3d_data *data) draw_points(data); draw_perks(data); draw_round(data); - if (data->player.weapon.is_shooting) - matrix_image_put(data, data->player.weapon.shoot_texture, + if (data->player.weapon->is_shooting) + matrix_image_put(data, data->player.weapon->shoot_texture, WIDTH / 2, HEIGHT - 175); else - matrix_image_put(data, data->player.weapon.texture, + matrix_image_put(data, data->player.weapon->texture, WIDTH / 2, HEIGHT - 175); matrix_set(data, WIDTH / 2, HEIGHT / 2, 0x0000FF00); } diff --git a/src/main.c b/src/main.c index 045fa6c..3224d56 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/17 16:20:34 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:55:39 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,43 +46,43 @@ void handle_shooting(t_cub3d_data *data) { if (data->keypresses.is_space_pressed) { - if (!data->player.weapon.is_auto) + if (!data->player.weapon->is_auto) data->keypresses.is_space_pressed = false; - if (data->last_since_shoot != 0 && data->player.weapon.is_auto) + 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) + if (data->player.weapon->clip <= 0 && data->player.weapon->remaining_ammos > 0) { ft_printf("reloading\n"); - if (data->player.weapon.remaining_ammos < 8) + if (data->player.weapon->remaining_ammos < 8) { - data->player.weapon.clip = data->player.weapon.remaining_ammos; - data->player.weapon.remaining_ammos = 0; + 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; + data->player.weapon->clip = 8; + data->player.weapon->remaining_ammos -= 8; } } - if (data->player.weapon.clip <= 0) + if (data->player.weapon->clip <= 0) return ; - data->player.weapon.is_shooting = true; + data->player.weapon->is_shooting = true; data->last_since_shoot = get_milliseconds(); - data->player.weapon.clip--; + 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); + 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); + ft_printf("Shoot! %d/%d\n", data->player.weapon->clip, data->player.weapon->remaining_ammos); } } @@ -101,10 +101,10 @@ int game_loop(t_cub3d_data *data) raycaster(data, &ray); sprite_caster(data); create_hud(data); - if (data->player.weapon.is_shooting) + if (data->player.weapon->is_shooting) { if (get_milliseconds() - data->last_since_shoot > 7000) - data->player.weapon.is_shooting = false; + data->player.weapon->is_shooting = false; } matrix_to_image(data); mlx_put_image_to_window(data->mlx, data->mlx_win, diff --git a/src/player/player.c b/src/player/player.c index e2db712..5b85e3f 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/16 16:06:46 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:56:50 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,11 +64,12 @@ 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); - ft_bzero(&player->weapon, sizeof(t_weapon)); - player->weapon.clip = 8; - 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 = 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"); if (dir == 'N' || dir == 'S') init_lon(player, dir); else diff --git a/src/player/player.h b/src/player/player.h index 3bc6bea..0ace102 100644 --- a/src/player/player.h +++ b/src/player/player.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */ -/* Updated: 2025/09/16 15:01:18 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:52:41 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # include "../map/mapdata.h" # include "../draw/img_data.h" # include "../sprites/sprite.h" +# include "weapon.h" typedef struct s_vec2 { @@ -31,17 +32,6 @@ typedef enum e_perks DOUBLETAP } t_perks; -typedef struct s_weapon -{ - t_img_data *texture; - t_img_data *shoot_texture; - bool is_auto; - bool is_shooting; - bool reloading; - int clip; - int remaining_ammos; -} t_weapon; - typedef struct s_player { double x; @@ -57,7 +47,7 @@ typedef struct s_player bool has_doubletap; t_perks perk_order[3]; t_vec2 movement; - t_weapon weapon; + t_weapon *weapon; t_sprite *aimed_zombie; } t_player; diff --git a/src/player/weapon.h b/src/player/weapon.h new file mode 100644 index 0000000..0317e4b --- /dev/null +++ b/src/player/weapon.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* weapon.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:50:52 by tchampio #+# #+# */ +/* Updated: 2025/09/17 16:52:29 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WEAPON_H +# define WEAPON_H + +# include "../draw/img_data.h" + +typedef struct s_weapon +{ + t_img_data *texture; + t_img_data *shoot_texture; + bool is_auto; + bool is_shooting; + bool reloading; + int clip; + int remaining_ammos; + int base_clip; + int base_ammos; +} t_weapon; + +#endif // WEAPON_H diff --git a/src/utils/destroy_utils.c b/src/utils/destroy_utils.c index 2d22873..341d5ff 100644 --- a/src/utils/destroy_utils.c +++ b/src/utils/destroy_utils.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/08/18 13:05:31 by kcolin #+# #+# */ -/* Updated: 2025/09/16 16:08:40 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:54:35 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,6 +79,6 @@ 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); + destroy_texture(data, data->player.weapon->texture); + destroy_texture(data, data->player.weapon->shoot_texture); } diff --git a/src/utils/frees.c b/src/utils/frees.c index ab6a19a..1d5fa68 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/08 14:15:58 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:57:32 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,6 +75,7 @@ int destroy(t_cub3d_data *data, int exit_code) free(data->sprite_list); free(data->mlx); free(data->screen_matrix); + free(data->player.weapon); exit(exit_code); return (0); } diff --git a/src/utils/hooks.c b/src/utils/hooks.c index 4638dea..278a96f 100644 --- a/src/utils/hooks.c +++ b/src/utils/hooks.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ -/* Updated: 2025/09/17 16:12:06 by tchampio ### ########.fr */ +/* Updated: 2025/09/17 16:55:52 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int keypress_handler(int keycode, t_cub3d_data *data) if (keycode == XK_u) data->round++; if (keycode == XK_m) - data->player.weapon.remaining_ammos = 1337; + data->player.weapon->remaining_ammos = 1337; return (0); }