From 3520715655d46dc705557243375aa22b7e25fa15 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sun, 29 Sep 2024 17:58:07 +0800 Subject: [PATCH] Allow urchin to be crushed LIMITATION - Urchin can be crushed by wooden crates. --- scenes/components.h | 5 ++++ scenes/engine_impl.c | 2 ++ scenes/game_systems.c | 61 +++++++++++++++++++++++++++---------------- scenes/items_ent.c | 3 ++- scenes/player_ent.c | 2 ++ 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/scenes/components.h b/scenes/components.h index 354523f..58b72b2 100644 --- a/scenes/components.h +++ b/scenes/components.h @@ -14,6 +14,7 @@ enum ComponentEnum { CWATERRUNNER_T, CAIRTIMER_T, CEMITTER_T, + CSQUISHABLE_T }; typedef struct _CMovementState_t { @@ -171,3 +172,7 @@ typedef struct _CEmitter_t EmitterHandle handle; Vector2 offset; } CEmitter_t; + +typedef struct _CSquishable_t { + bool active; //< Dummy variable +} CSquishable_t; diff --git a/scenes/engine_impl.c b/scenes/engine_impl.c index 1bdd081..1838f94 100644 --- a/scenes/engine_impl.c +++ b/scenes/engine_impl.c @@ -15,6 +15,7 @@ DEFINE_COMP_MEMPOOL_BUF(CLifeTimer_t, MAX_COMP_POOL_SIZE) DEFINE_COMP_MEMPOOL_BUF(CWaterRunner_t, 32) DEFINE_COMP_MEMPOOL_BUF(CAirTimer_t, 8) DEFINE_COMP_MEMPOOL_BUF(CEmitter_t, 32) +DEFINE_COMP_MEMPOOL_BUF(CSquishable_t, MAX_COMP_POOL_SIZE) // Component mempools are added in the order of the component enums BEGIN_DEFINE_COMP_MEMPOOL @@ -30,4 +31,5 @@ BEGIN_DEFINE_COMP_MEMPOOL ADD_COMP_MEMPOOL(CWaterRunner_t) ADD_COMP_MEMPOOL(CAirTimer_t) ADD_COMP_MEMPOOL(CEmitter_t) + ADD_COMP_MEMPOOL(CSquishable_t) END_DEFINE_COMP_MEMPOOL diff --git a/scenes/game_systems.c b/scenes/game_systems.c index 0a5b2a4..a0e6fc9 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -583,13 +583,16 @@ void player_crushing_system(Scene_t* scene) { LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); - Entity_t* p_player; - sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) + //sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player) + CSquishable_t* p_squish; + unsigned int ent_idx; + sc_map_foreach(&scene->ent_manager.component_map[CSQUISHABLE_T], ent_idx, p_squish) { - CBBox_t* p_bbox = get_component(p_player, CBBOX_COMP_T); + Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); + CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T); uint8_t edges = check_bbox_edges( - &data->tilemap, p_player, + &data->tilemap, p_ent, p_bbox->size, true ); @@ -601,14 +604,14 @@ void player_crushing_system(Scene_t* scene) uint8_t collide = 0; CollideEntity_t ent = { - .p_ent = p_player, - .bbox = (Rectangle){p_player->position.x, p_player->position.y, p_bbox->size.x, 1}, - .prev_bbox = (Rectangle){p_player->position.x, p_player->position.y, p_bbox->size.x, p_bbox->size.y}, + .p_ent = p_ent, + .bbox = (Rectangle){p_ent->position.x, p_ent->position.y, p_bbox->size.x, 1}, + .prev_bbox = (Rectangle){p_ent->position.x, p_ent->position.y, p_bbox->size.x, p_bbox->size.y}, .area = (TileArea_t){ - .tile_x1 = (p_player->position.x) / TILE_SIZE, - .tile_y1 = (p_player->position.y) / TILE_SIZE, - .tile_x2 = (p_player->position.x + p_bbox->size.x - 1) / TILE_SIZE, - .tile_y2 = (p_player->position.y + p_bbox->size.y - 1) / TILE_SIZE, + .tile_x1 = (p_ent->position.x) / TILE_SIZE, + .tile_y1 = (p_ent->position.y) / TILE_SIZE, + .tile_x2 = (p_ent->position.x + p_bbox->size.x - 1) / TILE_SIZE, + .tile_y2 = (p_ent->position.y + p_bbox->size.y - 1) / TILE_SIZE, } }; @@ -619,7 +622,7 @@ void player_crushing_system(Scene_t* scene) collide |= 1 << 1; } - ent.bbox.y = p_player->position.y + p_bbox->size.y; + ent.bbox.y = p_ent->position.y + p_bbox->size.y; collide_type = check_collision_line(&ent, &data->tilemap, true); if (collide_type == 1) { @@ -628,7 +631,14 @@ void player_crushing_system(Scene_t* scene) if (collide != 0) { - p_player->m_alive = false; + if (p_ent->m_tag == PLAYER_ENT_TAG) + { + p_ent->m_alive = false; + } + else + { + destroy_entity(scene, &data->tilemap, p_ent); + } return; } } @@ -638,14 +648,14 @@ void player_crushing_system(Scene_t* scene) uint8_t collide = 0; CollideEntity_t ent = { - .p_ent = p_player, - .bbox = (Rectangle){p_player->position.x, p_player->position.y, 1, p_bbox->size.y}, - .prev_bbox = (Rectangle){p_player->position.x, p_player->position.y, p_bbox->size.x, p_bbox->size.y}, + .p_ent = p_ent, + .bbox = (Rectangle){p_ent->position.x, p_ent->position.y, 1, p_bbox->size.y}, + .prev_bbox = (Rectangle){p_ent->position.x, p_ent->position.y, p_bbox->size.x, p_bbox->size.y}, .area = (TileArea_t){ - .tile_x1 = (p_player->position.x) / TILE_SIZE, - .tile_y1 = (p_player->position.y) / TILE_SIZE, - .tile_x2 = (p_player->position.x + p_bbox->size.x - 1) / TILE_SIZE, - .tile_y2 = (p_player->position.y + p_bbox->size.y - 1) / TILE_SIZE, + .tile_x1 = (p_ent->position.x) / TILE_SIZE, + .tile_y1 = (p_ent->position.y) / TILE_SIZE, + .tile_x2 = (p_ent->position.x + p_bbox->size.x - 1) / TILE_SIZE, + .tile_y2 = (p_ent->position.y + p_bbox->size.y - 1) / TILE_SIZE, } }; @@ -657,7 +667,7 @@ void player_crushing_system(Scene_t* scene) } //Right - ent.bbox.x = p_player->position.x + p_bbox->size.x; // 2 to account for the previous subtraction + ent.bbox.x = p_ent->position.x + p_bbox->size.x; // 2 to account for the previous subtraction collide_type = check_collision_line(&ent, &data->tilemap, false); if (collide_type == 1) { @@ -666,7 +676,14 @@ void player_crushing_system(Scene_t* scene) if (collide != 0) { - p_player->m_alive = false; + if (p_ent->m_tag == PLAYER_ENT_TAG) + { + p_ent->m_alive = false; + } + else + { + destroy_entity(scene, &data->tilemap, p_ent); + } return; } } diff --git a/scenes/items_ent.c b/scenes/items_ent.c index e6eae23..6979c82 100644 --- a/scenes/items_ent.c +++ b/scenes/items_ent.c @@ -268,12 +268,13 @@ Entity_t* create_urchin(EntityManager_t* ent_manager) CHitBoxes_t* p_hitbox = add_component(p_urchin, CHITBOXES_T); p_hitbox->n_boxes = 1; p_hitbox->boxes[0] = (Rectangle){3, 3, 26, 26}; - p_hitbox->atk = 1; + p_hitbox->atk = 2; CSprite_t* p_cspr = add_component(p_urchin, CSPRITE_T); p_cspr->sprites = item_sprite_map; p_cspr->current_idx = 21; + add_component(p_urchin, CSQUISHABLE_T); return p_urchin; } diff --git a/scenes/player_ent.c b/scenes/player_ent.c index 15f003d..8323f12 100644 --- a/scenes/player_ent.c +++ b/scenes/player_ent.c @@ -115,6 +115,8 @@ Entity_t* create_player(EntityManager_t* ent_manager) CEmitter_t* p_emitter = add_component(p_ent, CEMITTER_T); p_emitter->offset = (Vector2){7,0}; + add_component(p_ent, CSQUISHABLE_T); + return p_ent; }