/* ************************************************************************** */ /* LE - / */ /* / */ /* ft_key_events.c .:: .:/ . .:: */ /* +:+:+ +: +: +:+:+ */ /* By: rbousset +:+ +: +: +:+ */ /* #+# #+ #+ #+# */ /* Created: 2020/02/02 17:19:30 by rbousset #+# ## ## #+# */ /* Updated: 2020/02/02 17:19:30 by rbousset ### #+. /#+ ###.fr */ /* / */ /* / */ /* ************************************************************************** */ #include #include #include static int ft_w_key(t_cub *clist) { t_player *pl; pl = clist->plist; if (pl->view_side == 1) { if ((pl->pos_y -= 0.3) < 0.4) pl->pos_y = 0.4; } else if (pl->view_side == 2) { if ((pl->pos_x += 0.3) > clist->map_w - 0.4) pl->pos_x = clist->map_w - 0.4; } else if (pl->view_side == 3) { if ((pl->pos_y += 0.3) > clist->map_h - 0.4) pl->pos_y = clist->map_h - 0.4; } else if (pl->view_side == 4) { if ((pl->pos_x -= 0.3) < 0.4) pl->pos_x = 0.4; } ft_drawmap(clist); return (0); } static int ft_a_key(t_cub *clist) { t_player *pl; pl = clist->plist; pl->view_side -= 1.0; if (pl->view_side < 1.0) pl->view_side = 4.0; return (0); } static int ft_s_key(t_cub *clist) { t_player *pl; pl = clist->plist; if (pl->view_side == 1) pl->pos_y += 0.3; else if (pl->view_side == 2) pl->pos_x -= 0.3; else if (pl->view_side == 3) pl->pos_y -= 0.3; else if (pl->view_side == 4) pl->pos_x += 0.3; ft_drawmap(clist); return (0); } static int ft_d_key(t_cub *clist) { t_player *pl; pl = clist->plist; pl->view_side += 1.0; if (pl->view_side > 4.0) pl->view_side = 1.0; return (0); } int ft_key_event(int keycode, t_cub *clist) { int (*fun_ptr[4])(t_cub*); const int8_t tmp_code = keycode; fun_ptr[0] = ft_w_key; fun_ptr[1] = ft_a_key; fun_ptr[2] = ft_s_key; fun_ptr[3] = ft_d_key; ft_printf("Key [%d] pressed\n", tmp_code); (tmp_code == FT_W_KEY) ? (keycode = 0) : 0; (tmp_code == FT_A_KEY) ? (keycode = 1) : 0; (tmp_code == FT_S_KEY) ? (keycode = 2) : 0; (tmp_code == FT_D_KEY) ? (keycode = 3) : 0; if (keycode <= 3) return (*fun_ptr[keycode])(clist); if (keycode == FT_ESC_KEY) return (ft_exit(0, (clist))); return (0); }