diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index f6135e8..a5c8c92 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -39,16 +39,18 @@ TEST_CASE("Entity #0 should be reserved and never used") REQUIRE_FALSE(entity().is_alive()); } +//TODO: doesn't make sense anymore? (I stopped limitting amount of entities) TEST_CASE("World should throw on id overflow") { - world w; - - for(int i = 0; i < MAX_ZECSY_ENTITIES; ++i) - { - w.make_entity(); - } - - REQUIRE_THROWS(w.make_entity()); + /*world w;*/ + /**/ + /*for(int i = 0; i < MAX_ZECSY_ENTITIES; ++i)*/ + /*{*/ + /* w.make_entity();*/ + /*}*/ + /**/ + /*REQUIRE_THROWS(w.make_entity());*/ + REQUIRE(true); } struct ChoosenOne diff --git a/zecsy.hpp b/zecsy.hpp index 0566cc9..8f534ee 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -1,21 +1,17 @@ #pragma once #include #include -#include #include #include #include #include #include - -#ifndef MAX_ZECSY_ENTITIES - #define MAX_ZECSY_ENTITIES 65536 -#endif // !MAX_ZECSY_ENTITIES - +#include namespace zecsy { using entity_id = uint64_t; + using entities_set = std::unordered_set; class entity final { @@ -46,8 +42,6 @@ namespace zecsy class world* w = nullptr; }; - using zecsy_bits = std::bitset; - class component_storage { public: @@ -80,7 +74,7 @@ namespace zecsy void remove(entity_id e); private: using comp_index = std::type_index; - using comp_to_bitset = std::unordered_map; + using comp_to_entities_set = std::unordered_map; using entity_to_index = std::unordered_map; using comp_to_entity_dict = std::unordered_map>; - comp_to_bitset bitset_dict; - comp_to_entity_dict entity_dict; + comp_to_entities_set entities_dict; + comp_to_entity_dict indices_dict; comp_to_storage storage_dict; comp_to_reusable_ids reusable_id_queues; }; @@ -120,7 +114,7 @@ namespace zecsy template void remove(entity_id e); private: - zecsy_bits entities_bitset; + entities_set alive_entities; entity_id entity_counter = 0; component_storage storage; }; @@ -143,33 +137,27 @@ namespace zecsy inline entity world::make_entity() { auto id = ++entity_counter; - - if(id > MAX_ZECSY_ENTITIES) - { - throw std::runtime_error(std::format("Entity id {} exceeds " - "MAX_ZECSY_ENTITIES ({})", id, MAX_ZECSY_ENTITIES)); - } + alive_entities.emplace(id); - entities_bitset.set(id); return entity(this, id); } inline void world::destroy_entity(entity_id e) { - entities_bitset.reset(e); + alive_entities.erase(e); } inline bool world::is_alive(entity_id e) const { - return entities_bitset.test(e); + return alive_entities.contains(e); } template inline bool component_storage::has(entity_id e) const { - if(bitset_dict.contains(typeid(T))) + if(entities_dict.contains(typeid(T))) { - return bitset_dict.at(typeid(T)).test(e); + return entities_dict.at(typeid(T)).contains(e); } return false; } @@ -183,7 +171,7 @@ namespace zecsy e, typeid(T).name())); } - auto index = entity_dict[typeid(T)].at(e); + auto index = indices_dict[typeid(T)].at(e); auto* ptr = reinterpret_cast(&storage_dict[typeid(T)][0]); ptr += index; @@ -218,7 +206,7 @@ namespace zecsy template inline void component_storage::set(entity_id e, const T& comp) { - bitset_dict[typeid(T)].set(e); + entities_dict[typeid(T)].emplace(e); auto& storage = storage_dict[typeid(T)]; @@ -233,7 +221,7 @@ namespace zecsy void* ptr = &storage[0] + old_size; new(ptr) T(comp); - entity_dict[typeid(T)][e] = old_size / T_size; + indices_dict[typeid(T)][e] = old_size / T_size; return; } @@ -243,7 +231,7 @@ namespace zecsy auto ptr = reinterpret_cast(&storage[0]); new(ptr + index) T(comp); - entity_dict[typeid(T)][e] = index; + indices_dict[typeid(T)][e] = index; } template @@ -272,9 +260,9 @@ namespace zecsy return; } - bitset_dict[typeid(T)].reset(e); - reusable_id_queues[typeid(T)].push(entity_dict[typeid(T)][e]); - entity_dict[typeid(T)].erase(e); + entities_dict[typeid(T)].erase(e); + reusable_id_queues[typeid(T)].push(indices_dict[typeid(T)][e]); + indices_dict[typeid(T)].erase(e); } template