diff --git a/TODO.md b/TODO.md index f309eeb..fe0a23d 100644 --- a/TODO.md +++ b/TODO.md @@ -24,10 +24,10 @@ ### In progress -- [ ] Make functionnal hitboxes between entities and map - ### Done +- [x] Make functionnal hitboxes between entities and map + - [x] Make physics component use acceleration - [x] Rethink makefile structure diff --git a/core/map.c b/core/map.c index 0447d88..d8ac1bc 100644 --- a/core/map.c +++ b/core/map.c @@ -499,3 +499,42 @@ int map_display_foreground(void) return EXIT_SUCCESS; } + +#if DEBUG > 1 +int map_display_hitboxes(void) +{ + uint32_t left_tile = 0; + if(game.camera.view_rect.x / TILE_SIZE > 0) + left_tile = game.camera.view_rect.x / TILE_SIZE; + + uint32_t right_tile = game.map->width; + if((game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1 < (int)game.map->width) + right_tile = (game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1; + + uint32_t top_tile = 0; + if(game.camera.view_rect.y / TILE_SIZE > 0) + top_tile = game.camera.view_rect.y / TILE_SIZE; + + uint32_t bottom_tile = game.map->height; + if((game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1 < (int)game.map->height) + bottom_tile = (game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1; + + for(uint32_t tile_y = top_tile; tile_y < bottom_tile; tile_y++) + for(uint32_t tile_x = left_tile; tile_x < right_tile; tile_x++) + { + if(game.map->hitboxes[tile_y * game.map->width + tile_x] == 1) + { + frect_t draw_frect = { + .x = tile_x * TILE_SIZE, + .y = tile_y * TILE_SIZE, + .w = TILE_SIZE, + .h = TILE_SIZE + }; + + if(display_draw_frect(&draw_frect, C_RED, 128)) return_failure_int; + } + } + + return EXIT_SUCCESS; +} +#endif // DEBUG > 1 \ No newline at end of file diff --git a/core/map.h b/core/map.h index f8579fa..eb53a68 100644 --- a/core/map.h +++ b/core/map.h @@ -41,4 +41,8 @@ int map_display_background(void); int map_display_foreground(void); +#if DEBUG > 1 +int map_display_hitboxes(void); +#endif // DEBUG > 1 + #endif // MAP_H \ No newline at end of file diff --git a/src/components/physics_system.c b/src/components/physics_system.c index 785c01e..18885ec 100644 --- a/src/components/physics_system.c +++ b/src/components/physics_system.c @@ -54,9 +54,85 @@ int physics_system_update(component_t *component) system_data->velocity.x = lerp_time(system_data->velocity.x, system_data->target_velocity.x * system_data->speed, system_data->smoothing_factor, game.delta_time_ms); system_data->velocity.y = lerp_time(system_data->velocity.y, system_data->target_velocity.y * system_data->speed, system_data->smoothing_factor, game.delta_time_ms); + if(hitbox_component_data->activated) + { + // Resolve entities collisions. if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return EXIT_FAILURE; + // Resolve map collisions + if(game.map) + { + uint16_t left_tile = 0; + if(game.camera.view_rect.x / TILE_SIZE > 0) + left_tile = hitbox_component_data->bounds.x / TILE_SIZE; + + uint16_t right_tile = game.map->width; + if((hitbox_component_data->bounds.x + hitbox_component_data->bounds.w) / TILE_SIZE + 1 < game.map->width) + right_tile = (hitbox_component_data->bounds.x + hitbox_component_data->bounds.w) / TILE_SIZE + 1; + + uint16_t top_tile = 0; + if(hitbox_component_data->bounds.y / TILE_SIZE > 0) + top_tile = hitbox_component_data->bounds.y / TILE_SIZE; + + uint16_t bottom_tile = game.map->height; + if((hitbox_component_data->bounds.y + hitbox_component_data->bounds.h) / TILE_SIZE + 1 < game.map->height) + bottom_tile = (hitbox_component_data->bounds.y + hitbox_component_data->bounds.h) / TILE_SIZE + 1; + + for(uint16_t tile_y = top_tile; tile_y < bottom_tile; tile_y++) + for(uint16_t tile_x = left_tile; tile_x < right_tile; tile_x++) + { + if(game.map->hitboxes[tile_y * game.map->width + tile_x] == 1) + { + const frect_t *hitbox_bounds = &hitbox_component_data->bounds; + const frect_t target_hitbox_bounds = { + .x = tile_x * TILE_SIZE, + .y = tile_y * TILE_SIZE, + .w = TILE_SIZE, + .h = TILE_SIZE + }; + frect_t intersect; + + if(intersect_frect(hitbox_bounds, &target_hitbox_bounds, &intersect)) + { + if(intersect.w < intersect.h) + { + const float left_correction = hitbox_bounds->x + hitbox_bounds->w - target_hitbox_bounds.x; + const float right_correction = target_hitbox_bounds.x + target_hitbox_bounds.w - hitbox_bounds->x; + + if(left_correction < right_correction) + { + transform_component_data->bounds.x -= left_correction; + if(system_data->velocity.x > 0.0f) system_data->velocity.x = 0.0f; + } + else + { + transform_component_data->bounds.x += right_correction; + if(system_data->velocity.x < 0.0f) system_data->velocity.x = 0.0f; + } + } + else + { + float top_correction = hitbox_bounds->y + hitbox_bounds->h - target_hitbox_bounds.y; + float bottom_correction = target_hitbox_bounds.y + target_hitbox_bounds.h - hitbox_bounds->y; + + if(top_correction < bottom_correction) + { + transform_component_data->bounds.y -= top_correction; + if(system_data->velocity.y > 0.0f) system_data->velocity.y = 0.0f; + } + else + { + transform_component_data->bounds.y += bottom_correction; + if(system_data->velocity.y < 0.0f) system_data->velocity.y = 0.0f; + } + } + } + } + } + } + } + transform_component_data->bounds.x += system_data->velocity.x * (float)game.delta_time_ms / 1000.0f; transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f; diff --git a/src/game.c b/src/game.c index ed9e800..10fd381 100644 --- a/src/game.c +++ b/src/game.c @@ -186,6 +186,14 @@ int game_render(void) return_failure_int; } +#if DEBUG > 1 + if(map_display_hitboxes()) + { + game.is_running = false; + return_failure_int; + } +#endif // DEBUG > 1 + if(display_update()) { game.is_running = false;