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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
/* Updated: 2024/06/10 16:28:09 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "MLX42/MLX42.h"
#include "ft_printf.h"
#include "so_long.h"
static t_vector get_direction_from_input(t_game *game);
static void handle_collectible_collision(t_actor *player, t_tilemap *map);
static void collect_collectible(t_ivector pos, t_tilemap *map);
static void handle_exit_collision(t_game *game);
void player_process(t_game *game)
{
t_actor *player;
player = &game->player;
player->direction = get_direction_from_input(game);
player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED;
player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED;
handle_collectible_collision(player, &game->map);
handle_exit_collision(game);
if (move_and_slide(player, &game->map, game->mlx->delta_time))
player->steps++;
}
static void handle_collectible_collision(t_actor *player, t_tilemap *map)
{
t_collider player_collider;
int collision;
t_ivector player_tile;
player_collider = (t_collider){player->position, player->size};
player_tile = screen_to_grid_pos(player->position, map->tile_size);
collision = check_map_collision(player_collider, map, COLLECTIBLE);
if (collision & UP)
{
if (collision & LEFT)
collect_collectible(player_tile, map);
if (collision & RIGHT)
collect_collectible((t_ivector){player_tile.x + 1, player_tile.y},
map);
}
if (collision & DOWN)
{
if (collision & LEFT)
collect_collectible((t_ivector){player_tile.x, player_tile.y + 1},
map);
if (collision & RIGHT)
collect_collectible((t_ivector){player_tile.x + 1, player_tile.y
+ 1}, map);
}
}
static void collect_collectible(t_ivector pos, t_tilemap *map)
{
size_t i;
t_vector collectible_pos;
t_ivector collectible_tile;
set_tile(map, pos.x, pos.y, EMPTY);
i = 0;
while (i < map->collectible_img->count)
{
collectible_pos.x = map->collectible_img->instances[i].x;
collectible_pos.y = map->collectible_img->instances[i].y;
collectible_tile = screen_to_grid_pos(collectible_pos, map->tile_size);
if (pos.x == collectible_tile.x && pos.y == collectible_tile.y)
{
map->collectible_img->instances[i].enabled = 0;
break ;
}
i++;
}
}
static void handle_exit_collision(t_game *game)
{
t_collider player_collider;
size_t i;
t_actor *player;
t_tilemap *map;
player = &game->player;
map = &game->map;
player_collider = (t_collider){player->position, player->size};
if (check_map_collision(player_collider, map, EXIT))
{
i = 0;
while (i < map->collectible_img->count)
{
if (map->collectible_img->instances[i].enabled)
return ;
i++;
}
mlx_close_window(game->mlx);
}
}
static t_vector get_direction_from_input(t_game *game)
{
t_vector result;
result.x = ((game->input_direction & RIGHT) != 0)
- ((game->input_direction & LEFT) != 0);
result.y = ((game->input_direction & DOWN) != 0)
- ((game->input_direction & UP) != 0);
return (result);
}
|