Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi 4414747c79 Improve auto-crouch detection
Changelog:
- Move tilemap update to be at the start
    - This is to account for new entities being created
    - The post tile collision is not used for now
- Change auto-crouch collision check to check for entities as well
2023-07-03 22:57:13 +08:00
En Yi 966432867f Add delay in crate destruction
Changelog:
- Add Lifetimer and remove hurtbox on hit
    - Still insta-remove on player hit
- Fix adding a component to an entity that already has that component
- Reorder lifetimer update system
2023-07-03 22:18:35 +08:00
En Yi 5049efb952 Allow hitboxes to hit multiple hurtboxes 2023-07-03 20:57:17 +08:00
4 changed files with 47 additions and 37 deletions

View File

@ -642,6 +642,7 @@ void init_level_scene(LevelScene_t* scene)
init_level_scene_data(&scene->data); init_level_scene_data(&scene->data);
// insert level scene systems // insert level scene systems
sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_array_add(&scene->scene.systems, &player_movement_input_system); sc_array_add(&scene->scene.systems, &player_movement_input_system);
sc_array_add(&scene->scene.systems, &player_bbox_update_system); sc_array_add(&scene->scene.systems, &player_bbox_update_system);
sc_array_add(&scene->scene.systems, &player_pushing_system); sc_array_add(&scene->scene.systems, &player_pushing_system);
@ -653,14 +654,14 @@ void init_level_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &boulder_destroy_wooden_tile_system); sc_array_add(&scene->scene.systems, &boulder_destroy_wooden_tile_system);
sc_array_add(&scene->scene.systems, &update_tilemap_system); sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_array_add(&scene->scene.systems, &tile_collision_system); sc_array_add(&scene->scene.systems, &tile_collision_system);
sc_array_add(&scene->scene.systems, &update_tilemap_system); //sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_array_add(&scene->scene.systems, &hitbox_update_system); sc_array_add(&scene->scene.systems, &hitbox_update_system);
sc_array_add(&scene->scene.systems, &player_crushing_system); sc_array_add(&scene->scene.systems, &player_crushing_system);
sc_array_add(&scene->scene.systems, &spike_collision_system); sc_array_add(&scene->scene.systems, &spike_collision_system);
sc_array_add(&scene->scene.systems, &state_transition_update_system); sc_array_add(&scene->scene.systems, &state_transition_update_system);
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system); sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
sc_array_add(&scene->scene.systems, &container_destroy_system);
sc_array_add(&scene->scene.systems, &lifetimer_update_system); sc_array_add(&scene->scene.systems, &lifetimer_update_system);
sc_array_add(&scene->scene.systems, &container_destroy_system);
sc_array_add(&scene->scene.systems, &sprite_animation_system); sc_array_add(&scene->scene.systems, &sprite_animation_system);
sc_array_add(&scene->scene.systems, &camera_update_system); sc_array_add(&scene->scene.systems, &camera_update_system);
sc_array_add(&scene->scene.systems, &player_dir_reset_system); sc_array_add(&scene->scene.systems, &player_dir_reset_system);

View File

@ -125,15 +125,21 @@ Entity_t* get_entity(EntityManager_t* p_manager, unsigned long id)
void* add_component(Entity_t* p_entity, ComponentEnum_t comp_type) void* add_component(Entity_t* p_entity, ComponentEnum_t comp_type)
{ {
unsigned long comp_idx = 0; if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE)
void* p_comp = new_component_from_mempool(comp_type, &comp_idx);
if (p_comp)
{ {
p_entity->components[comp_type] = comp_idx;
struct EntityUpdateEventInfo evt = (struct EntityUpdateEventInfo){p_entity->m_id, comp_type, comp_idx, COMP_ADDTION}; unsigned long comp_idx = 0;
sc_queue_add_last(&p_entity->manager->to_update, evt); void* p_comp = new_component_from_mempool(comp_type, &comp_idx);
if (p_comp)
{
p_entity->components[comp_type] = comp_idx;
struct EntityUpdateEventInfo evt = (struct EntityUpdateEventInfo){p_entity->m_id, comp_type, comp_idx, COMP_ADDTION};
sc_queue_add_last(&p_entity->manager->to_update, evt);
}
return p_comp;
} }
return p_comp;
return get_component(p_entity, comp_type);
} }
void* get_component(Entity_t *p_entity, ComponentEnum_t comp_type) void* get_component(Entity_t *p_entity, ComponentEnum_t comp_type)
@ -148,5 +154,6 @@ void remove_component(Entity_t *p_entity, ComponentEnum_t comp_type)
{ {
if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return; if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return;
struct EntityUpdateEventInfo evt = (struct EntityUpdateEventInfo){p_entity->m_id, comp_type, p_entity->components[comp_type] , COMP_DELETION}; struct EntityUpdateEventInfo evt = (struct EntityUpdateEventInfo){p_entity->m_id, comp_type, p_entity->components[comp_type] , COMP_DELETION};
p_entity->components[comp_type] = MAX_COMP_POOL_SIZE;
sc_queue_add_last(&p_entity->manager->to_update, evt); sc_queue_add_last(&p_entity->manager->to_update, evt);
} }

View File

@ -274,7 +274,7 @@ static uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid
// TODO: This should be a point collision check, not an AABB check // TODO: This should be a point collision check, not an AABB check
static bool check_collision_offset( static uint8_t check_collision_offset(
Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz,
TileGrid_t* grid, Vector2 offset TileGrid_t* grid, Vector2 offset
) )
@ -689,17 +689,8 @@ void player_movement_input_system(Scene_t* scene)
} }
} }
bool hit = false; uint8_t collide_type = check_collision_offset(p_player, p_ctransform->position, p_bbox->size, &tilemap, (Vector2){0, -TILE_SIZE});
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; if (collide_type == 1)
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE;
unsigned int tile_y = (p_ctransform->position.y) / TILE_SIZE;
if (p_bbox->size.y < TILE_SIZE && tile_y > 0) tile_y--; // hack to detect small bbox state
for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++)
{
hit |= tilemap.tiles[tile_y * tilemap.width + tile_x].tile_type == SOLID_TILE;
}
if (hit)
{ {
p_pstate->is_crouch |= 0b10; p_pstate->is_crouch |= 0b10;
} }
@ -1645,19 +1636,21 @@ void update_tilemap_system(Scene_t* scene)
void hitbox_update_system(Scene_t* scene) void hitbox_update_system(Scene_t* scene)
{ {
//static bool checked_entities[MAX_COMP_POOL_SIZE] = {0}; static bool checked_entities[MAX_COMP_POOL_SIZE] = {0};
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = data->tilemap;
unsigned int ent_idx; unsigned int ent_idx;
CHitBoxes_t* p_hitbox; CHitBoxes_t* p_hitbox;
sc_map_foreach(&scene->ent_manager.component_map[CHITBOXES_T], ent_idx, p_hitbox) sc_map_foreach(&scene->ent_manager.component_map[CHITBOXES_T], ent_idx, p_hitbox)
{ {
bool hit = false;
Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
if (!p_ent->m_alive) continue; if (!p_ent->m_alive) continue;
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
memset(checked_entities, 0, sizeof(checked_entities));
for (uint8_t i = 0; i < p_hitbox->n_boxes; ++i) for (uint8_t i = 0; i < p_hitbox->n_boxes; ++i)
{ {
Vector2 hitbox_pos = { Vector2 hitbox_pos = {
@ -1679,7 +1672,6 @@ void hitbox_update_system(Scene_t* scene)
unsigned int other_ent_idx; unsigned int other_ent_idx;
Entity_t* p_other_ent; Entity_t* p_other_ent;
Vector2 overlap; Vector2 overlap;
//memset(checked_entities, 0, sizeof(checked_entities));
if (tilemap.tiles[tile_idx].tile_type != EMPTY_TILE) if (tilemap.tiles[tile_idx].tile_type != EMPTY_TILE)
{ {
@ -1692,21 +1684,19 @@ void hitbox_update_system(Scene_t* scene)
) )
) )
{ {
hit = true;
if (p_hitbox->atk > tilemap.tiles[tile_idx].def) if (p_hitbox->atk > tilemap.tiles[tile_idx].def)
{ {
change_a_tile(&tilemap, tile_idx, EMPTY_TILE); change_a_tile(&tilemap, tile_idx, EMPTY_TILE);
} continue;
if (p_hitbox->one_hit)
{
remove_entity_from_tilemap(&scene->ent_manager, &tilemap, p_ent);
goto hitbox_done;
} }
} }
} }
sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, other_ent_idx, p_other_ent) sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, other_ent_idx, p_other_ent)
{ {
if (other_ent_idx == ent_idx) continue; if (other_ent_idx == ent_idx) continue;
//if (checked_entities[other_ent_idx]) continue; if (checked_entities[other_ent_idx]) continue;
Entity_t* p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); Entity_t* p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
if (!p_other_ent->m_alive) continue; // To only allow one way collision check if (!p_other_ent->m_alive) continue; // To only allow one way collision check
@ -1724,6 +1714,7 @@ void hitbox_update_system(Scene_t* scene)
) )
) )
{ {
hit = true;
if (p_hitbox->atk > p_other_hurtbox->def) if (p_hitbox->atk > p_other_hurtbox->def)
{ {
p_other_hurtbox->damage_src = ent_idx; p_other_hurtbox->damage_src = ent_idx;
@ -1747,20 +1738,29 @@ void hitbox_update_system(Scene_t* scene)
} }
} }
} }
remove_entity_from_tilemap(&scene->ent_manager, &tilemap, p_other_ent);
if (p_ent->m_tag != PLAYER_ENT_TAG)
{
remove_component(p_other_ent, CHURTBOX_T);
CLifeTimer_t* p_clifetimer = add_component(p_other_ent, CLIFETIMER_T);
p_clifetimer->life_time = 8;
}
else
{
// Need to remove immediately, otherwise will interfere with bomb spawning
remove_entity_from_tilemap(&scene->ent_manager, &tilemap, p_other_ent);
}
} }
if (p_hitbox->one_hit)
{
remove_entity_from_tilemap(&scene->ent_manager, &tilemap, p_ent);
goto hitbox_done;
}
} }
} }
} }
} }
} }
hitbox_done: continue; if (p_hitbox->one_hit && hit)
{
remove_entity_from_tilemap(&scene->ent_manager, &tilemap, p_ent);
}
} }
} }

View File

@ -18,6 +18,7 @@ Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool meta
CHurtbox_t* p_hurtbox = add_component(p_crate, CHURTBOX_T); CHurtbox_t* p_hurtbox = add_component(p_crate, CHURTBOX_T);
p_hurtbox->size = p_bbox->size; p_hurtbox->size = p_bbox->size;
p_hurtbox->def = metal ? 2 : 1; p_hurtbox->def = metal ? 2 : 1;
p_hurtbox->damage_src = -1;
if (item != CONTAINER_EMPTY) if (item != CONTAINER_EMPTY)
{ {
@ -47,6 +48,7 @@ Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets)
CHurtbox_t* p_hurtbox = add_component(p_boulder, CHURTBOX_T); CHurtbox_t* p_hurtbox = add_component(p_boulder, CHURTBOX_T);
p_hurtbox->size = p_bbox->size; p_hurtbox->size = p_bbox->size;
p_hurtbox->def = 2; p_hurtbox->def = 2;
p_hurtbox->damage_src = -1;
return p_boulder; return p_boulder;
} }