Component retrieval

This commit is contained in:
NukeBird 2025-02-14 23:56:01 +03:00
parent 21c7e416ec
commit a0f09710b0
2 changed files with 25 additions and 2 deletions

View file

@ -1,4 +1,6 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/internal/catch_test_spec_parser.hpp>
#include <string>
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp> #include <catch2/catch_all.hpp>
@ -92,3 +94,22 @@ TEST_CASE("Attach a simple component to an entity and verify it is correctly ass
REQUIRE(e2.has<ChoosenOne>()); REQUIRE(e2.has<ChoosenOne>());
REQUIRE(w.has<ChoosenOne>(e2)); REQUIRE(w.has<ChoosenOne>(e2));
} }
struct Name
{
std::string value;
};
TEST_CASE("Retrieve a component from an entity and verify its data matches what was set")
{
world w;
auto e = w.make_entity();
e.set(Name{"zecsy!"});
REQUIRE(e.get<Name>().value == "zecsy!");
e.get<Name>().value = "super-zecsy!";
REQUIRE(e.get<Name>().value == "super-zecsy!");
}

View file

@ -190,9 +190,9 @@ namespace zecsy
} }
template<typename T> template<typename T>
inline void component_storage::set(entity_id id, const T& comp) inline void component_storage::set(entity_id e, const T& comp)
{ {
bitset_dict[typeid(T)].set(id); bitset_dict[typeid(T)].set(e);
auto& storage = storage_dict[typeid(T)]; auto& storage = storage_dict[typeid(T)];
@ -202,6 +202,8 @@ namespace zecsy
storage.resize(old_size + T_size); storage.resize(old_size + T_size);
void* ptr = &storage[0] + old_size; void* ptr = &storage[0] + old_size;
new(ptr) T(comp); new(ptr) T(comp);
entity_dict[typeid(T)][e] = old_size / T_size;
} }
template<typename T> template<typename T>