diff options
Diffstat (limited to 'src/ft_key_loop.c')
-rw-r--r-- | src/ft_key_loop.c | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index d913c49..d40de34 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -15,13 +15,46 @@ #include <stdint.h> #include <stddef.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 (1); +} + +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 (1); +} + static void - ft_collision(float old_y, float old_x, t_player *pl, t_map *ml) + ft_collision(float old_y, float old_x, int32_t key, t_cub *cl) { - const size_t x = pl->pos_x; - const size_t y = pl->pos_y; + uint64_t x; + uint64_t y; + t_player *pl; - if (ml->map[y][x] == '1') + pl = &cl->plist; + x = ft_find_x(key, pl); + y = ft_find_y(key, pl); + if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[y][x]) || + cl->mlist.map[y][x] == '\0') { pl->pos_y = old_y; pl->pos_x = old_x; @@ -32,19 +65,24 @@ int ft_key_loop(t_cub *cl) { uint8_t i; - const float old_y = cl->plist->pos_y; - const float old_x = cl->plist->pos_x; + const float old_y = cl->plist.pos_y; + const float old_x = cl->plist.pos_x; i = 0; while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5) { cl->key_ptr[cl->key_input[i]](cl); - ft_collision(old_y, old_x, cl->plist, cl->mlist); + ft_collision(old_y, old_x, cl->key_input[i], cl); + if (cl->mlist.isnlvl) + { + if (ft_warp_level(cl) < 0) + return (ft_exit(FT_RET_FAILED_STRUCTS, cl)); + } i++; } - if (cl->key_input[0] != -1) - { - ft_draw_scene(cl); - } + if (cl->key_input[0] != -1) + { + ft_draw_scene(cl); + } return (0); } |