Implement ensure method
This commit is contained in:
parent
e99adff481
commit
5afe852f75
2 changed files with 23 additions and 4 deletions
|
@ -67,13 +67,12 @@ TEST_CASE("Attach a simple component to an entity and verify it is correctly "
|
|||
world w;
|
||||
|
||||
auto e1 = w.make_entity();
|
||||
w.set(e1, ChoosenOne{});
|
||||
|
||||
w.set(e1, ChoosenOne{});
|
||||
REQUIRE(w.has<ChoosenOne>(e1));
|
||||
|
||||
auto e2 = w.make_entity();
|
||||
w.set(e2, ChoosenOne{});
|
||||
|
||||
REQUIRE(w.has<ChoosenOne>(e2));
|
||||
}
|
||||
|
||||
|
@ -94,8 +93,15 @@ TEST_CASE("Retrieve a component from an entity and verify its data matches "
|
|||
REQUIRE(w.get<Comp>(e).v == 0);
|
||||
|
||||
w.get<Comp>(e).v = 77;
|
||||
|
||||
REQUIRE(w.get<Comp>(e).v == 77);
|
||||
|
||||
w.ensure<Comp>(e).v = 4;
|
||||
REQUIRE(w.ensure<Comp>(e).v == 4);
|
||||
REQUIRE(w.get<Comp>(e).v == 4);
|
||||
|
||||
w.remove<Comp>(e);
|
||||
w.ensure<Comp>(e).v = 123;
|
||||
REQUIRE(w.get<Comp>(e).v == 123);
|
||||
}
|
||||
|
||||
TEST_CASE(
|
||||
|
@ -112,7 +118,6 @@ TEST_CASE(
|
|||
|
||||
w.set(e, ChoosenOne{});
|
||||
REQUIRE_NOTHROW(w.remove<ChoosenOne>(e));
|
||||
|
||||
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
||||
}
|
||||
|
||||
|
|
14
zecsy.hpp
14
zecsy.hpp
|
@ -70,6 +70,9 @@ namespace zecsy
|
|||
void set(entity_id e, const First& comp0, const Second& comp1,
|
||||
const Rest&... rest_comps);
|
||||
|
||||
template<Component T>
|
||||
T& ensure(entity_id e);
|
||||
|
||||
template<Component T>
|
||||
void remove(entity_id e);
|
||||
|
||||
|
@ -219,6 +222,17 @@ namespace zecsy
|
|||
return false;
|
||||
}
|
||||
|
||||
template<Component T>
|
||||
T& world::ensure(entity_id e)
|
||||
{
|
||||
if(!has<T>(e))
|
||||
{
|
||||
set<T>(e);
|
||||
}
|
||||
|
||||
return get<T>(e);
|
||||
}
|
||||
|
||||
template<Component T>
|
||||
inline T& world::get(entity_id e)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue