Compare commits

..

4 commits

Author SHA1 Message Date
Theo Champion
37bb56733e feat: Made zombies killable
Code is almost normed I will make a function to handle sprites
interractions
2025-09-15 15:46:06 +02:00
Theo Champion
46b40540dc fix: Added a currently aimed zombie pointer to the player struct and improved zombie checker function 2025-09-15 14:14:50 +02:00
Theo Champion
50164e94bb dev: Added zombie_checker.c in the makefile 2025-09-15 14:14:02 +02:00
Theo Champion
27af850525 norme: Fixed norme errors 2025-09-15 13:11:59 +02:00
13 changed files with 110 additions and 28 deletions

View file

@ -32,6 +32,7 @@ SOURCEFILES = \
src/raycast/barricades.c \
src/raycast/ray.c \
src/raycast/walls.c \
src/raycast/zombie_checker.c \
src/renderer/render.c \
src/sprites/create_sprite.c \
src/sprites/move_sprites.c \

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/08 17:27:12 by tchampio #+# #+# */
/* Updated: 2025/09/10 15:59:15 by tchampio ### ########.fr */
/* Updated: 2025/09/15 13:06:07 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -84,5 +84,6 @@ void create_hud(t_cub3d_data *data)
draw_points(data);
draw_perks(data);
draw_round(data);
matrix_image_put(data, data->player.weapon.texture, WIDTH / 2, HEIGHT - 175);
matrix_image_put(data, data->player.weapon.texture,
WIDTH / 2, HEIGHT - 175);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/14 16:41:00 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");
}
}
@ -54,6 +71,7 @@ int game_loop(t_cub3d_data *data)
move_player(data);
handle_shooting(data);
move_sprites(data);
data->player.aimed_zombie = NULL;
raycaster(data, &ray);
sprite_caster(data);
create_hud(data);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */
/* Updated: 2025/09/10 15:51:55 by tchampio ### ########.fr */
/* Updated: 2025/09/15 13:18:49 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
# include "../map/mapdata.h"
# include "../draw/img_data.h"
# include "../sprites/sprite.h"
typedef struct s_vec2
{
@ -55,6 +56,7 @@ typedef struct s_player
t_perks perk_order[3];
t_vec2 movement;
t_weapon weapon;
t_sprite *aimed_zombie;
} t_player;
#endif // PLAYER_H

View file

@ -6,11 +6,12 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */
/* Updated: 2025/09/14 17:40:53 by tchampio ### ########.fr */
/* Updated: 2025/09/15 14:08:27 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "ray.h"
#include "zombie_checker.h"
#include "barricades.h"
#include "raycaster.h"
#include "../player/player.h"
@ -18,8 +19,8 @@
#include "../cub3d_data.h"
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include "../map/collision.h"
#include "../../libft/includes/libft.h"
void init_ray(t_ray *ray, int pos_x, t_player *player)
{
@ -58,21 +59,10 @@ void ray_calculate_step(t_ray *ray, t_player *player)
void calculate_wall_dist(t_ray *ray, int x, t_cub3d_data *data)
{
int i;
while (true)
{
if (x == WIDTH / 2)
{
i = 0;
while (i < data->sprite_counter)
{
if (data->sprite_list[i]->sprite_type == ZOMBIE && ray->map_x == (int)data->sprite_list[i]->x && ray->map_y == (int)data->sprite_list[i]->y)
{
ft_printf("Zombie found\n");
}
i++;
}
}
check_for_zombies(ray, data);
if (ray->side_dist_x < ray->side_dist_y)
{
ray->side_dist_x += ray->delta_dist_x;

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* zombie_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/15 13:08:48 by tchampio #+# #+# */
/* Updated: 2025/09/15 14:10:17 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "ray.h"
#include "../cub3d_data.h"
#include <stdlib.h>
void check_for_zombies(t_ray *ray, t_cub3d_data *data)
{
int i;
i = 0;
while (i < data->sprite_counter)
{
if (ray->map_x == (int)data->sprite_list[i]->x
&& ray->map_y == (int)data->sprite_list[i]->y)
{
if (data->sprite_list[i]->sprite_type == ZOMBIE)
{
data->player.aimed_zombie = data->sprite_list[i];
break ;
}
else
data->player.aimed_zombie = NULL;
}
i++;
}
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* zombie_checker.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/15 13:09:54 by tchampio #+# #+# */
/* Updated: 2025/09/15 13:10:44 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ZOMBIE_CHECKER_H
# define ZOMBIE_CHECKER_H
# include "ray.h"
# include "../cub3d_data.h"
void check_for_zombies(t_ray *ray, t_cub3d_data *data);
#endif // ZOMBIE_CHECKER_H

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/10 16:00:47 by tchampio ### ########.fr */
/* Updated: 2025/09/15 15:39:44 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,7 @@ void destroy_texture(t_cub3d_data *data, t_img_data *data_img)
void destroy_textures(t_cub3d_data *data)
{
int i;
if (data->no_texture)
mlx_destroy_image(data->mlx, data->no_texture->img);
free(data->no_texture);
@ -52,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++;
}

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:28:47 by tchampio #+# #+# */
/* Updated: 2025/09/10 15:51:42 by tchampio ### ########.fr */
/* Updated: 2025/09/15 13:05:14 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,6 @@
void init_cub3d_data(t_cub3d_data *data, char **argv);
t_img_data *load_single_texture(t_cub3d_data *data, char *path);
void init_player(t_cub3d_data *data, t_player *player, t_mapdata *map);
void init_player(t_cub3d_data *data, t_player *player, t_mapdata *map);
#endif // INITS_H