diff --git a/dinkyecs.hpp b/dinkyecs.hpp index 4e3b4b6..6652a88 100644 --- a/dinkyecs.hpp +++ b/dinkyecs.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace DinkyECS { @@ -16,8 +17,6 @@ namespace DinkyECS const Entity NONE = 0; - using EntityMap = std::unordered_map; - template struct ComponentStorage { std::vector data; @@ -29,17 +28,22 @@ namespace DinkyECS std::any data; }; - typedef std::queue EventQueue; + using EntityMap = std::unordered_map; + using EventQueue = std::queue; + using TypeMap = std::unordered_map; struct World { unsigned long entity_count = NONE+1; std::unordered_map $components; - std::unordered_map $facts; + std::shared_ptr $facts = nullptr; std::unordered_map $events; std::unordered_map $component_storages; std::unordered_map> $free_indices; std::unordered_map $constants; + World() : $facts(std::make_shared()) + {} + Entity entity() { return ++entity_count; } void destroy(DinkyECS::Entity entity) { @@ -135,26 +139,26 @@ namespace DinkyECS template void set_the(Comp val) { - $facts.insert_or_assign(std::type_index(typeid(Comp)), val); + $facts->insert_or_assign(std::type_index(typeid(Comp)), val); } template Comp &get_the() { auto comp_id = std::type_index(typeid(Comp)); - dbc::check($facts.contains(comp_id), + dbc::check($facts->contains(comp_id), fmt::format("!!!! ATTEMPT to access world fact that hasn't " "been set yet: {}", typeid(Comp).name())); // use .at to get std::out_of_range if fact not set - std::any &res = $facts.at(comp_id); + std::any &res = $facts->at(comp_id); return std::any_cast(res); } template bool has_the() { auto comp_id = std::type_index(typeid(Comp)); - return $facts.contains(comp_id); + return $facts->contains(comp_id); } template