-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay.c
100 lines (91 loc) · 2.89 KB
/
gameplay.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gameplay.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:21:41 by hbaddrul #+# #+# */
/* Updated: 2021/11/21 19:21:36 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "so_long.h"
#include "minilibx_opengl_20191021/mlx.h"
#include "libft/libft.h"
static void move_enemy(t_game *g)
{
int k;
t_enemy enemy;
k = -1;
while (++k < g->map.v)
{
enemy = g->enemies[k];
if (g->lines[enemy.actual.y - 1][enemy.actual.x] == '0' )
g->lines[enemy.actual.y - 1][enemy.actual.x] = 'V';
else if (g->lines[enemy.actual.y + 1][enemy.actual.x] == '0')
g->lines[enemy.actual.y + 1][enemy.actual.x] = 'V';
else
continue ;
g->lines[enemy.actual.y][enemy.actual.x] = '0';
}
}
static void animate(t_game *g)
{
void *tmp;
tmp = g->c;
g->c = g->c2;
g->c2 = tmp;
if (g->lines[g->player.attempt.y][g->player.attempt.x] == 'C')
g->p = g->p3;
else
g->p = g->p2;
}
void end_game(t_game *g)
{
char **tmp;
if (g->lines[g->player.attempt.y][g->player.attempt.x] == 'E' \
&& g->player.c == g->map.c)
printf("You win!\n");
else
printf("You lose!\n");
tmp = g->lines;
while (*tmp)
free(*tmp++);
free(g->lines);
if (g->enemies)
free(g->enemies);
mlx_destroy_image(g->mlx, g->o);
mlx_destroy_image(g->mlx, g->c);
mlx_destroy_image(g->mlx, g->c2);
mlx_destroy_image(g->mlx, g->e);
mlx_destroy_image(g->mlx, g->p2);
mlx_destroy_image(g->mlx, g->p3);
mlx_destroy_image(g->mlx, g->v);
mlx_clear_window(g->mlx, g->win);
mlx_destroy_window(g->mlx, g->win);
free(g->mlx);
exit(0);
}
void move(t_game *g)
{
char *moves;
if (g->lines[g->player.attempt.y][g->player.attempt.x] == 'V')
end_game(g);
g->lines[g->player.actual.y][g->player.actual.x] = '0';
move_enemy(g);
animate(g);
if (g->lines[g->player.attempt.y][g->player.attempt.x] == 'C')
g->player.c += 1;
else if (g->lines[g->player.attempt.y][g->player.attempt.x] == 'E' \
|| g->lines[g->player.attempt.y][g->player.attempt.x] == 'V')
end_game(g);
g->lines[g->player.attempt.y][g->player.attempt.x] = 'P';
g->player.moves += 1;
mlx_clear_window(g->mlx, g->win);
put_images(g);
moves = ft_itoa(g->player.moves);
mlx_string_put(g->mlx, g->win, 12, 22, 0x00FFFFFF, moves);
free(moves);
}