From a0f09710b0d85a841c3ca4da18484f977870c05d Mon Sep 17 00:00:00 2001 From: NukeBird Date: Fri, 14 Feb 2025 23:56:01 +0300 Subject: [PATCH] Component retrieval --- tests/zecsy.cpp | 21 +++++++++++++++++++++ zecsy.hpp | 6 ++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index 2659208..cfa96b5 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -1,4 +1,6 @@ #include +#include +#include #define CATCH_CONFIG_MAIN #include @@ -92,3 +94,22 @@ TEST_CASE("Attach a simple component to an entity and verify it is correctly ass REQUIRE(e2.has()); REQUIRE(w.has(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().value == "zecsy!"); + + e.get().value = "super-zecsy!"; + + REQUIRE(e.get().value == "super-zecsy!"); +} diff --git a/zecsy.hpp b/zecsy.hpp index 7b5fe27..ac3e2a2 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -190,9 +190,9 @@ namespace zecsy } template - 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)]; @@ -202,6 +202,8 @@ namespace zecsy storage.resize(old_size + T_size); void* ptr = &storage[0] + old_size; new(ptr) T(comp); + + entity_dict[typeid(T)][e] = old_size / T_size; } template