Collision between physics component and map done. Also added map hitboxes display functions in debug mode.
This commit is contained in:
parent
7d95055abf
commit
d0797d7ddc
4
TODO.md
4
TODO.md
|
|
@ -24,10 +24,10 @@
|
||||||
|
|
||||||
### In progress
|
### In progress
|
||||||
|
|
||||||
- [ ] Make functionnal hitboxes between entities and map
|
|
||||||
|
|
||||||
### Done
|
### Done
|
||||||
|
|
||||||
|
- [x] Make functionnal hitboxes between entities and map
|
||||||
|
|
||||||
- [x] Make physics component use acceleration
|
- [x] Make physics component use acceleration
|
||||||
|
|
||||||
- [x] Rethink makefile structure
|
- [x] Rethink makefile structure
|
||||||
|
|
|
||||||
39
core/map.c
39
core/map.c
|
|
@ -499,3 +499,42 @@ int map_display_foreground(void)
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
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
|
||||||
|
|
@ -41,4 +41,8 @@ int map_display_background(void);
|
||||||
|
|
||||||
int map_display_foreground(void);
|
int map_display_foreground(void);
|
||||||
|
|
||||||
|
#if DEBUG > 1
|
||||||
|
int map_display_hitboxes(void);
|
||||||
|
#endif // DEBUG > 1
|
||||||
|
|
||||||
#endif // MAP_H
|
#endif // MAP_H
|
||||||
|
|
@ -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.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);
|
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)
|
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;
|
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.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;
|
transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,14 @@ int game_render(void)
|
||||||
return_failure_int;
|
return_failure_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DEBUG > 1
|
||||||
|
if(map_display_hitboxes())
|
||||||
|
{
|
||||||
|
game.is_running = false;
|
||||||
|
return_failure_int;
|
||||||
|
}
|
||||||
|
#endif // DEBUG > 1
|
||||||
|
|
||||||
if(display_update())
|
if(display_update())
|
||||||
{
|
{
|
||||||
game.is_running = false;
|
game.is_running = false;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue