From f208ca946e59d6b3f2469c43548fdfb15b33fde6 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 2 Jun 2025 23:33:59 -0400 Subject: [PATCH] Made the components module work like textures and sound so that there's just one constant map of components. --- components.cpp | 42 ++++++++++++++++++++++++------------------ components.hpp | 5 +++-- levelmanager.cpp | 7 +++---- levelmanager.hpp | 1 - main.cpp | 1 + systems.cpp | 4 ++-- systems.hpp | 2 +- tests/components.cpp | 13 +++++-------- worldbuilder.cpp | 2 +- worldbuilder.hpp | 6 ++---- 10 files changed, 42 insertions(+), 41 deletions(-) diff --git a/components.cpp b/components.cpp index d5e586e..77fcd54 100644 --- a/components.cpp +++ b/components.cpp @@ -3,28 +3,34 @@ #include "easings.hpp" namespace components { - void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data) { + static ComponentMap MAP; + static bool MAP_configured = false; + + void configure_entity(DinkyECS::World& world, DinkyECS::Entity ent, json& data) { for (auto &i : data) { dbc::check(i.contains("_type") && i["_type"].is_string(), fmt::format("component has no _type: {}", data.dump())); - dbc::check(component_map.contains(i["_type"]), fmt::format("component_map doesn't have type {}", std::string(i["_type"]))); - component_map.at(i["_type"])(world, ent, i); + dbc::check(MAP.contains(i["_type"]), fmt::format("MAP doesn't have type {}", std::string(i["_type"]))); + MAP.at(i["_type"])(world, ent, i); } } - void configure(ComponentMap& component_map) { - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); - components::enroll(component_map); + void init() { + if(!MAP_configured) { + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + components::enroll(MAP); + MAP_configured = true; + } } } diff --git a/components.hpp b/components.hpp index a2b506f..c30ed93 100644 --- a/components.hpp +++ b/components.hpp @@ -181,8 +181,9 @@ namespace components { }; } - void configure(ComponentMap& component_map); - void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data); + void init(); + + void configure_entity(DinkyECS::World& world, DinkyECS::Entity ent, json& data); } diff --git a/levelmanager.cpp b/levelmanager.cpp index 7b43864..5039035 100644 --- a/levelmanager.cpp +++ b/levelmanager.cpp @@ -11,7 +11,6 @@ using std::shared_ptr, std::make_shared; using namespace components; LevelManager::LevelManager() { - components::configure($components); create_level(); } @@ -47,7 +46,7 @@ shared_ptr LevelManager::create_bossfight(shared_ptrentity(); - components::configure_entity($components, *world, boss_id, boss_data["components"]); + components::configure_entity(*world, boss_id, boss_data["components"]); return make_shared(world, boss_id); } @@ -59,7 +58,7 @@ DinkyECS::Entity LevelManager::spawn_enemy(std::string named) { auto &config = level.world->get_the(); auto entity_data = config.enemies[named]; - WorldBuilder builder(*level.map, $components); + WorldBuilder builder(*level.map); auto entity_id = builder.configure_entity_in_room(*level.world, entity_data, 0); @@ -87,7 +86,7 @@ size_t LevelManager::create_level(shared_ptr prev_world) { auto scaling = scale_level(); auto map = make_shared(scaling.map_width, scaling.map_height); - WorldBuilder builder(*map, $components); + WorldBuilder builder(*map); builder.generate(*world); size_t index = $levels.size(); diff --git a/levelmanager.hpp b/levelmanager.hpp index 98784bf..eaa0aa6 100644 --- a/levelmanager.hpp +++ b/levelmanager.hpp @@ -27,7 +27,6 @@ struct LevelScaling { class LevelManager { public: - components::ComponentMap $components; std::vector $levels; size_t $current_level = 0; diff --git a/main.cpp b/main.cpp index 1a15ed0..24fab23 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,7 @@ int main(int argc, char* argv[]) { try { + components::init(); sfml::Backend backend; guecs::init(&backend); ai::init("assets/ai.json"); diff --git a/systems.cpp b/systems.cpp index ee7d21e..35ba130 100644 --- a/systems.cpp +++ b/systems.cpp @@ -162,7 +162,7 @@ void System::distribute_loot(DinkyECS::World &world, DinkyECS::Entity& ent, nloh world.set(ent, pile); } -void System::death(GameLevel &level, components::ComponentMap& components) { +void System::death(GameLevel &level) { auto &world = *level.world; auto player = world.get_the(); auto& config = world.get_the(); @@ -207,7 +207,7 @@ void System::death(GameLevel &level, components::ComponentMap& components) { } auto entity_data = config.items["GRAVE_STONE"]; - components::configure_entity(components, world, ent, entity_data["components"]); + components::configure_entity(world, ent, entity_data["components"]); if(entity_data["inventory_count"] > 0) { System::distribute_loot(world, ent, entity_data); } diff --git a/systems.hpp b/systems.hpp index e0b90c7..5bc3329 100644 --- a/systems.hpp +++ b/systems.hpp @@ -9,7 +9,7 @@ namespace System { void lighting(GameLevel &level); void motion(GameLevel &level); void collision(GameLevel &level); - void death(GameLevel &level, components::ComponentMap& components); + void death(GameLevel &level); void generate_paths(GameLevel &level); void enemy_pathing(GameLevel &level); void enemy_ai_initialize(GameLevel &level); diff --git a/tests/components.cpp b/tests/components.cpp index bdca7e2..538dc8b 100644 --- a/tests/components.cpp +++ b/tests/components.cpp @@ -12,8 +12,7 @@ TEST_CASE("confirm component loading works", "[components]") { std::vector test_list{ "assets/enemies.json", "assets/items.json", "assets/devices.json"}; - components::ComponentMap comp_map; - components::configure(comp_map); + components::init(); DinkyECS::World world; for(auto test_data : test_list) { @@ -24,7 +23,7 @@ TEST_CASE("confirm component loading works", "[components]") { auto& components = data["components"]; fmt::println("TEST COMPONENT: {} from file {}", key, test_data); auto ent = world.entity(); - components::configure_entity(comp_map, world, ent, components); + components::configure_entity(world, ent, components); } } } @@ -42,21 +41,19 @@ TEST_CASE("make sure json_mods works", "[components]") { } // this then confirms everything else about the json conversion - - ComponentMap comp_map; - components::configure(comp_map); + components::init(); DinkyECS::World world; auto rat_king = world.entity(); - components::configure_entity(comp_map, world, rat_king, config["RAT_KING"]["components"]); + components::configure_entity(world, rat_king, config["RAT_KING"]["components"]); auto boss = world.get(rat_king); REQUIRE(boss.stage == std::nullopt); // now load the other one for the other way optional is used auto devils_fingers = world.entity(); - components::configure_entity(comp_map, world, devils_fingers, config["DEVILS_FINGERS"]["components"]); + components::configure_entity(world, devils_fingers, config["DEVILS_FINGERS"]["components"]); auto boss2 = world.get(devils_fingers); REQUIRE(boss2.stage != std::nullopt); } diff --git a/worldbuilder.cpp b/worldbuilder.cpp index add538d..dc8e013 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -93,7 +93,7 @@ DinkyECS::Entity WorldBuilder::configure_entity_in_map(DinkyECS::World &world, j } if(entity_data.contains("components")) { - components::configure_entity($components, world, item, entity_data["components"]); + components::configure_entity(world, item, entity_data["components"]); } $collision.insert(pos, item); diff --git a/worldbuilder.hpp b/worldbuilder.hpp index 588fbd7..ffba7d1 100644 --- a/worldbuilder.hpp +++ b/worldbuilder.hpp @@ -8,12 +8,10 @@ class WorldBuilder { public: Map& $map; - components::ComponentMap& $components; SpatialMap $collision; - WorldBuilder(Map &map, components::ComponentMap& components) : - $map(map), - $components(components) + WorldBuilder(Map &map) : + $map(map) { } void generate_map();