Made the components module work like textures and sound so that there's just one constant map of components.

master
Zed A. Shaw 5 days ago
parent ab391aaa97
commit f208ca946e
  1. 42
      components.cpp
  2. 5
      components.hpp
  3. 7
      levelmanager.cpp
  4. 1
      levelmanager.hpp
  5. 1
      main.cpp
  6. 4
      systems.cpp
  7. 2
      systems.hpp
  8. 13
      tests/components.cpp
  9. 2
      worldbuilder.cpp
  10. 6
      worldbuilder.hpp

@ -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<BossFight>(component_map);
components::enroll<Combat>(component_map);
components::enroll<Position>(component_map);
components::enroll<Weapon>(component_map);
components::enroll<Curative>(component_map);
components::enroll<EnemyConfig>(component_map);
components::enroll<Personality>(component_map);
components::enroll<Tile>(component_map);
components::enroll<Motion>(component_map);
components::enroll<LightSource>(component_map);
components::enroll<Device>(component_map);
components::enroll<Sprite>(component_map);
components::enroll<Animation>(component_map);
components::enroll<Sound>(component_map);
void init() {
if(!MAP_configured) {
components::enroll<BossFight>(MAP);
components::enroll<Combat>(MAP);
components::enroll<Position>(MAP);
components::enroll<Weapon>(MAP);
components::enroll<Curative>(MAP);
components::enroll<EnemyConfig>(MAP);
components::enroll<Personality>(MAP);
components::enroll<Tile>(MAP);
components::enroll<Motion>(MAP);
components::enroll<LightSource>(MAP);
components::enroll<Device>(MAP);
components::enroll<Sprite>(MAP);
components::enroll<Animation>(MAP);
components::enroll<Sound>(MAP);
MAP_configured = true;
}
}
}

@ -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);
}

@ -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<gui::BossFightUI> LevelManager::create_bossfight(shared_ptr<DinkyECS:
auto& boss_data = config.bosses[level_name];
auto boss_id = world->entity();
components::configure_entity($components, *world, boss_id, boss_data["components"]);
components::configure_entity(*world, boss_id, boss_data["components"]);
return make_shared<gui::BossFightUI>(world, boss_id);
}
@ -59,7 +58,7 @@ DinkyECS::Entity LevelManager::spawn_enemy(std::string named) {
auto &config = level.world->get_the<GameConfig>();
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<DinkyECS::World> prev_world) {
auto scaling = scale_level();
auto map = make_shared<Map>(scaling.map_width, scaling.map_height);
WorldBuilder builder(*map, $components);
WorldBuilder builder(*map);
builder.generate(*world);
size_t index = $levels.size();

@ -27,7 +27,6 @@ struct LevelScaling {
class LevelManager {
public:
components::ComponentMap $components;
std::vector<GameLevel> $levels;
size_t $current_level = 0;

@ -10,6 +10,7 @@
int main(int argc, char* argv[]) {
try {
components::init();
sfml::Backend backend;
guecs::init(&backend);
ai::init("assets/ai.json");

@ -162,7 +162,7 @@ void System::distribute_loot(DinkyECS::World &world, DinkyECS::Entity& ent, nloh
world.set<ritual::JunkPile>(ent, pile);
}
void System::death(GameLevel &level, components::ComponentMap& components) {
void System::death(GameLevel &level) {
auto &world = *level.world;
auto player = world.get_the<Player>();
auto& config = world.get_the<GameConfig>();
@ -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);
}

@ -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);

@ -12,8 +12,7 @@ TEST_CASE("confirm component loading works", "[components]") {
std::vector<std::string> 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<BossFight>(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<BossFight>(devils_fingers);
REQUIRE(boss2.stage != std::nullopt);
}

@ -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);

@ -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();

Loading…
Cancel
Save