aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_find_item.c
blob: a76c1acb8eca7020baf6a31d521790c93d0eaebe (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
115
116
117
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_find_item.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/17 20:06:26 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/17 20:06:29 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

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

static uint16_t
	ft_fetch_heals_id(uint64_t pos_y, uint64_t pos_x, t_cub *cl)
{
	uint16_t	id;

	id = 0;
	while (id <= 64)
	{
		if (cl->sprites[9][id].s_pos_y == pos_y &&
			cl->sprites[9][id].s_pos_x == pos_x)
			return (id);
		id++;
	}
	return (0);
}

static uint16_t
	ft_fetch_weaps_id(int8_t weap_id, uint64_t pos_y, uint64_t pos_x, t_cub *cl)
{
	uint16_t	id;

	id = 0;
	while (id <= 32)
	{
		if (cl->sprites[10 + weap_id][id].s_pos_y == pos_y &&
			cl->sprites[10 + weap_id][id].s_pos_x == pos_x)
			return (id);
		id++;
	}
	return (0);
}

static void
	ft_set_ammo(uint8_t weap_id, t_player *pl)
{
	if (weap_id == 0)
		pl->ammo[0] = FT_WEAP_ONE_STRT_AMMO;
	else if (weap_id == 1)
		pl->ammo[1] += FT_WEAP_TWO_STRT_AMMO;
	else if (weap_id == 2)
		pl->ammo[2] += FT_WEAP_THREE_STRT_AMMO;
	else if (weap_id == 3)
	{
		pl->ammo[2] += FT_AMMO_PACK;
		weap_id = 2;
	}
	if (pl->ammo[weap_id] > FT_WEAP_MAX_AMMO)
		pl->ammo[weap_id] = FT_WEAP_MAX_AMMO;
}

static void
	ft_weapon_check(const char map_char, t_player *pl, t_cub *cl)
{
	uint8_t	weap_id;
	uint8_t	that_id;

	if (ft_ischarset(FT_CHRST_WEAPONS, map_char))
	{
		weap_id = 0;
		weap_id = (map_char == '@') ? (1) : (weap_id);
		weap_id = (map_char == '#') ? (2) : (weap_id);
		weap_id = (map_char == 'x') ? (3) : (weap_id);
		cl->mlist.map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x] = '0';
		if (weap_id != 3)
		{
			that_id = ft_fetch_weaps_id(weap_id, (uint64_t)pl->pos_y,
												(uint64_t)pl->pos_x, cl);
			cl->sprites[10 + weap_id][that_id].s_pos_x = 0;
			cl->sprites[10 + weap_id][that_id].s_pos_y = 0;
			pl->has_weapon[weap_id] = 1;
		}
		pl->handles_weapon = (weap_id == 0) ? (0) : (pl->handles_weapon);
		pl->handles_weapon = (weap_id == 1) ? (2) : (pl->handles_weapon);
		pl->handles_weapon = (weap_id == 2) ? (4) : (pl->handles_weapon);
		ft_set_ammo((weap_id == 3) ? (2) : (weap_id), pl);
		cl->sfx[6 + (((weap_id == 3) ? (2) : (weap_id)) * 2)].sfx_play(cl->sfx);
	}
}

void
	ft_find_item(t_player *pl, t_map *ml, t_cub *cl)
{
	uint16_t	id;
	const char	map_char = ml->map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x];

	if (ft_ischarset(FT_CHRST_ITEM, map_char))
	{
		if (map_char == '+' && pl->life < FT_STRT_LIFE)
		{
			pl->life += FT_HEAL_PACK_AMOUNT;
			pl->life = (pl->life > FT_STRT_LIFE) ? (FT_STRT_LIFE) : (pl->life);
			ml->map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x] = '0';
			id = ft_fetch_heals_id((uint64_t)pl->pos_y,
									(uint64_t)pl->pos_x, cl);
			cl->sprites[9][id].s_pos_x = 0;
			cl->sprites[9][id].s_pos_y = 0;
			cl->sfx[5].sfx_play(cl->sfx);
		}
		ft_weapon_check(map_char, pl, cl);
	}
}