aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_shoot.c
blob: dbd88620a3307cb4b7a865a4e2b9b401cffcda89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_shoot.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:22:32 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:23:42 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <math.h>

static void
	ft_hide_sprite(t_cub *cl)
{
	int64_t	i;
	int64_t	j;
	uint8_t	stop;

	i = -1;
	j = -1;
	stop = 0;
	while (++i < FT_TOTAL_SPRT - 5)
	{
		while (++j < 4096)
		{
			if (cl->sprites[i][j].s_pos_x == cl->rlist.sqx &&
				cl->sprites[i][j].s_pos_y == cl->rlist.sqy)
			{
				stop = 1;
				break ;
			}
		}
		if (stop == 1)
			break ;
		j = -1;
	}
	cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx] = '0';
	cl->sprites[i][j].s_pos_x = 0;
	cl->sprites[i][j].s_pos_y = 0;
}

static int8_t
	ft_weap_range(t_cub *cl)
{
	if (cl->plist.handles_weapon == 0 &&
		sqrtf(powf(cl->plist.pos_x - cl->rlist.sqx, 2)
		+ powf(cl->plist.pos_y - cl->rlist.sqy, 2)) > 1.6)
		return (0);
	return (1);
}

static void
	ft_set_sq(t_cub *cl)
{
	if (cl->rlist.x_side_dist < cl->rlist.y_side_dist)
	{
		cl->rlist.x_side_dist += cl->rlist.x_delta_dist;
		cl->rlist.sqy += cl->mlist.x_step;
		cl->rlist.side = 0;
	}
	else
	{
		cl->rlist.y_side_dist += cl->rlist.y_delta_dist;
		cl->rlist.sqx += cl->mlist.y_step;
		cl->rlist.side = 1;
	}
}

static void
	ft_hitscan(t_cub *cl, uint16_t hit)
{
	while (hit == 0)
	{
		ft_set_sq(cl);
		if (ft_ischarset(FT_CHRST_SPRITES,
			cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx]) && ft_weap_range(cl))
		{
			hit = 1;
			ft_hide_sprite(cl);
		}
		else if (cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx] == 'e' &&
			ft_weap_range(cl))
		{
			hit = 1;
			ft_damage_bad_boy(cl);
		}
		else if (cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx] == '1')
			hit = 1;
	}
}

void
	ft_shoot(t_cub *cl)
{
	t_win		*wl;
	t_player	*pl;

	wl = &cl->wlist;
	pl = &cl->plist;
	cl->rlist.y_ray_pos = pl->pos_y;
	cl->rlist.x_ray_pos = pl->pos_x;
	cl->rlist.y_ray_dir = -pl->dir_y;
	cl->rlist.x_ray_dir = pl->dir_x;
	cl->rlist.sqy = (uint64_t)cl->rlist.y_ray_pos;
	cl->rlist.sqx = (uint64_t)cl->rlist.x_ray_pos;
	ft_detection_init_x(cl);
	ft_detection_init_y(cl);
	ft_hitscan(cl, 0);
}