mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
dev: allocated weapon on the heap
This commit is contained in:
parent
0b00f5608c
commit
4048f5d1e3
8 changed files with 68 additions and 45 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
32
src/main.c
32
src/main.c
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
|
|
|
|||
31
src/player/weapon.h
Normal file
31
src/player/weapon.h
Normal 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
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue