-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast.c
134 lines (125 loc) · 3.58 KB
/
cast.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhedeon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/04 16:49:49 by mhedeon #+# #+# */
/* Updated: 2019/01/31 17:25:20 by mhedeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
static void prepare(t_wolf *wolf, int x)
{
wolf->cam_x = 2.0 * x / (double)SCREEN_WIDTH - 1;
wolf->ray_dir_x = wolf->dir_x + wolf->plane_x * wolf->cam_x;
wolf->ray_dir_y = wolf->dir_y + wolf->plane_y * wolf->cam_x;
wolf->m_x = (int)wolf->p_x;
wolf->m_y = (int)wolf->p_y;
wolf->d_x = fabs(1.0 / wolf->ray_dir_x);
wolf->d_y = fabs(1.0 / wolf->ray_dir_y);
wolf->hit = 0;
wolf->step_x = wolf->ray_dir_x < 0.0 ? -1 : 1;
wolf->step_y = wolf->ray_dir_y < 0.0 ? -1 : 1;
wolf->side_x = wolf->d_x * ((wolf->ray_dir_x < 0.0) ?
(wolf->p_x - (double)wolf->m_x) :
((double)wolf->m_x + 1.0 - wolf->p_x));
wolf->side_y = wolf->d_y * ((wolf->ray_dir_y < 0.0) ?
(wolf->p_y - (double)wolf->m_y) :
((double)wolf->m_y + 1.0 - wolf->p_y));
}
static void ty_for_norm(t_wolf *wolf)
{
if (wolf->side_x < wolf->side_y)
{
wolf->side_x += wolf->d_x;
wolf->m_x += wolf->step_x;
wolf->side = wolf->ray_dir_x < 0 ? EAST : WEST;
}
else
{
wolf->side_y += wolf->d_y;
wolf->m_y += wolf->step_y;
wolf->side = wolf->ray_dir_y < 0 ? NORTH : SOUTH;
}
}
void cast(t_wolf *wolf)
{
int x;
x = wolf->start - 1;
while (++x < wolf->end)
{
prepare(wolf, x);
while (1)
{
ty_for_norm(wolf);
if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].w)
{
draw(wolf, x);
break ;
}
else if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].d &&
wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].c)
{
draw_door(wolf, x);
break ;
}
else if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].s)
draw_sprite(wolf, x);
}
}
}
int cast_door(t_wolf *wolf)
{
int x;
x = SCREEN_WIDTH / 2;
prepare(wolf, x);
while (1)
{
if (wolf->side_x < wolf->side_y)
{
wolf->side_x += wolf->d_x;
wolf->m_x += wolf->step_x;
wolf->side = wolf->ray_dir_x < 0 ? EAST : WEST;
}
else
{
wolf->side_y += wolf->d_y;
wolf->m_y += wolf->step_y;
wolf->side = wolf->ray_dir_y < 0 ? NORTH : SOUTH;
}
if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].w)
return (0);
else if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].d)
return (1);
}
return (0);
}
int cast_enemy(t_wolf *wolf)
{
int x;
x = SCREEN_WIDTH / 2;
prepare(wolf, x);
while (1)
{
if (wolf->side_x < wolf->side_y)
{
wolf->side_x += wolf->d_x;
wolf->m_x += wolf->step_x;
}
else
{
wolf->side_y += wolf->d_y;
wolf->m_y += wolf->step_y;
}
if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].w ||
wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].d)
return (0);
if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].s)
if (wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].sprite == 43 ||
wolf->map[wolf->m_y * wolf->m_width + wolf->m_x].sprite == 44)
return (1);
}
return (0);
}