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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */
/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <cub3d.h>
#include <mlx.h>
#include <stddef.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
static uint64_t
ft_find_x(int32_t key, const t_player *pl)
{
if (key == 0)
return (pl->pos_x + (pl->dir_x * FT_COLL_MULT));
else if (key == 1)
return (pl->pos_x + (-pl->dir_y * (FT_COLL_MULT / 2)));
else if (key == 2)
return (pl->pos_x - (pl->dir_x * FT_COLL_MULT));
else if (key == 3)
return (pl->pos_x - (-pl->dir_y * (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_y * FT_COLL_MULT));
else if (key == 1)
return (pl->pos_y - (pl->dir_x * (FT_COLL_MULT / 2)));
else if (key == 2)
return (pl->pos_y - (-pl->dir_y * FT_COLL_MULT));
else if (key == 3)
return (pl->pos_y + (pl->dir_x * (FT_COLL_MULT / 2)));
return ((uint64_t)pl->pos_y);
}
static void
ft_collision(double old_y, double 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);
cl->sfx[4].sfx_play(cl->sfx);
if (FT_OS == 1)
ft_macos_suffer_animation(FT_TRAP_DAMAGE_AMOUNT, cl);
else
ft_linux_suffer_animation(FT_TRAP_DAMAGE_AMOUNT, 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_handle_keys(uint8_t i, float old_y, float old_x, t_cub *cl)
{
cl->key_ptr[cl->key_input[i]](cl);
if (cl->key_input[i] >= 0 && cl->key_input[i] <= 3)
{
cl->sfx[1].sfx_play(cl->sfx);
ft_collision(old_y, old_x, cl->key_input[i], cl);
ft_find_item(&cl->plist, &cl->mlist, cl);
if (cl->mlist.isnlvl)
{
if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx &&
(uint32_t)cl->plist.pos_y == cl->mlist.nly)
{
cl->sfx[2].sfx_play(cl->sfx);
return ((ft_warp_level(cl->mlist.nlevel_path, cl) < 0) ?
(ft_exit(FT_RET_FAILED_STRUCTS, cl)) : (0));
}
}
}
return (0);
}
/*
** sl->r index list
** ------------------
** 0: goes south
** 1: goes north
** 2: goes west
** 3: goes east
*/
int8_t
ft_bb_collision(double old_y, double old_x, t_sprite *sl, t_map *ml)
{
if (sl->r == 0 && !ft_ischarset("0e",
ml->map[llround(sl->s_pos_y + 0.4)][llround(old_x)]))
{
sl->s_pos_y = old_y;
return (1);
}
else if (sl->r == 1 && !ft_ischarset("0e",
ml->map[llround(sl->s_pos_y - 0.4)][llround(old_x)]))
{
sl->s_pos_y = old_y;
return (1);
}
else if (sl->r == 2 && !ft_ischarset("0e",
ml->map[llround(old_y)][llround(sl->s_pos_x - 0.4)]))
{
sl->s_pos_x = old_x;
return (1);
}
else if (sl->r == 3 && !ft_ischarset("0e",
ml->map[llround(old_y)][llround(sl->s_pos_x + 0.4)]))
{
sl->s_pos_x = old_x;
return (1);
}
return (0);
}
|