/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_key_loop.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 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(float old_y, float 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); 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; } 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; 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->key_input[i], cl); if (cl->mlist.isnlvl && !cl->plist.isdead) { if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && (uint32_t)cl->plist.pos_y == cl->mlist.nly) { return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ? (ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0)); } } i++; } /* if (cl->key_input[0] != -1) */ ft_draw_scene(cl); return (0); }