feat: Made zombies killable

Code is almost normed I will make a function to handle sprites
interractions
This commit is contained in:
Theo Champion 2025-09-15 15:46:06 +02:00
parent 46b40540dc
commit 37bb56733e
6 changed files with 37 additions and 10 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/15 14:09:13 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:45:27 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,12 +34,29 @@
#include "sprites/move_sprites.h"
#include "hud/hud.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;
}
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->player.aimed_zombie)
{
data->player.aimed_zombie->health -= 10;
ft_printf("Shooting %p, now at %d HP\n", data->player.aimed_zombie,
data->player.aimed_zombie->health);
if (data->player.aimed_zombie->health <= 0)
kill_zombie(data, data->player.aimed_zombie);
}
else
ft_printf("Shoot!\n");
}
}
@ -56,7 +73,6 @@ int game_loop(t_cub3d_data *data)
move_sprites(data);
data->player.aimed_zombie = NULL;
raycaster(data, &ray);
ft_printf("Current aimed %p\n", data->player.aimed_zombie);
sprite_caster(data);
create_hud(data);
matrix_to_image(data);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/18 13:52:23 by kcolin #+# #+# */
/* Updated: 2025/09/09 14:08:10 by tchampio ### ########.fr */
/* Updated: 2025/09/15 14:36:47 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,6 +37,7 @@ void create_zombie(t_cub3d_data *data, double x, double y)
data->sprite_list[data->sprite_counter] = create_sprite(data,
"ressources/zombie.xpm", x, y);
data->sprite_list[data->sprite_counter]->sprite_type = ZOMBIE;
data->sprite_list[data->sprite_counter]->health = 250;
data->sprite_counter++;
}
@ -59,6 +60,7 @@ t_sprite *place_right_sprite(t_cub3d_data *data, char c, double x, double y)
{
sprite = create_sprite(data, ZOMBIE_TEX, x + 0.5, y + 0.5);
sprite->sprite_type = ZOMBIE;
sprite->health = 250;
}
if (c == 'D' || c == 'Q' || c == 'J')
sprite->sprite_type = PERK;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/18 13:22:50 by kcolin #+# #+# */
/* Updated: 2025/08/20 12:38:53 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:25:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "../map/collision.h"
#include "sprite.h"
#include <math.h>
#include <stdlib.h>
void make_move(t_cub3d_data *data, t_sprite *sprite)
{

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 12:59:44 by tchampio #+# #+# */
/* Updated: 2025/08/13 14:44:02 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:26:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,6 +18,7 @@
typedef enum e_sprite_type
{
ZOMBIE,
DEAD_ZOMBIE,
PERK,
BOX,
OTHER
@ -62,6 +63,7 @@ typedef struct s_sprite
int sprite_draw_start_x;
int sprite_draw_end_x;
t_sprite_type sprite_type;
int health;
} t_sprite;
#endif // SPRITE_H

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */
/* Updated: 2025/09/01 15:55:41 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:41:09 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "sort_sprites.h"
#include <math.h>
#include "../../libft/includes/libft.h"
#include "sprite.h"
static void calculate_pos_and_transform(t_cub3d_data *data, t_sprite *sprite,
int i)
@ -128,7 +129,9 @@ void sprite_caster(t_cub3d_data *data)
while (data->sprite_list[i] && i < MAX_SPRITES)
{
sprite_calculate_pos_and_dist(data, i);
render_sprites(data, i);
if (data->sprite_list[data->sprite_order[i]]->sprite_type
!= DEAD_ZOMBIE)
render_sprites(data, i);
i++;
}
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/18 13:05:31 by kcolin #+# #+# */
/* Updated: 2025/09/15 13:05:37 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:39:44 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,8 +53,11 @@ void destroy_sprites(t_cub3d_data *data)
return ;
while (data->sprite_list[sprite] && sprite < MAX_SPRITES)
{
mlx_destroy_image(data->mlx, data->sprite_list[sprite]->image->img);
free(data->sprite_list[sprite]->image);
if (data->sprite_list[sprite]->sprite_type != DEAD_ZOMBIE)
{
mlx_destroy_image(data->mlx, data->sprite_list[sprite]->image->img);
free(data->sprite_list[sprite]->image);
}
free(data->sprite_list[sprite]);
sprite++;
}