From 14b08c7f6a5fc0efa61af63ef651fd444b00b697 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 30 Mar 2020 21:16:48 +0200 Subject: Nigga be shootin' quick af --- src/ft_bad_boy_actions.c | 6 +-- src/ft_collision.c | 97 ++++++++++++++++++++++++++++++++++++++++++ src/ft_key_loop.c | 81 ++++++----------------------------- src/ft_select_bad_boy_action.c | 36 +++++++++------- src/ft_suffer_animation.c | 13 +++--- src/ft_time.c | 2 +- 6 files changed, 140 insertions(+), 95 deletions(-) create mode 100644 src/ft_collision.c (limited to 'src') diff --git a/src/ft_bad_boy_actions.c b/src/ft_bad_boy_actions.c index eada61a..3e961fb 100644 --- a/src/ft_bad_boy_actions.c +++ b/src/ft_bad_boy_actions.c @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -26,11 +27,10 @@ void 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])) - { + if (!ft_ischarset("0e", ml->map[lround(old_y)][lround(sl->s_pos_x)])) sl->s_pos_x = old_x; + if (!ft_ischarset("0e", ml->map[lround(sl->s_pos_y)][lround(old_x)])) sl->s_pos_y = old_y; - } } void diff --git a/src/ft_collision.c b/src/ft_collision.c new file mode 100644 index 0000000..00dab1f --- /dev/null +++ b/src/ft_collision.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */ +/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include + +static uint64_t + ft_find_x(int32_t key, const t_player *pl) +{ + if (key == 0) + return (pl->pos_x + (pl->dir_y * FT_COLL_MULT)); + else if (key == 1) + return (pl->pos_x + (pl->dir_x * (FT_COLL_MULT / 2))); + else if (key == 2) + return (pl->pos_x - (pl->dir_y * FT_COLL_MULT)); + else if (key == 3) + return (pl->pos_x - (pl->dir_x * (FT_COLL_MULT / 2))); + return ((uint64_t)pl->pos_x); +} + +static uint64_t + ft_find_y(int32_t key, const t_player *pl) +{ + if (key == 0) + return (pl->pos_y + (pl->dir_x * FT_COLL_MULT)); + else if (key == 1) + return (pl->pos_y - (pl->dir_y * (FT_COLL_MULT / 2))); + else if (key == 2) + return (pl->pos_y - (pl->dir_x * FT_COLL_MULT)); + else if (key == 3) + return (pl->pos_y + (pl->dir_y * (FT_COLL_MULT / 2))); + return ((uint64_t)pl->pos_y); +} + +static void + ft_collision(double old_y, double old_x, int32_t key, t_cub *cl) +{ + uint64_t x; + uint64_t y; + + x = ft_find_x(key, &cl->plist); + y = ft_find_y(key, &cl->plist); + if (cl->mlist.map[y][x] == 'T') + { + cl->plist.pos_x = old_x + ((old_x - x) / 2); + cl->plist.pos_y = old_y + ((old_y - y) / 2); + cl->sfx[4].sfx_play(cl->sfx); + if (FT_OS == 1) + ft_macos_suffer_animation(FT_TRAP_DAMAGE_AMOUNT, cl); + else + ft_linux_suffer_animation(FT_TRAP_DAMAGE_AMOUNT, cl); + x = ft_find_x(key, &cl->plist); + y = ft_find_y(key, &cl->plist); + } + if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[(uint64_t)old_y][x]) || + cl->mlist.map[(uint64_t)old_y][x] == '\0') + cl->plist.pos_x = old_x; + if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[y][(uint64_t)old_x]) || + cl->mlist.map[y][(uint64_t)old_x] == '\0') + cl->plist.pos_y = old_y; +} + +int + ft_handle_keys(uint8_t i, float old_y, float old_x, t_cub *cl) +{ + cl->key_ptr[cl->key_input[i]](cl); + if (cl->key_input[i] >= 0 && cl->key_input[i] <= 3) + { + cl->sfx[1].sfx_play(cl->sfx); + ft_collision(old_y, old_x, cl->key_input[i], cl); + ft_find_item(&cl->plist, &cl->mlist, cl); + if (cl->mlist.isnlvl) + { + if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && + (uint32_t)cl->plist.pos_y == cl->mlist.nly) + { + cl->sfx[2].sfx_play(cl->sfx); + return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ? + (ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0)); + } + } + } + return (0); +} diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index eb020da..dad6467 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -17,82 +17,24 @@ #include #include -static uint64_t - ft_find_x(int32_t key, const t_player *pl) -{ - if (key == 0) - return (pl->pos_x + (pl->dir_y * FT_COLL_MULT)); - else if (key == 1) - return (pl->pos_x + (pl->dir_x * (FT_COLL_MULT / 2))); - else if (key == 2) - return (pl->pos_x - (pl->dir_y * FT_COLL_MULT)); - else if (key == 3) - return (pl->pos_x - (pl->dir_x * (FT_COLL_MULT / 2))); - return ((uint64_t)pl->pos_x); -} - -static uint64_t - ft_find_y(int32_t key, const t_player *pl) -{ - if (key == 0) - return (pl->pos_y + (pl->dir_x * FT_COLL_MULT)); - else if (key == 1) - return (pl->pos_y - (pl->dir_y * (FT_COLL_MULT / 2))); - else if (key == 2) - return (pl->pos_y - (pl->dir_x * FT_COLL_MULT)); - else if (key == 3) - return (pl->pos_y + (pl->dir_y * (FT_COLL_MULT / 2))); - return ((uint64_t)pl->pos_y); -} - static void - ft_collision(double old_y, double old_x, int32_t key, t_cub *cl) + ft_check_bad_boy_shoot(t_cub *cl) { - uint64_t x; - uint64_t y; - - x = ft_find_x(key, &cl->plist); - y = ft_find_y(key, &cl->plist); - if (cl->mlist.map[y][x] == 'T') - { - cl->plist.pos_x = old_x + ((old_x - x) / 2); - cl->plist.pos_y = old_y + ((old_y - y) / 2); - if (FT_OS == 1) - ft_macos_suffer_animation(cl); - else - ft_linux_suffer_animation(cl); - x = ft_find_x(key, &cl->plist); - y = ft_find_y(key, &cl->plist); - } - if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[(uint64_t)old_y][x]) || - cl->mlist.map[(uint64_t)old_y][x] == '\0') - cl->plist.pos_x = old_x; - if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[y][(uint64_t)old_x]) || - cl->mlist.map[y][(uint64_t)old_x] == '\0') - cl->plist.pos_y = old_y; -} + int8_t i; -static int - ft_handle_keys(uint8_t i, float old_y, float old_x, t_cub *cl) -{ - cl->key_ptr[cl->key_input[i]](cl); - if (cl->key_input[i] >= 0 && cl->key_input[i] <= 3) + i = 0; + while (i < cl->mlist.sprite_nbr[13]) { - cl->sfx[1].sfx_play(cl->sfx); - ft_collision(old_y, old_x, cl->key_input[i], cl); - ft_find_item(&cl->plist, &cl->mlist, cl); - if (cl->mlist.isnlvl) + if (cl->bad_boy[i].does == 2) { - if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && - (uint32_t)cl->plist.pos_y == cl->mlist.nly) - { - cl->sfx[2].sfx_play(cl->sfx); - return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ? - (ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0)); - } + cl->sfx[11].sfx_play(cl->sfx); + if (FT_OS == 1) + ft_macos_suffer_animation(FT_ENMY_DAMAGE_AMOUNT, cl); + else + ft_linux_suffer_animation(FT_ENMY_DAMAGE_AMOUNT, cl); } + i++; } - return (0); } int @@ -109,6 +51,7 @@ int ft_handle_keys(i, old_y, old_x, cl); i++; } + ft_check_bad_boy_shoot(cl); cl->moves = (cl->key_input[0] == -1) ? (0) : (cl->moves); begin_frame = clock(); ft_draw_scene(cl); diff --git a/src/ft_select_bad_boy_action.c b/src/ft_select_bad_boy_action.c index 5f4cc3a..c716e33 100644 --- a/src/ft_select_bad_boy_action.c +++ b/src/ft_select_bad_boy_action.c @@ -25,9 +25,9 @@ ** rand() rules ** ------------ ** I. player is not in sight -** rand 0-3 | 0-2 wait | 3 walk +** rand(0-3) | [0-2] wait | [3] walk ** II. player is in sight -** rand 0-7 | 0-1 wait | 2-5 walk | 6-7 +** rand(0-7) | [0-1] wait | [2-3] walk | [4-7] fire */ static double @@ -50,7 +50,7 @@ static int8_t return (1); else return (0); - return (r); + return (0); } static int8_t @@ -64,11 +64,18 @@ static int8_t r = rand() % 8; if (r <= 1) return (0); - else if (r >= 2 && r <= 5) + else if (r >= 2 && r <= 3) return (1); else return (2); - return (r); + return (0); +} + +static void + ft_bb_act(int8_t r, int8_t i, t_cub *cl) +{ + cl->bad_boy[i].act[r](&cl->bad_boy[i], + &cl->sprites[13][i], &cl->mlist); } void @@ -77,28 +84,27 @@ void int8_t i; int8_t r; - i = 0; - while (i < cl->mlist.sprite_nbr[13]) + i = -1; + while (++i < cl->mlist.sprite_nbr[13]) { if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 && (ft_get_dist(cl->sprites[13][i], cl) > FT_ENMY_SIGHT_RANGE - || cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] > - ft_get_dist(cl->sprites[13][0], cl))) + || cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] < + ft_get_dist(cl->sprites[13][i], cl))) { r = ft_set_r_i(); + ft_bb_act(r, i, cl); } else if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 && (ft_get_dist(cl->sprites[13][i], cl) <= FT_ENMY_SIGHT_RANGE - || cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] <= - ft_get_dist(cl->sprites[13][0], cl))) + && cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] > + ft_get_dist(cl->sprites[13][i], cl))) { r = ft_set_r_ii(); + ft_bb_act(r, i, cl); } else if (cl->bad_boy[i].life <= 0) - /* something */ - cl->bad_boy[i].act[r](&cl->bad_boy[i], - &cl->sprites[13][i], &cl->mlist); + cl->bad_boy[i].does = 3; cl->bad_boy[i].sleep = 1; - i++; } } diff --git a/src/ft_suffer_animation.c b/src/ft_suffer_animation.c index 1b48e47..d813348 100644 --- a/src/ft_suffer_animation.c +++ b/src/ft_suffer_animation.c @@ -16,9 +16,9 @@ #include static void - ft_get_damaged(t_cub *cl) + ft_get_damaged(uint16_t dmg, t_cub *cl) { - cl->plist.life -= FT_TRAP_DAMAGE_AMOUNT; + cl->plist.life -= dmg; if (cl->plist.life <= 0) { cl->isdead = 1; @@ -27,13 +27,12 @@ static void } else { - cl->sfx[4].sfx_play(cl->sfx); cl->sfx[3].sfx_play(cl->sfx); } } void - ft_linux_suffer_animation(t_cub *cl) + ft_linux_suffer_animation(uint16_t dmg, t_cub *cl) { int32_t x; int32_t y; @@ -53,11 +52,11 @@ void mlx_put_image_to_window(cl->wlist.wlx, cl->wlist.winptr, cl->img.img, 0, 0); mlx_destroy_image(cl->wlist.wlx, cl->img.img); - ft_get_damaged(cl); + ft_get_damaged(dmg, cl); } void - ft_macos_suffer_animation(t_cub *cl) + ft_macos_suffer_animation(uint16_t dmg, t_cub *cl) { int32_t x; int32_t y; @@ -78,6 +77,6 @@ void (x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(0, rgb, cl); } } - ft_get_damaged(cl); + ft_get_damaged(dmg, cl); cl->doicast = 0; } diff --git a/src/ft_time.c b/src/ft_time.c index 0157fd8..ea0d221 100644 --- a/src/ft_time.c +++ b/src/ft_time.c @@ -45,7 +45,7 @@ void curr = clock(); dt[i] += curr - before; - if (cl->bad_boy[i].sleep == 1 && dt[i] > 0 && ft_clock_to_ms(dt[i]) > 1500.0) + if (cl->bad_boy[i].sleep == 1 && dt[i] > 0 && ft_clock_to_ms(dt[i]) > 1500) { cl->bad_boy[i].sleep = 0; dt[i] = 0; -- cgit v1.2.3