Clean up the DinkyECSso that it's easier to understand and more obvious what a fact vs. component is.

This commit is contained in:
Zed A. Shaw 2024-10-29 07:33:00 -04:00
parent 4ed06b10b1
commit 143fe7784c
7 changed files with 82 additions and 105 deletions

View file

@ -33,25 +33,25 @@ namespace DinkyECS {
}
template <typename Comp>
void set(Comp val) {
void set_the(Comp val) {
$facts[std::type_index(typeid(Comp))] = val;
}
template <typename Comp>
Comp &get() {
Comp &get_the() {
// use .at to get std::out_of_range if fact not set
std::any &res = $facts.at(std::type_index(typeid(Comp)));
return std::any_cast<Comp&>(res);
}
template <typename Comp>
void assign(Entity ent, Comp val) {
void set(Entity ent, Comp val) {
EntityMap &map = entity_map_for<Comp>();
map[ent] = val;
}
template <typename Comp>
Comp &component(Entity ent) {
Comp &get(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
// use .at for bounds checking
std::any &res = map.at(ent);
@ -59,7 +59,7 @@ namespace DinkyECS {
}
template<typename Comp>
void system(std::function<void(const Entity&, Comp&)> cb) {
void query(std::function<void(const Entity&, Comp&)> cb) {
EntityMap &map = entity_map_for<Comp>();
for(auto& [entity, any_comp] : map) {
Comp &res = std::any_cast<Comp&>(any_comp);
@ -68,31 +68,17 @@ namespace DinkyECS {
}
template<typename CompA, typename CompB>
void system(std::function<void(const Entity&, CompA&, CompB&)> cb) {
void query(std::function<void(const Entity&, CompA&, CompB&)> cb) {
EntityMap &map_a = entity_map_for<CompA>();
EntityMap &map_b = entity_map_for<CompB>();
for(auto& [entity, any_a] : map_a) {
if(map_b.contains(entity)) {
CompA &res_a = std::any_cast<CompA&>(any_a);
CompB &res_b = component<CompB>(entity);
CompB &res_b = get<CompB>(entity);
cb(entity, res_a, res_b);
}
}
}
template<typename CompA, typename CompB>
std::function<void()> runner(std::function<void(const Entity&, CompA&, CompB&)> cb) {
return [&]{
system<CompA, CompB>(cb);
};
}
template<typename CompA>
std::function<void()> runner(std::function<void(const Entity&, CompA&)> cb) {
return [&]{
system<CompA>(cb);
};
}
};
}