我正在尝试为我的游戏引擎实现一个简单的ECS。我知道我的实现并不严格是ECS,但是我正在重构我的代码以使其更加基于组件。到目前为止,我有以下课程:
--build-arg
:这是一个组件容器,并且由于我希望我的实体具有多个相同类型的组件,因此将它们存储在一个
Entity
。每个组件都有一个唯一的ID(一个无符号的int),该ID是我从网上学到的一个简单的模板技巧获得的:
一个名为GetUniqueComponentID的函数:
std::map<ComponentID,std::vector<std::unique_ptr<Component>>>
包含一个计数器,该计数器仅生成递增的数字。 我从名为GetComponentID的函数模板中调用此函数:
using ComponentID = unsigned int;
inline ComponentID GetUniqueComponentID()
{
static ComponentID id = 0;
return id++;
}
此模板为我添加到实体中的每个组件实例化了一个不同的函数,因此需要检索组件的代码可以使用template <typename T>
ComponentID GetComponentID()
{
static ComponentID id = GetUniqueComponentID();
return id;
}
来索引地图,并将具体的组件类型作为该函数的模板参数
实体类具有AddComponent和GetComponent之类的方法,它们分别创建一个组件并将其添加到实体中,并检索一个组件(如果存在):
GetComponentId<Component_type>
由于我想存储相同类型的多个组件,因此将它们存储在class Entity
{
public:
Entity();
~Entity();
template <typename T, typename... TArgs>
T &AddComponent(TArgs&&... args);
template <typename T>
bool HasComponent();
//template <typename T>
//T &GetComponent();
template <typename T>
std::vector<T*> GetComponents();
bool IsAlive() { return mIsAlive; }
void Destroy() { mIsAlive = false; }
private:
//std::map<ComponentID, std::unique_ptr<Component>> mComponents; // single component per type
std::map<ComponentID, std::vector<std::unique_ptr<Component>>> mComponents; // multiple components per type
bool mIsAlive = true;
};
template <typename T, typename... TArgs>
T &Entity::AddComponent(TArgs&&... args)
{
T *c = new T(std::forward<TArgs>(args)...);
std::unique_ptr<Component> component(c);
component->SetEntity(this);
mComponents[GetComponentID<T>()].push_back(std::move(component));
return *c;
}
template <typename T>
bool Entity::HasComponent() // use bitset (faster)
{
std::map<ComponentID, std::vector<std::unique_ptr<Component>>>::iterator it = mComponents.find(GetComponentID<T>());
if (it != mComponents.end())
return true;
return false;
}
template <typename T>
std::vector<T*> Entity::GetComponents()
{
std::vector<T*> components;
for (std::unique_ptr<Component> &component : mComponents[GetComponentID<T>()])
components.push_back(static_cast<T*>(component.get()));
return components;
}
中。
现在我的问题是:
我需要为一种类型的组件创建一个组件层次结构:我有一个ForceGenerator组件,它是所有具体的ForceGenerator(弹簧,重力等)的(抽象)基类。因此,我需要创建具体的组件,但是我需要通过指向基类的指针来多态使用它们:我的物理子系统只需要关心指向基力生成器的指针,调用其Update()方法即可处理更新力
我无法使用当前方法,因为每次创建特定的ForceGenerator组件时,我都会使用不同的类型调用AddComponent,而我需要将它们存储在同一数组中(映射到基本ForceGenerator的组件ID) 。
我该如何解决这个问题?
答案 0 :(得分:1)
您可以使用以下默认模板参数:
class Entity
{
template <typename T,typename StoreAs=T, typename... TArgs>
T &Entity::AddComponent(TArgs&&... args);
};
template <typename T,typename StoreAs, typename... TArgs>
T &Entity::AddComponent(TArgs&&... args)
{
T *c = new T(std::forward<TArgs>(args)...);
std::unique_ptr<Component> component(c);
component->SetEntity(this);
mComponents[GetComponentID<StoreAs()].push_back(std::move(component));
return *c;
}
被称为
entity.AddComponent<T>(...)//Will instatiate AddComponent<T,T,...>
entity.AddComponent<T,U>(...)//Will instatiate AddComponent<T,U,...>
您甚至可以更进一步,仅在可以将组件存储为该类型时才使用一些SFINAE来启用此功能:(可能会或可能不会真正改善错误消息)
template <typename T,typename StoreAs, typename... TArgs>
std::enable_if_t<std::is_base_of_v<StoreAs,T>,T&> //Return type is `T&`
Entity::AddComponent(TArgs&&... args)
{
T *c = new T(std::forward<TArgs>(args)...);
std::unique_ptr<Component> component(c);
component->SetEntity(this);
mComponents[GetComponentID<StoreAs>()].push_back(std::move(component));
return *c;
}
我假设Component
是所有组件的基类。如果您有一组有限的已知组件,则可以将它们存储在std::variant<List types here>
中,而不是唯一的指针中。
编辑:
显然clang抱怨:“模板参数重新定义了默认参数”。 Gcc并不介意,只是为了正确,只将StoreAs
初始化StoreAs=T
放在Entity类中,而不放在实现中。我编辑了源代码。
答案 1 :(得分:1)
新提案
看看另一个答案,我有一个主意,您可以从另一个CRTP基类继承来定义存储位置(仅在使用映射存储时)。
示例:
//Just for check class
struct StoreAs {};
//Give the store type
template<typename T>
struct StoreAsT : public StoreAs {
using store_as_type = T;
};
//Some components
struct ComponentA { };
struct ComponentC { };
struct ComponentB : public StoreAsT<ComponentC> { };
//Dummy add
template<typename T>
void Add(T&& cmp) {
if constexpr(std::is_base_of_v<StoreAs, T>) {
std::cout << "Store as (remap)" << GetComponentID<typename T::store_as_type>() << std::endl;
} else {
std::cout << "Store as " << GetComponentID<T>() << std::endl;
}
}
//Example add
int main() {
Add(ComponentA {});
Add(ComponentB {});
Add(ComponentC {});
return 0;
}
输出:
Store as 0
Store as (remap)1
Store as 1
旧建议:
作为一种简单的方法,但很冗长而不是通用的解决方案,您可以扩展ID生成技巧:
template <typename T>
ComponentID GetComponentID()
{
static ComponentID id = GetUniqueComponentID();
return id;
}
到
template <typename T>
struct ComponentIDGenerator {
static ComponentID GetComponentID() {
static ComponentID id = GetUniqueComponentID();
return id;
}
};
现在,代替使用GetComponentID,您需要使用ComponenteIDGenerator :: GetComponentID(),但现在您可以创建特定的专业化。
因此,您可以专门重新映射一些ID:
template<>
struct ComponentIDGenerator<SomeForce1> {
static ComponentID GetComponentID() {
return ComponentIDGenerator<NotRemappedForceType>::GetComponentID();
}
};
template<>
struct ComponentIDGenerator<SomeForce2> {
static ComponentID GetComponentID() {
return ComponentIDGenerator<NotRemappedForceType>::GetComponentID();
}
};
现在(SomeFroce1和SomeForce2)都返回ID“ NotRemappedForceType”
并最后恢复原始功能:
template<typename T>
ComponentID GetComponentID() {
return ComponentIDGenerator<T>::GetComponentID();
}