-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks_and_rules.c
105 lines (95 loc) · 2.95 KB
/
hooks_and_rules.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hooks_and_rules.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsimeono <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/17 14:02:28 by vsimeono #+# #+# */
/* Updated: 2022/03/21 19:30:04 by vsimeono ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
/* Determining where the Key Pressed Takes the Player
and what Action it Determines */
int key_hook(int keycode, t_long *arch)
{
if (keycode == UP || keycode == DOWN \
|| keycode == LEFT || keycode == RIGHT)
{
p_position_and_counting_objects(arch, keycode);
create_visual_map(arch);
}
if (keycode == ESC)
finish(arch);
return (0);
}
/* Using Player's Position and Next Move to Determine
what Happens to the Objects in the Map and Finishes the Game */
void p_position_and_counting_objects(t_long *arch, int keycode)
{
t_list *temp;
char *next;
temp = player_position(arch, arch->lines);
next = player_next_move(temp, arch, keycode);
if (*next != '1')
{
if (*next == 'C' || *next == '0')
{
if (*next == 'C')
arch->collected++;
arch->moves++;
}
else if (*next == 'E' && arch->collected == arch->collect)
{
arch->moves++;
finish(arch);
}
else if (*next == 'E')
return ;
*next = 'P';
*(char *)(temp->next->line + arch->pos_x) = '0';
}
return ;
}
/* Determining the Player's Position in the Map */
t_list *player_position(t_long *arch, t_list *lines)
{
t_list *temp;
temp = lines->next;
while (temp)
{
arch->pos_x = 0;
while (*(char *)(temp->line + arch->pos_x) \
&& *(char *)(temp->line + arch->pos_x) != 'P' \
&& *(char *)(temp->line + arch->pos_x) != '\n' )
arch->pos_x++;
if (*(char *)(temp->line + arch->pos_x) == 'P')
break ;
lines = temp;
temp = temp->next;
}
return (lines);
}
/* Determining where the Next Player Move would Arrive the Player*/
char *player_next_move(t_list *temp, t_long *arch, int keycode)
{
char *next;
next = (char *)temp->line;
if (keycode == UP)
next = (char *)temp->line + arch->pos_x;
if (keycode == DOWN)
next = (char *)temp->next->next->line + arch->pos_x;
if (keycode == LEFT)
next = (char *)temp->next->line + arch->pos_x - 1;
if (keycode == RIGHT)
next = (char *)temp->next->line + arch->pos_x + 1;
return (next);
}
int loop(t_long *arch)
{
char *moves;
moves = ft_itoa(arch->moves);
mlx_string_put(arch->mlx, arch->mlx_win, 30, 30, 0xffff00, moves);
return (0);
}