diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-30 21:16:48 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-30 21:16:48 +0200 |
commit | 14b08c7f6a5fc0efa61af63ef651fd444b00b697 (patch) | |
tree | b8b0f72d26d615f7cc5e6c3c6e7fa96a593753a3 | |
parent | Fine (diff) | |
download | 42-cub3d-14b08c7f6a5fc0efa61af63ef651fd444b00b697.tar.gz 42-cub3d-14b08c7f6a5fc0efa61af63ef651fd444b00b697.tar.bz2 42-cub3d-14b08c7f6a5fc0efa61af63ef651fd444b00b697.tar.xz 42-cub3d-14b08c7f6a5fc0efa61af63ef651fd444b00b697.tar.zst 42-cub3d-14b08c7f6a5fc0efa61af63ef651fd444b00b697.zip |
Nigga be shootin' quick af
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | inc/cub3d.h | 5 | ||||
-rw-r--r-- | inc/cub3d_defines.h | 1 | ||||
-rw-r--r-- | map/map_seven.cub | 4 | ||||
-rw-r--r-- | src/ft_bad_boy_actions.c | 6 | ||||
-rw-r--r-- | src/ft_collision.c | 97 | ||||
-rw-r--r-- | src/ft_key_loop.c | 81 | ||||
-rw-r--r-- | src/ft_select_bad_boy_action.c | 36 | ||||
-rw-r--r-- | src/ft_suffer_animation.c | 13 | ||||
-rw-r--r-- | src/ft_time.c | 2 |
10 files changed, 147 insertions, 99 deletions
@@ -129,6 +129,7 @@ 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_NAME += ft_collision.c #--------------------------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #--------------------------------------------------------------------------------------------------# diff --git a/inc/cub3d.h b/inc/cub3d.h index aaf4f55..28dfcfe 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -101,8 +101,8 @@ void ft_draw_heals(t_cub *cl, t_sprite *sprite); void ft_calc_weaps(t_cub *cl); void ft_draw_weapon(t_cub *cl, t_sprite *sprite); void ft_draw_handweap(t_cub *cl); -void ft_macos_suffer_animation(t_cub *cl); -void ft_linux_suffer_animation(t_cub *cl); +void ft_macos_suffer_animation(uint16_t dmg, t_cub *cl); +void ft_linux_suffer_animation(uint16_t dmg, t_cub *cl); void *ft_ammo_back_thread(void *vargp); void *ft_minimap_back_thread(void *vargp); void *ft_map_thread(void *vargp); @@ -277,5 +277,6 @@ int8_t ft_switch_weap_one(t_cub *cl); int8_t ft_switch_weap_two(t_cub *cl); int8_t ft_switch_weap_three(t_cub *cl); void ft_shoot(t_cub *cl, uint16_t center); +int ft_handle_keys(uint8_t i, float old_y, float old_x, t_cub *cl); # endif diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index f57ee6f..4cb67ea 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -304,6 +304,7 @@ enum # define FT_WEAP_THREE_STRT_AMMO 24 # define FT_WEAP_MAX_AMMO 1000 # define FT_ENMY_SIGHT_RANGE 7.0 +# define FT_ENMY_DAMAGE_AMOUNT 54 /* ** ====== OTHER ====== diff --git a/map/map_seven.cub b/map/map_seven.cub index e32f677..9461df2 100644 --- a/map/map_seven.cub +++ b/map/map_seven.cub @@ -5,7 +5,7 @@ SO ./media/img/tex/plate_small.xpm EA ./media/img/tex/plate_small.xpm WE ./media/img/tex/plate_small.xpm S ./media/img/sprites/pillar.xpm -S3 ./media/img/sprites/spikes.xpm +T ./media/img/sprites/spikes.xpm F 150,150,150 C 150,150,150 @@ -15,7 +15,7 @@ C 150,150,150 1000000000000000001 1000000000000000001 10000000e0000000001 -1000001111100000001 +1000000000000T00001 1000000000000000001 1000000000000000001 1000000000000000001 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 <libft.h> #include <cub3d.h> +#include <mlx.h> #include <stdlib.h> #include <math.h> @@ -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 <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */ +/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <mlx.h> +#include <stddef.h> +#include <stdint.h> +#include <time.h> + +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 <stdint.h> #include <time.h> -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 <stdint.h> 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; |