diff options
| author | Dominik Kaiser | 2024-05-15 17:02:29 +0200 |
|---|---|---|
| committer | GitHub | 2024-05-15 17:02:29 +0200 |
| commit | cb6f98a5fa7bb2ed361abe68b3000f2e3f578ea7 (patch) | |
| tree | 150075893065fd1e41c8b3a8272e0faee421848e | |
| parent | e6edad24c9fa56538d66067dcff6bde39f746239 (diff) | |
| parent | 33a54536567a8b93cd0320814eba4ff515d5277b (diff) | |
| download | so_long-cb6f98a5fa7bb2ed361abe68b3000f2e3f578ea7.tar.gz so_long-cb6f98a5fa7bb2ed361abe68b3000f2e3f578ea7.zip | |
Merge refactoring into master
Refactoring
| -rw-r--r-- | include/so_long.h | 26 | ||||
| -rw-r--r-- | src/collision.c | 87 | ||||
| -rw-r--r-- | src/draw.c | 15 | ||||
| -rw-r--r-- | src/init.c | 13 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/map_utils.c | 16 | ||||
| -rw-r--r-- | src/player_process.c | 9 |
7 files changed, 86 insertions, 84 deletions
diff --git a/include/so_long.h b/include/so_long.h index 48c34aa..5d0d394 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 14:04:06 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 16:41:49 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,14 +50,20 @@ typedef struct s_ivector int y; } t_ivector; -typedef struct s_player +typedef struct s_collider +{ + t_vector position; + t_ivector size; +} t_collider; + +typedef struct s_actor { t_vector position; t_vector direction; t_vector velocity; t_ivector size; mlx_image_t *img; -} t_player; +} t_actor; typedef struct s_tilemap { @@ -72,7 +78,7 @@ typedef struct s_game { mlx_t *mlx; void *window; - t_player player; + t_actor player; int input_direction; t_tilemap map; } t_game; @@ -83,17 +89,15 @@ int init(t_game *game); void loop(void *params); void player_process(t_game *game); int draw(t_game *game); -int draw_map(t_game *game); +int draw_walls(t_game *game); void on_key_input(mlx_key_data_t event, void *params); t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size); t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size); enum e_tile get_tile(t_tilemap *map, int x, int y); -int check_collision(t_vector a_pos, t_ivector a_size, - t_vector b_pos, t_ivector b_size); -int check_wall_collision(t_vector a_pos, t_ivector a_size, - t_tilemap *map); -void move_and_slide(t_player *player, t_tilemap *map, +int check_collision(t_collider a, t_collider b); +int check_map_collision(t_collider collider, t_tilemap *map, enum e_tile type); +void move_and_slide(t_actor *actor, t_tilemap *map, double delta_time); -int is_on_floor(t_vector pos, t_ivector size, t_tilemap *map); +int is_on_floor(t_collider collider, t_tilemap *map); #endif // SO_LONG_H diff --git a/src/collision.c b/src/collision.c index 5c16130..115b70a 100644 --- a/src/collision.c +++ b/src/collision.c @@ -6,61 +6,60 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 13:20:29 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 15:19:54 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, - t_tilemap *map, t_ivector tile); +static int check_map_collision_for_tile(t_collider collider, t_tilemap *map, + t_ivector tile, enum e_tile type); /* * Checks if a and b are colliding and returns the direction relative to a. * Example: The top left edge of a and the bottom right of b overlap. * In this case, check_collision() will return (UP | LEFT). */ -int check_collision(t_vector a_pos, t_ivector a_size, t_vector b_pos, - t_ivector b_size) +int check_collision(t_collider a, t_collider b) { int result; result = 0; - if (a_pos.x < b_size.x + b_pos.x && a_pos.x + a_size.x > b_pos.x - && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) + if (a.position.x < b.size.x + b.position.x && a.position.x + + a.size.x > b.position.x && a.position.y < b.size.y + b.position.y + && a.position.y + a.size.y > b.position.y) { - if (a_pos.x < b_pos.x) + if (a.position.x < b.position.x) result |= RIGHT; - if (a_pos.x > b_pos.x) + if (a.position.x > b.position.x) result |= LEFT; - if (a_pos.y < b_pos.y) + if (a.position.y < b.position.y) result |= DOWN; - if (a_pos.y > b_pos.y) + if (a.position.y > b.position.y) result |= UP; } return (result); } -int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) +int check_map_collision(t_collider collider, t_tilemap *map, enum e_tile type) { int result; - t_ivector a_tile; + t_ivector local_tile; t_ivector check_tile; int x; int y; - result = 0; - a_tile = screen_to_grid_pos(a_pos, map->tile_size); + result = 0; + local_tile = screen_to_grid_pos(collider.position, map->tile_size); x = -1; while (x <= 1) { y = -1; while (y <= 1) { - check_tile.x = a_tile.x + x; - check_tile.y = a_tile.y + y; - result |= check_wall_collision_with_cell(a_pos, a_size, map, - check_tile); + check_tile.x = local_tile.x + x; + check_tile.y = local_tile.y + y; + result |= check_map_collision_for_tile(collider, map, check_tile, type); y++; } x++; @@ -68,46 +67,44 @@ int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) return (result); } -static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, - t_tilemap *map, t_ivector tile) +static int check_map_collision_for_tile(t_collider collider, t_tilemap *map, + t_ivector tile, enum e_tile type) { - t_vector wall_pos; - t_ivector wall_size; + t_collider tile_collider; - if (get_tile(map, tile.x, tile.y) == WALL) + if (get_tile(map, tile.x, tile.y) == type) { - wall_pos = grid_to_screen_pos(tile, map->tile_size); - wall_size.x = map->tile_size.x; - wall_size.y = map->tile_size.y; - return (check_collision(a_pos, a_size, wall_pos, wall_size)); + tile_collider.position = grid_to_screen_pos(tile, map->tile_size); + tile_collider.size.x = map->tile_size.x; + tile_collider.size.y = map->tile_size.y; + return (check_collision(collider, tile_collider)); } return (0); } -void move_and_slide(t_player *player, t_tilemap *map, double delta_time) +void move_and_slide(t_actor *actor, t_tilemap *map, double delta_time) { - t_vector move_pos; - - move_pos.x = player->position.x + (player->velocity.x * delta_time); - move_pos.y = player->position.y; + t_collider c; - if ((check_wall_collision(move_pos, player->size, map) & (RIGHT | LEFT)) == 0) - player->position.x = move_pos.x; + c.size = actor->size; + c.position.x = actor->position.x + (actor->velocity.x * delta_time); + c.position.y = actor->position.y; + if ((check_map_collision(c, map, WALL) & (RIGHT | LEFT)) == 0) + actor->position.x = c.position.x; else - player->velocity.x = 0; - - move_pos.x = player->position.x; - move_pos.y = player->position.y + (player->velocity.y * delta_time); - if ((check_wall_collision(move_pos, player->size, map) & (UP | DOWN)) == 0) - player->position.y = move_pos.y; + actor->velocity.x = 0; + c.position.x = actor->position.x; + c.position.y = actor->position.y + (actor->velocity.y * delta_time); + if ((check_map_collision(c, map, WALL) & (UP | DOWN)) == 0) + actor->position.y = c.position.y; else - player->velocity.y = 0; + actor->velocity.y = 0; } -int is_on_floor(t_vector pos, t_ivector size, t_tilemap *map) +int is_on_floor(t_collider collider, t_tilemap *map) { - pos.y += 5; - if (check_wall_collision(pos, size, map)) + collider.position.y += 5; + if (check_map_collision(collider, map, WALL)) return (1); return (0); } @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */ -/* Updated: 2024/05/11 16:00:17 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 16:41:15 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,12 @@ #include "libft.h" #include "so_long.h" -int draw_map(t_game *game) +int draw_walls(t_game *game) { - int x; - int y; - mlx_texture_t *wall_texture; - mlx_image_t *wall_image; + int x; + int y; + mlx_texture_t *wall_texture; + mlx_image_t *wall_image; wall_texture = mlx_load_png("textures/wall.png"); wall_image = mlx_texture_to_image(game->mlx, wall_texture); @@ -33,7 +33,8 @@ int draw_map(t_game *game) while (y < game->map.grid_size.y) { if (game->map.tiles[y * game->map.grid_size.x + x] == WALL) - mlx_image_to_window(game->mlx, wall_image, x * game->map.tile_size.x, y * game->map.tile_size.y); + mlx_image_to_window(game->mlx, wall_image, x + * game->map.tile_size.x, y * game->map.tile_size.y); y++; } x++; @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 12:13:03 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 15:09:44 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,15 @@ #include "so_long.h" static void init_hooks(t_game *game); -static void init_player(t_game *game); +static void init_actor(t_game *game); int init(t_game *game) { - game->mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; init_hooks(game); - init_player(game); + init_actor(game); return (0); } @@ -33,10 +32,10 @@ static void init_hooks(t_game *game) mlx_key_hook(game->mlx, on_key_input, game); } -static void init_player(t_game *game) +static void init_actor(t_game *game) { mlx_texture_t *texture; - t_player *player; + t_actor *player; player = &game->player; texture = mlx_load_png("textures/player.png"); @@ -49,5 +48,5 @@ static void init_player(t_game *game) player->img = mlx_texture_to_image(game->mlx, texture); mlx_resize_image(player->img, player->size.x, player->size.y); mlx_image_to_window(game->mlx, player->img, player->position.x, - player->position.y); + player->position.y); } @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */ -/* Updated: 2024/05/11 15:58:31 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 16:41:35 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int main(int argc, char *argv[]) if (load_map_from_file(&game.map, argv[1])) return (1); init(&game); - draw_map(&game); + draw_walls(&game); mlx_loop(game.mlx); } diff --git a/src/map_utils.c b/src/map_utils.c index 82acaa9..6d8fbd9 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 13:19:34 by dkaiser #+# #+# */ -/* Updated: 2024/05/14 14:44:39 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 15:04:31 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,16 +21,16 @@ t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size) return (screen_pos); } -t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size) +t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size) { - t_ivector grid_pos; + t_ivector grid_pos; - grid_pos.x = screen_pos.x / tile_size.x; - grid_pos.y = screen_pos.y / tile_size.y; - return (grid_pos); + grid_pos.x = screen_pos.x / tile_size.x; + grid_pos.y = screen_pos.y / tile_size.y; + return (grid_pos); } -enum e_tile get_tile(t_tilemap *map, int x, int y) +enum e_tile get_tile(t_tilemap *map, int x, int y) { - return map->tiles[y * map->grid_size.x + x]; + return (map->tiles[y * map->grid_size.x + x]); } diff --git a/src/player_process.c b/src/player_process.c index 7e27530..c285c4d 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 13:53:01 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 16:55:29 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,15 @@ static t_vector get_direction_from_input(t_game *game); void player_process(t_game *game) { - t_player *player; + 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 += 50; - if (player->direction.y == -1 && is_on_floor(player->position, player->size, - &game->map)) + if (player->direction.y == -1 && is_on_floor( + (t_collider){player->position, + player->size}, &game->map)) player->velocity.y = -1000; move_and_slide(player, &game->map, game->mlx->delta_time); } |
