-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_enemy.c
141 lines (130 loc) · 3.47 KB
/
move_enemy.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
135
136
137
138
139
140
141
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alimotta <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 11:02:48 by alimotta #+# #+# */
/* Updated: 2024/01/18 11:02:52 by alimotta ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static void ft_move_up(t_vars *v, int s)
{
int x;
int y;
int i;
x = v -> x_enemy;
y = v -> y_enemy;
if (v -> arr[x - 1][y] != '1' && v -> arr[x - 1][y] != 'E')
{
v -> arr[x][y] = '0';
put_image_to_map(v->arr[x][y], y * s, x * s, &v);
put_image_to_map(v->arr[x][y], y * s, (x - 1) * s, &v);
x--;
v -> arr[x][y] = 'L';
i = s;
while (i >= 0)
{
i -= 1;
put_animation_enemy(i, y * s, x * s + i, &v);
}
v -> x_enemy -= 1;
}
}
static void ft_move_down(t_vars *v, int s)
{
int x;
int y;
int i;
x = v -> x_enemy;
y = v -> y_enemy;
if (v -> arr[x + 1][y] != '1' && v -> arr[x + 1][y] != 'E')
{
v -> arr[x][y] = '0';
put_image_to_map(v->arr[x][y], y * s, x * s, &v);
put_image_to_map(v->arr[x][y], y * s, (x + 1) * s, &v);
x++;
v -> arr[x][y] = 'L';
i = s;
while (i >= 0)
{
i -= 1;
put_animation_enemy(i, y * s, x * s - i, &v);
}
v -> x_enemy += 1;
}
}
static void ft_move_left(t_vars *v, int s)
{
int x;
int y;
int i;
x = v -> x_enemy;
y = v -> y_enemy;
if (v -> arr[x][y - 1] != '1' && v -> arr[x][y - 1] != 'E')
{
v -> arr[x][y] = '0';
put_image_to_map(v->arr[x][y], y * s, x * s, &v);
put_image_to_map(v->arr[x][y], (y - 1) * s, x * s, &v);
y--;
v -> arr[x][y] = 'L';
i = s;
while (i >= 0)
{
i -= 1;
put_animation_enemy(i, y * s + i, x * s, &v);
}
v -> y_enemy -= 1;
}
}
static void ft_move_right(t_vars *v, int s)
{
int x;
int y;
int i;
x = v -> x_enemy;
y = v -> y_enemy;
if (v -> arr[x][y + 1] != '1' && v -> arr[x][y + 1] != 'E')
{
v -> arr[x][y] = '0';
put_image_to_map(v->arr[x][y], y * s, x * s, &v);
put_image_to_map(v->arr[x][y], (y + 1) * s, x * s, &v);
y++;
v -> arr[x][y] = 'L';
i = s;
while (i >= 0)
{
i -= 1;
put_animation_enemy(i, y * s - i, x * s, &v);
}
v -> y_enemy += 1;
}
}
int ft_move_enemy(t_vars *v)
{
int m;
v->frame++;
v->framerate = v->frame % 100000;
if (v->enemy == 1 && v->framerate == 0)
{
m = (v->x_player + v->y_player + v->x_exit + v->x_enemy) % 4;
if (ft_is_safe_enemy(v, v->x_enemy + 1, v->y_enemy) == 0 && m == 0)
ft_move_down(v, v->s);
else if (ft_is_safe_enemy(v, v->x_enemy, v->y_enemy - 1) == 0 && m == 1)
ft_move_left(v, v->s);
else if (ft_is_safe_enemy(v, v->x_enemy - 1, v->y_enemy) == 0 && m == 2)
ft_move_up(v, v->s);
else if (ft_is_safe_enemy(v, v->x_enemy, v->y_enemy + 1) == 0 && m == 3)
ft_move_right(v, v->s);
else if (ft_is_safe_enemy(v, v->x_enemy + 1, v->y_enemy) == 0)
ft_move_down(v, v->s);
if (v->x_player == v->x_enemy && v->y_player == v->y_enemy)
{
v->lose = 2;
mlx_loop_end(v->mlx);
}
}
return (0);
}