diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-14 17:04:05 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-14 17:04:05 +0200 |
commit | 84ac504edbe27a56018b77fd93f289b90b1d8bdc (patch) | |
tree | 17e4484db8a227c17cd9fe469ae661714f3e36e7 | |
parent | Bad boys do not eat walls anymore (diff) | |
download | 42-cub3d-84ac504edbe27a56018b77fd93f289b90b1d8bdc.tar.gz 42-cub3d-84ac504edbe27a56018b77fd93f289b90b1d8bdc.tar.bz2 42-cub3d-84ac504edbe27a56018b77fd93f289b90b1d8bdc.tar.xz 42-cub3d-84ac504edbe27a56018b77fd93f289b90b1d8bdc.tar.zst 42-cub3d-84ac504edbe27a56018b77fd93f289b90b1d8bdc.zip |
Fixed bad boys collision, reroll if hit a wall
-rw-r--r-- | inc/cub3d.h | 2 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 1 | ||||
-rw-r--r-- | libft/Makefile | 12 | ||||
-rw-r--r-- | map/lvl_two.cub | 6 | ||||
-rw-r--r-- | map/map_seven.cub | 8 | ||||
-rw-r--r-- | src/ft_bad_boy_actions.c | 57 | ||||
-rw-r--r-- | src/ft_collision.c | 30 | ||||
-rw-r--r-- | src/ft_draw_sprite_extra.c | 1 |
8 files changed, 82 insertions, 35 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h index 5ae96c4..c0b324b 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -261,6 +261,8 @@ void ft_bb_fire(t_bad_boy *bl, t_sprite *sl, t_map *ml); void ft_check_bad_boy_shoot(t_cub *cl); void ft_damage_bad_boy(t_cub *cl); int8_t ft_can_it_shoot(int8_t id, double d, t_cub *cl); +int8_t ft_bb_collision(double old_y, double old_x, + t_sprite *sl, t_map *ml); /* ** ====== DELETION ====== diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index dcf633f..9a605c9 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -106,6 +106,7 @@ typedef struct s_sprite int32_t tex_y; double s_pos_x; double s_pos_y; + uint8_t r; double spritex; double spritey; int8_t exists; diff --git a/libft/Makefile b/libft/Makefile index 8ad24a7..2bd6342 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -118,10 +118,18 @@ OS = $(shell uname) #==============================================================================# #-------------------------------- Compiler ------------------------------------# #==============================================================================# -DBG = -glldb +ifeq (${OS}, Linux) + DBG = -ggdb +else + DBG = -glldb +endif FSANITIZE = -fsanitize=address #------------------------------------------------------------------------------# -CC = clang +ifeq (${OS}, Linux) + CC = gcc +else + CC = clang +endif #------------------------------------------------------------------------------# CFLAGS = -std=c89 CFLAGS += -Wall diff --git a/map/lvl_two.cub b/map/lvl_two.cub index 5ce7380..103c251 100644 --- a/map/lvl_two.cub +++ b/map/lvl_two.cub @@ -28,15 +28,15 @@ MU ./media/sound/BITURE-ET-MELANCOLIE.wav 11011 11311 1T0T1 -11311 +11011 1T0T1 11311 1T0T1 -11311 +11011 1T0T1 11311 1T0T1 -11311 +11011 1T0T1 11311 11L11 diff --git a/map/map_seven.cub b/map/map_seven.cub index bdd5427..25a7998 100644 --- a/map/map_seven.cub +++ b/map/map_seven.cub @@ -12,14 +12,14 @@ C 150,150,150 1111111111111111111 +1000000000000111111 +100000000000000e001 +1000000000000111111 +1000000000000000001 1000000000000000001 -1000000000000000011 1000000000000000001 1000000000000000001 1000000000000000001 -1000000010100000001 -100000000e000000001 -1000000010100000001 1000000000000000001 1000000000000000001 1000000!@#000000001 diff --git a/src/ft_bad_boy_actions.c b/src/ft_bad_boy_actions.c index 269ba0d..220e5ee 100644 --- a/src/ft_bad_boy_actions.c +++ b/src/ft_bad_boy_actions.c @@ -24,41 +24,48 @@ void 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)sl->s_pos_x])) - sl->s_pos_x = old_x; - if (!ft_ischarset("0e", ml->map[(uint64_t)sl->s_pos_y][(uint64_t)old_x])) - sl->s_pos_y = old_y; -} +/* +** rand(3) index list +** ------------------ +** rand() 0 - 3 +** 0: goes south +** 1: goes north +** 2: goes west +** 3: goes east +*/ -void - ft_bb_walk(t_bad_boy *bl, t_sprite *sl, t_map *ml) +static int8_t + ft_bb_actual_walk(t_bad_boy *bl, t_sprite *sl, t_map *ml) { - int8_t r_x; - int8_t r_y; + int8_t r; const double old_x = sl->s_pos_x; const double old_y = sl->s_pos_y; if (FT_OS == 2) - { - r_x = random() % 3; - r_y = random() % 3; - } + r = random() % 4; else - { - r_x = rand() % 3; - r_y = rand() % 3; - } - r_x = (r_x == 2) ? (-1) : (r_x); - r_y = (r_y == 2) ? (-1) : (r_y); - sl->s_pos_x += (FT_MOVE_SPEED * 1.5 * r_x); - sl->s_pos_y += (FT_MOVE_SPEED * 1.5 * r_y); - ft_bb_collision(old_y, old_x, sl, ml); + r = rand() % 4; + sl->r = r; + if (r == 0) + sl->s_pos_y += (FT_MOVE_SPEED * 1.5); + else if (r == 1) + sl->s_pos_y -= (FT_MOVE_SPEED * 1.5); + else if (r == 2) + sl->s_pos_x -= (FT_MOVE_SPEED * 1.5); + else if (r == 3) + sl->s_pos_x += (FT_MOVE_SPEED * 1.5); + if (ft_bb_collision(old_y, old_x, sl, ml)) + return (ft_bb_actual_walk(bl, 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; + return (0); +} + +void + ft_bb_walk(t_bad_boy *bl, t_sprite *sl, t_map *ml) +{ + ft_bb_actual_walk(bl, sl, ml); } /* diff --git a/src/ft_collision.c b/src/ft_collision.c index 385b2f7..6468433 100644 --- a/src/ft_collision.c +++ b/src/ft_collision.c @@ -95,3 +95,33 @@ int } return (0); } + +int8_t + ft_bb_collision(double old_y, double old_x, t_sprite *sl, t_map *ml) +{ + if (sl->r == 0 && !ft_ischarset("0e", + ml->map[(uint64_t)(sl->s_pos_y + 1.5)][(uint64_t)old_x])) + { + sl->s_pos_y = old_y; + return (1); + } + else if (sl->r == 1 && !ft_ischarset("0e", + ml->map[(uint64_t)(sl->s_pos_y - 0.5)][(uint64_t)old_x])) + { + sl->s_pos_y = old_y; + return (1); + } + else if (sl->r == 2 && !ft_ischarset("0e", + ml->map[(uint64_t)old_y][(uint64_t)(sl->s_pos_x - 0.5)])) + { + sl->s_pos_x = old_x; + return (1); + } + else if (sl->r == 3 && !ft_ischarset("0e", + ml->map[(uint64_t)old_y][(uint64_t)(sl->s_pos_x + 1.5)])) + { + sl->s_pos_x = old_x; + return (1); + } + return (0); +} diff --git a/src/ft_draw_sprite_extra.c b/src/ft_draw_sprite_extra.c index 81b299d..45833c8 100644 --- a/src/ft_draw_sprite_extra.c +++ b/src/ft_draw_sprite_extra.c @@ -50,7 +50,6 @@ static void (1 + sprite->transformx / sprite->transformy); } -#include <stdio.h> void ft_calc_sprite(t_cub *cl) { |