我正在尝试在C ++ 11/14中静态实现实体组件系统模式。我已经设法为我的实体创建一个容器,我正在尝试添加功能来处理里面的数据。这是我到目前为止的代码:
template<typename... Components>
struct Entity {
std::tuple<Components...> comps;
Entity() = default;
Entity(Components... cs) {
comps = std::make_tuple(cs...);
};
};
template<typename... EntityTypes>
struct EntityCollection {
std::tuple<std::vector<EntityTypes>...> entities;
template<typename EntityType>
void add(EntityType e) {
auto& cont = std::get<std::vector<EntityType>>(entities);
cont.push_back(e);
};
template<typename... Components, typename F>
void for_each_containing(F f) {
// todo... apply f on all entities for which 'Components'
// are a subset of those in the entity.
};
};
我已经看到使用boost fusion(以及stl适配器)循环遍历元组中所有元素的代码和这样的通用lambda:
// inside entity class
void update() {
for_each(comps, [](auto& c) { c.update(); }; // assuming all components has an update function
};
但是,我想将函数应用于包含一组特定组件的所有实体,如for_each_containing函数建议的那样。我一直在尝试使用融合中的filter_view编写一些东西,但是没有成功......
任何帮助都非常感谢!
答案 0 :(得分:5)
实用工具:
#include <iostream>
#include <tuple>
#include <vector>
#include <type_traits>
template <typename... Ts>
struct pack {};
template <typename T, typename... Ts>
constexpr bool Contains = false;
template <typename T, typename U, typename... Ts>
constexpr bool Contains<T, U, Ts...> = Contains<T, Ts...>;
template <typename T, typename... Ts>
constexpr bool Contains<T, T, Ts...> = true;
template <bool...>
constexpr bool All = true;
template <bool Head, bool... Tail>
constexpr bool All<Head, Tail...> = Head && All<Tail...>;
template <typename Subset, typename Set>
struct IsSubset;
template <typename... Ts, typename... Us>
struct IsSubset<pack<Ts...>, pack<Us...>>
: std::integral_constant<bool, All<Contains<Ts, Us...>...>> {};
带有示例Entity
成员函数的 update
:
template <typename... Components>
struct Entity
{
std::tuple<Components...> comps;
Entity() = default;
Entity(Components... cs)
: comps(cs...)
{
}
void update()
{
std::cout << "Updating " << __PRETTY_FUNCTION__ << std::endl;
}
};
带有EntityCollection
功能的 apply
:
template <typename... EntityTypes>
struct EntityCollection
{
std::tuple<std::vector<EntityTypes>...> entities;
template <typename EntityType>
void add(EntityType e)
{
auto& cont = std::get<std::vector<EntityType>>(entities);
cont.push_back(e);
}
template <typename... Components, typename F>
void for_each_containing(F f)
{
using expander = int[];
(void)expander{ 0, (void(
apply<Components...>(std::get<std::vector<EntityTypes>>(entities), f)
), 0)... };
}
template <typename... Components, typename... Ts, typename F>
auto apply(std::vector<Entity<Ts...>>& entities, F f)
-> std::enable_if_t<IsSubset<pack<Components...>, pack<Ts...>>{}>
{
for (auto& v : entities)
{
f(v);
}
}
template <typename... Components, typename... Ts, typename F>
auto apply(std::vector<Entity<Ts...>>&, F)
-> std::enable_if_t<!IsSubset<pack<Components...>, pack<Ts...>>{}>
{
}
};
试验:
int main()
{
Entity<int, short> a;
Entity<long, int> b;
Entity<float, double> c;
EntityCollection<Entity<int, short>,
Entity<long, int>,
Entity<float, double>> collection;
collection.add(a);
collection.add(b);
collection.add(c);
collection.for_each_containing<int>([](auto& c){ c.update(); });
collection.for_each_containing<float>([](auto& c){ c.update(); });
collection.for_each_containing<short>([](auto& c){ c.update(); });
collection.for_each_containing<short, int>([](auto& c){ c.update(); });
}
输出:
Updating void Entity<int, short>::update() [Components = <int, short>]
Updating void Entity<long, int>::update() [Components = <long, int>]
Updating void Entity<float, double>::update() [Components = <float, double>]
Updating void Entity<int, short>::update() [Components = <int, short>]
Updating void Entity<int, short>::update() [Components = <int, short>]