aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-14 17:04:05 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-14 17:04:05 +0200
commit84ac504edbe27a56018b77fd93f289b90b1d8bdc (patch)
tree17e4484db8a227c17cd9fe469ae661714f3e36e7 /src
parentBad boys do not eat walls anymore (diff)
download42-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
Diffstat (limited to 'src')
-rw-r--r--src/ft_bad_boy_actions.c57
-rw-r--r--src/ft_collision.c30
-rw-r--r--src/ft_draw_sprite_extra.c1
3 files changed, 62 insertions, 26 deletions
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)
{