diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | inc/cub3d.h | 9 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 11 | ||||
-rw-r--r-- | src/ft_bad_boy_actions.c | 59 | ||||
-rw-r--r-- | src/ft_big_t.c | 1 | ||||
-rw-r--r-- | src/ft_init_bad_boys.c | 10 | ||||
-rw-r--r-- | src/ft_key_loop.c | 2 | ||||
-rw-r--r-- | src/ft_raycasting.c | 1 | ||||
-rw-r--r-- | src/ft_select_bad_boy_action.c | 35 | ||||
-rw-r--r-- | src/ft_time.c | 23 |
10 files changed, 147 insertions, 6 deletions
@@ -127,6 +127,8 @@ SRCS_NAME += ft_sort_s_t.c SRCS_NAME += ft_big_t.c SRCS_NAME += ft_time.c SRCS_NAME += ft_init_bad_boys.c +SRCS_NAME += ft_bad_boy_actions.c +SRCS_NAME += ft_select_bad_boy_action.c #--------------------------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #--------------------------------------------------------------------------------------------------# diff --git a/inc/cub3d.h b/inc/cub3d.h index 85e2ab3..aaf4f55 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -240,6 +240,15 @@ void ft_handle_firing(clock_t before, t_cub *cl); void ft_get_fps_count(clock_t before, t_cub *cl); /* +** ====== BAD BOYS ====== +*/ + +void ft_select_bad_boy_action(t_cub *cl); +void ft_bb_wait(t_bad_boy *bl, t_sprite *sl, t_map *ml); +void ft_bb_walk(t_bad_boy *bl, t_sprite *sl, t_map *ml); +void ft_bb_fire(t_bad_boy *bl, t_sprite *sl, t_map *ml); + +/* ** ====== DELETION ====== */ diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 329f8bb..9ebc305 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -104,8 +104,8 @@ typedef struct s_sprite int32_t y; int32_t tex_x; int32_t tex_y; - uint64_t s_pos_x; - uint64_t s_pos_y; + double s_pos_x; + double s_pos_y; double spritex; double spritey; int8_t exists; @@ -124,8 +124,8 @@ typedef struct s_sprite typedef struct s_player { - float pos_x; - float pos_y; + double pos_x; + double pos_y; float pos_z; float start_x; float start_y; @@ -228,7 +228,8 @@ typedef struct s_bad_boy { int8_t life; int8_t does; - void (*act[3])(struct s_sprite*, struct s_map*); + int8_t sleep; + void (*act[3])(struct s_bad_boy*, struct s_sprite*, t_map*); } t_bad_boy; typedef struct s_cub diff --git a/src/ft_bad_boy_actions.c b/src/ft_bad_boy_actions.c new file mode 100644 index 0000000..6b7b3bf --- /dev/null +++ b/src/ft_bad_boy_actions.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bad_boy_actions.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:51 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:51 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdlib.h> + +void + ft_bb_wait(t_bad_boy *bl, t_sprite *sl, t_map *ml) +{ + (void)sl; + (void)ml; + bl->does = 0; +} + +static void + ft_bb_collision(double old_y, double old_x, t_sprite *sl, t_map *ml) +{ + if (!ft_ischarset("0e", ml->map[(uint64_t)old_y][(uint64_t)old_x])) + { + sl->s_pos_x = old_x; + sl->s_pos_y = old_y; + } +} + +void + ft_bb_walk(t_bad_boy *bl, t_sprite *sl, t_map *ml) +{ + int8_t r_x; + int8_t r_y; + const double old_x = sl->s_pos_x; + const double old_y = sl->s_pos_y; + + r_x = (rand() % 2 == 1) ? (1) : (-1); + r_y = (rand() % 2 == 1) ? (1) : (-1); + sl->s_pos_x += 0.3 * (r_x); + sl->s_pos_y += 0.3 * (r_y); + ft_bb_collision(old_y, old_x, sl, ml); + ml->map[(uint64_t)old_y][(uint64_t)old_x] = '0'; + ml->map[(uint64_t)sl->s_pos_y][(uint64_t)sl->s_pos_x] = 'e'; + bl->does = 1; +} + +void + ft_bb_fire(t_bad_boy *bl, t_sprite *sl, t_map *ml) +{ + (void)sl; + (void)ml; + bl->does = 2; +} diff --git a/src/ft_big_t.c b/src/ft_big_t.c index b4d7e52..5fe65c6 100644 --- a/src/ft_big_t.c +++ b/src/ft_big_t.c @@ -70,6 +70,7 @@ void { cl->big_t[i][0] = dist_tab[j][k]; cl->big_t[i][1] = (float)ft_set_current_sprite(cl, j); + cl->big_t[i][1] += (j == 13) ? (cl->bad_boy[k].does) : (0); cl->big_t[i][2] = (float)j; cl->big_t[i][3] = (float)k; k++; diff --git a/src/ft_init_bad_boys.c b/src/ft_init_bad_boys.c index 8ddc9a5..ef67498 100644 --- a/src/ft_init_bad_boys.c +++ b/src/ft_init_bad_boys.c @@ -15,6 +15,15 @@ #include <stdint.h> #include <stdlib.h> +/* +** bad_boy[].does list +** ------------------- +** 0: waiting +** 1: walking +** 2: firing +** 3: be dead +*/ + int8_t ft_init_bad_boys(t_cub *cl) { @@ -28,6 +37,7 @@ int8_t { cl->bad_boy[i].life = 5; cl->bad_boy[i].does = 0; + cl->bad_boy[i].sleep = 0; cl->bad_boy[i].act[0] = ft_bb_wait; cl->bad_boy[i].act[1] = ft_bb_walk; cl->bad_boy[i].act[2] = ft_bb_fire; diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index 9ca9185..eb020da 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -46,7 +46,7 @@ static uint64_t } static void - ft_collision(float old_y, float old_x, int32_t key, t_cub *cl) + ft_collision(double old_y, double old_x, int32_t key, t_cub *cl) { uint64_t x; uint64_t y; diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index cc00963..e01e4ff 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -106,6 +106,7 @@ void pthread_create(&tid[1], 0x0, ft_floor_cast, (void*)cl); pthread_join(tid[0], 0x0); pthread_join(tid[1], 0x0); + ft_select_bad_boy_action(cl); ft_calc_sprite(cl); if (cl->plist.handles_weapon > -1) ft_draw_handweap(cl); diff --git a/src/ft_select_bad_boy_action.c b/src/ft_select_bad_boy_action.c new file mode 100644 index 0000000..6b3d2ef --- /dev/null +++ b/src/ft_select_bad_boy_action.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_select_bad_boy_actions.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:51 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:51 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdlib.h> + +void + ft_select_bad_boy_action(t_cub *cl) +{ + int8_t i; + int8_t r; + + i = 0; + while (i < cl->mlist.sprite_nbr[13]) + { + if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0) + { + r = rand() % 2; + cl->bad_boy[i].act[r](&cl->bad_boy[i], + &cl->sprites[13][i], &cl->mlist); + cl->bad_boy[i].sleep = 1; + } + i++; + } +} diff --git a/src/ft_time.c b/src/ft_time.c index fb0b0d2..d175bbf 100644 --- a/src/ft_time.c +++ b/src/ft_time.c @@ -38,9 +38,32 @@ void } void + ft_bad_boys_time(clock_t before, t_cub *cl) +{ + int8_t i; + static clock_t dt = 0; + clock_t curr; + + curr = clock(); + dt += curr - before; + i = 0; + while (i < cl->mlist.sprite_nbr[13]) + { + if (cl->bad_boy[i].sleep == 1 && dt > 0 && ft_clock_to_ms(dt) > 600.0) + { + cl->bad_boy[i].sleep = 0; + dt = 0; + } + i++; + } +} + +void ft_timings(clock_t before, t_cub *cl) { if (cl->plist.fire == 1) ft_handle_firing(before, cl); + if (cl->mlist.sprite_nbr[13] > 0) + ft_bad_boys_time(before, cl); ft_get_fps_count(before, cl); } |