Compare commits

...

4 commits

Author SHA1 Message Date
Theo Champion
4048f5d1e3 dev: allocated weapon on the heap 2025-09-17 16:59:32 +02:00
Theo Champion
0b00f5608c feat: Added a cheat for ammos 2025-09-17 16:46:47 +02:00
Theo Champion
c339bb7334 fix: Fixed a bug preventing auto shoot 2025-09-17 16:05:41 +02:00
Theo Champion
784962c79d feat: Added a cooldown for auto weapons 2025-09-17 12:52:30 +02:00
8 changed files with 75 additions and 45 deletions

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/08 17:27:12 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_points(data);
draw_perks(data); draw_perks(data);
draw_round(data); draw_round(data);
if (data->player.weapon.is_shooting) if (data->player.weapon->is_shooting)
matrix_image_put(data, data->player.weapon.shoot_texture, matrix_image_put(data, data->player.weapon->shoot_texture,
WIDTH / 2, HEIGHT - 175); WIDTH / 2, HEIGHT - 175);
else else
matrix_image_put(data, data->player.weapon.texture, matrix_image_put(data, data->player.weapon->texture,
WIDTH / 2, HEIGHT - 175); WIDTH / 2, HEIGHT - 175);
matrix_set(data, WIDTH / 2, HEIGHT / 2, 0x0000FF00); matrix_set(data, WIDTH / 2, HEIGHT / 2, 0x0000FF00);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 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 16:55:39 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,38 +46,43 @@ void handle_shooting(t_cub3d_data *data)
{ {
if (data->keypresses.is_space_pressed) if (data->keypresses.is_space_pressed)
{ {
if (!data->player.weapon.is_auto) if (!data->player.weapon->is_auto)
data->keypresses.is_space_pressed = false; data->keypresses.is_space_pressed = false;
if (data->player.weapon.clip <= 0) if (data->last_since_shoot != 0 && data->player.weapon->is_auto)
{
if (get_milliseconds() - data->last_since_shoot < 50000)
return ; return ;
data->player.weapon.is_shooting = true; }
data->last_since_shoot = get_milliseconds(); if (data->player.weapon->clip <= 0 && data->player.weapon->remaining_ammos > 0)
data->player.weapon.clip--;
if (data->player.weapon.clip == 0)
{ {
ft_printf("reloading\n"); 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->clip = data->player.weapon->remaining_ammos;
data->player.weapon.remaining_ammos = 0; data->player.weapon->remaining_ammos = 0;
} }
else else
{ {
data->player.weapon.clip = 8; data->player.weapon->clip = 8;
data->player.weapon.remaining_ammos -= 8; 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) if (data->player.aimed_zombie)
{ {
data->player.aimed_zombie->health -= 32; data->player.aimed_zombie->health -= 32;
data->player.points += 10; data->player.points += 10;
ft_printf("Shooting %p, now at %d HP %d/%d\n", data->player.aimed_zombie, 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) if (data->player.aimed_zombie->health <= 0)
kill_zombie(data, data->player.aimed_zombie); kill_zombie(data, data->player.aimed_zombie);
} }
else 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);
} }
} }
@ -96,10 +101,10 @@ int game_loop(t_cub3d_data *data)
raycaster(data, &ray); raycaster(data, &ray);
sprite_caster(data); sprite_caster(data);
create_hud(data); create_hud(data);
if (data->player.weapon.is_shooting) if (data->player.weapon->is_shooting)
{ {
if (get_milliseconds() - data->last_since_shoot > 7000) if (get_milliseconds() - data->last_since_shoot > 7000)
data->player.weapon.is_shooting = false; data->player.weapon->is_shooting = false;
} }
matrix_to_image(data); matrix_to_image(data);
mlx_put_image_to_window(data->mlx, data->mlx_win, mlx_put_image_to_window(data->mlx, data->mlx_win,

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/06 11:29:14 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->health = 100;
player->points = 500; player->points = 500;
ft_bzero(player->perk_order, 3); ft_bzero(player->perk_order, 3);
ft_bzero(&player->weapon, sizeof(t_weapon)); player->weapon = ft_calloc(sizeof(t_weapon), 1);
player->weapon.clip = 8; player->weapon->clip = 8;
player->weapon.remaining_ammos = 35; player->weapon->is_auto = true;
player->weapon.texture = load_hud_texture(data, "ressources/weapon.xpm"); player->weapon->remaining_ammos = 35;
player->weapon.shoot_texture = load_hud_texture(data, "ressources/weapon_shooting.xpm"); 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') if (dir == 'N' || dir == 'S')
init_lon(player, dir); init_lon(player, dir);
else else

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 15:51:29 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 "../map/mapdata.h"
# include "../draw/img_data.h" # include "../draw/img_data.h"
# include "../sprites/sprite.h" # include "../sprites/sprite.h"
# include "weapon.h"
typedef struct s_vec2 typedef struct s_vec2
{ {
@ -31,17 +32,6 @@ typedef enum e_perks
DOUBLETAP DOUBLETAP
} t_perks; } 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 typedef struct s_player
{ {
double x; double x;
@ -57,7 +47,7 @@ typedef struct s_player
bool has_doubletap; bool has_doubletap;
t_perks perk_order[3]; t_perks perk_order[3];
t_vec2 movement; t_vec2 movement;
t_weapon weapon; t_weapon *weapon;
t_sprite *aimed_zombie; t_sprite *aimed_zombie;
} t_player; } t_player;

31
src/player/weapon.h Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* weapon.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/18 13:05:31 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; i = 0;
while (i < 3) while (i < 3)
destroy_texture(data, data->perk_logos[i++]); destroy_texture(data, data->perk_logos[i++]);
destroy_texture(data, data->player.weapon.texture); destroy_texture(data, data->player.weapon->texture);
destroy_texture(data, data->player.weapon.shoot_texture); destroy_texture(data, data->player.weapon->shoot_texture);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 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->sprite_list);
free(data->mlx); free(data->mlx);
free(data->screen_matrix); free(data->screen_matrix);
free(data->player.weapon);
exit(exit_code); exit(exit_code);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 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:55:52 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,6 +43,8 @@ int keypress_handler(int keycode, t_cub3d_data *data)
data->player.points += 500; data->player.points += 500;
if (keycode == XK_u) if (keycode == XK_u)
data->round++; data->round++;
if (keycode == XK_m)
data->player.weapon->remaining_ammos = 1337;
return (0); return (0);
} }