dev: allocated weapon on the heap

This commit is contained in:
Theo Champion 2025-09-17 16:59:32 +02:00
parent 0b00f5608c
commit 4048f5d1e3
8 changed files with 68 additions and 45 deletions

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: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,