From 6463914b0dc8136c453a1c163dd98f39e55e1c21 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Thu, 19 Mar 2020 19:04:46 +0100
Subject: Decent tier

---
 inc/cub3d.h              |  1 +
 inc/cub3d_structs.h      |  2 ++
 src/ft_convert_keycode.c |  1 +
 src/ft_draw_handweap.c   |  5 +++--
 src/ft_extra_keys.c      | 11 +++++++++++
 src/ft_get_fps_count.c   | 16 +++++++++++++++-
 src/ft_init_lists.c      |  1 +
 src/ft_key_loop.c        |  2 +-
 8 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/inc/cub3d.h b/inc/cub3d.h
index e422a59..8a7cfcb 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -52,6 +52,7 @@ int				ft_d_key(t_cub *clist);
 int				ft_f1_key(t_cub *clist);
 int				ft_left_key(t_cub *clist);
 int				ft_right_key(t_cub *clist);
+int				ft_space_key(t_cub *clist);
 int				ft_click_close(int keycode, t_cub *clist);
 int				ft_key_loop(t_cub *cl);
 int32_t			ft_convert_keycode(const int32_t tmp_code);
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index ba6243b..c2e1d4a 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -16,6 +16,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/types.h>
+#include <time.h>
 #include <pthread.h>
 
 typedef struct			s_win
@@ -142,6 +143,7 @@ typedef struct			s_player
 	float				cam_x;
 	float				plane_x;
 	float				plane_y;
+	uint8_t				fire;
 	int8_t				life;
 	uint8_t				has_weapon[2];
 	int8_t				handles_weapon;
diff --git a/src/ft_convert_keycode.c b/src/ft_convert_keycode.c
index 899dae9..a152cfa 100644
--- a/src/ft_convert_keycode.c
+++ b/src/ft_convert_keycode.c
@@ -32,5 +32,6 @@ int32_t
 	keycode = (tmp_code == FT_F1_KEY) ? (FT_F1_KEY) : (keycode);
 	keycode = (tmp_code == FT_TAB_KEY) ? (FT_TAB_KEY) : (keycode);
 	keycode = (tmp_code == FT_RET_KEY) ? (FT_RET_KEY) : (keycode);
+	keycode = (tmp_code == FT_SPC_KEY) ? (FT_SPC_KEY) : (keycode);
 	return (keycode);
 }
diff --git a/src/ft_draw_handweap.c b/src/ft_draw_handweap.c
index ae756e8..cdc5f5a 100644
--- a/src/ft_draw_handweap.c
+++ b/src/ft_draw_handweap.c
@@ -27,16 +27,17 @@ static void
 		cl->tweap[w_id].img_w * cl->tweap[w_id].tex_y + 2);
 }
 
-#include <libft.h>
 void
 	ft_draw_handweap(t_cub *cl)
 {
-	const int8_t	w_id = cl->plist.handles_weapon;
 	int32_t			x_ratio;
 	int32_t			y_ratio;
 	int16_t			x;
 	int16_t			y;
+	int8_t			w_id;
 
+	w_id = cl->plist.handles_weapon;
+	w_id += (cl->plist.fire == 1) ? (1) : 0;
 	x_ratio = (int32_t)((cl->tweap[w_id].img_w << 16) / cl->wlist.x_size) + 1;
 	y_ratio = (int32_t)((cl->tweap[w_id].img_h << 16) / cl->wlist.y_size) + 1;
 	y = 0;
diff --git a/src/ft_extra_keys.c b/src/ft_extra_keys.c
index e694667..e99b32f 100644
--- a/src/ft_extra_keys.c
+++ b/src/ft_extra_keys.c
@@ -13,6 +13,8 @@
 #include <cub3d.h>
 #include <stdint.h>
 #include <math.h>
+#include <time.h>
+#include <pthread.h>
 
 int
 	ft_left_key(t_cub *clist)
@@ -65,3 +67,12 @@ int
 	}
 	return (0);
 }
+
+int
+	ft_space_key(t_cub *clist)
+{
+	/* TODO: ft_sfx_weapon_fire(3) */
+	if (clist->plist.fire == 0)
+		clist->plist.fire = 1;
+	return (0);
+}
diff --git a/src/ft_get_fps_count.c b/src/ft_get_fps_count.c
index 16306c1..74afb45 100644
--- a/src/ft_get_fps_count.c
+++ b/src/ft_get_fps_count.c
@@ -22,7 +22,19 @@ static float
 	return ((ticks / (float)CLOCKS_PER_SEC) * 1000.0);
 }
 
-#include <stdio.h>
+
+static void
+	ft_handle_firing(clock_t dt, t_cub *cl)
+{
+	static clock_t	curr_time = 0;
+
+	curr_time += dt;
+	if (dt > 0 && ft_clock_to_ms(curr_time) > 200.0)
+	{
+		cl->plist.fire = 0;
+		curr_time -= CLOCKS_PER_SEC;
+	}
+}
 
 void
 	ft_get_fps_count(clock_t dt, t_cub *cl)
@@ -30,6 +42,8 @@ void
 	static clock_t	curr_time = 0;
 
 	curr_time += dt;
+	if (cl->plist.fire == 1)
+		ft_handle_firing(dt, cl);
 	if (dt > 0 && ft_clock_to_ms(curr_time) > 1000.0)
 	{
 		ft_sprintf(cl->fps_count, "fps: %ld", (uint32_t)CLOCKS_PER_SEC / dt);
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 9b44114..4eccf01 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -54,6 +54,7 @@ t_player
 	plist.has_weapon[0] = 0;
 	plist.has_weapon[1] = 0;
 	plist.handles_weapon = -1;
+	plist.fire = 0;
 	return (plist);
 }
 
diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c
index e0177e2..f44da13 100644
--- a/src/ft_key_loop.c
+++ b/src/ft_key_loop.c
@@ -80,7 +80,6 @@ int
 	const float		old_x = cl->plist.pos_x;
 	clock_t			begin_frame;
 
-	begin_frame = clock();
 	i = 0;
 	while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5)
 	{
@@ -103,6 +102,7 @@ int
 		}
 		i++;
 	}
+	begin_frame = clock();
 	ft_draw_scene(cl);
 	ft_get_fps_count(clock() - begin_frame, cl);
 	return (0);
-- 
cgit v1.2.3