summaryrefslogtreecommitdiff
path: root/src/player_process.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-20 20:30:41 +0200
committerDominik Kaiser2024-05-20 20:30:41 +0200
commit3ad8aa9e66f7e159b175a9a52e5ad00bbfd90734 (patch)
treeaaa06976a569ad3f3452121800e6bf33fc8d14ac /src/player_process.c
parente45d23fa56958a127c067e599d1408b030220ce8 (diff)
downloadso_long-3ad8aa9e66f7e159b175a9a52e5ad00bbfd90734.tar.gz
so_long-3ad8aa9e66f7e159b175a9a52e5ad00bbfd90734.zip
Add collectibles
Exit now only works after collecting everything. I don't really like my current solution, but it works for now.
Diffstat (limited to 'src/player_process.c')
-rw-r--r--src/player_process.c80
1 files changed, 71 insertions, 9 deletions
diff --git a/src/player_process.c b/src/player_process.c
index 0d4f0c3..7df0d53 100644
--- a/src/player_process.c
+++ b/src/player_process.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
-/* Updated: 2024/05/15 17:53:54 by dkaiser ### ########.fr */
+/* Updated: 2024/05/20 20:29:07 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,9 @@
#include "so_long.h"
static t_vector get_direction_from_input(t_game *game);
-static int check_exit(t_actor *player, t_tilemap *map);
+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_actor *player, t_tilemap *map);
void player_process(t_game *game)
{
@@ -24,23 +26,83 @@ void player_process(t_game *game)
player->direction = get_direction_from_input(game);
player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED;
player->velocity.y += 50;
- if (player->direction.y == -1 && is_on_floor(
- (t_collider){player->position,
+ if (player->direction.y == -1 && is_on_floor((t_collider){player->position,
player->size}, &game->map))
player->velocity.y = -1000;
- if (check_exit(player, &game->map))
- exit(0);
+ handle_collectible_collision(player, &game->map);
+ handle_exit_collision(player, &game->map);
move_and_slide(player, &game->map, game->mlx->delta_time);
}
-static int check_exit(t_actor *player, t_tilemap *map)
+static void handle_collectible_collision(t_actor *player, t_tilemap *map)
{
- t_collider player_collider;
+ t_collider player_collider;
+ int collision;
+ t_ivector player_tile;
player_collider = (t_collider){player->position, player->size};
- return (check_map_collision(player_collider, map, EXIT));
+ 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;
+
+ ft_printf("Collected a collectile.\n");
+ 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_actor *player, t_tilemap *map)
+{
+ t_collider player_collider;
+ size_t i;
+
+ 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++;
+ }
+ exit(0);
+ }
+}
static t_vector get_direction_from_input(t_game *game)
{