From 966432867f4439f30dca6a7f230666a8026ee3a8 Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 3 Jul 2023 22:18:35 +0800 Subject: [PATCH] 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 --- scenes/editor_scene.c | 2 +- scenes/engine/EC/entManager.c | 21 ++++++++++++++------- scenes/game_systems.c | 20 ++++++++++++++++---- scenes/items_ent.c | 2 ++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index daa0cd6..b21a9cf 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -659,8 +659,8 @@ void init_level_scene(LevelScene_t* scene) 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, &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, &container_destroy_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, &player_dir_reset_system); diff --git a/scenes/engine/EC/entManager.c b/scenes/engine/EC/entManager.c index 4e27786..9a31ae7 100644 --- a/scenes/engine/EC/entManager.c +++ b/scenes/engine/EC/entManager.c @@ -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) { - unsigned long comp_idx = 0; - void* p_comp = new_component_from_mempool(comp_type, &comp_idx); - if (p_comp) + if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) { - 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); + + unsigned long comp_idx = 0; + 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) @@ -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; 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); } diff --git a/scenes/game_systems.c b/scenes/game_systems.c index 17a52e6..68859dc 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -1645,7 +1645,7 @@ void update_tilemap_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); TileGrid_t tilemap = data->tilemap; @@ -1658,6 +1658,8 @@ void hitbox_update_system(Scene_t* scene) Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); if (!p_ent->m_alive) continue; 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) { Vector2 hitbox_pos = { @@ -1679,7 +1681,6 @@ void hitbox_update_system(Scene_t* scene) unsigned int other_ent_idx; Entity_t* p_other_ent; Vector2 overlap; - //memset(checked_entities, 0, sizeof(checked_entities)); if (tilemap.tiles[tile_idx].tile_type != EMPTY_TILE) { @@ -1704,7 +1705,7 @@ void hitbox_update_system(Scene_t* scene) sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, other_ent_idx, p_other_ent) { 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); if (!p_other_ent->m_alive) continue; // To only allow one way collision check @@ -1746,7 +1747,18 @@ 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); + } } } diff --git a/scenes/items_ent.c b/scenes/items_ent.c index 185699e..a9f3fe4 100644 --- a/scenes/items_ent.c +++ b/scenes/items_ent.c @@ -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); p_hurtbox->size = p_bbox->size; p_hurtbox->def = metal ? 2 : 1; + p_hurtbox->damage_src = -1; 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); p_hurtbox->size = p_bbox->size; p_hurtbox->def = 2; + p_hurtbox->damage_src = -1; return p_boulder; }