3

我正在尝试为我的游戏引擎实现一个简单的 ECS。我知道我的实现并不是严格意义上的 ECS,但我正在重构我的代码,使其更加基于组件。到目前为止,我有以下课程:

Entity: 它是一个组件容器,因为我希望我的实体有多个相同类型的组件,所以它将它们存储在一个 std::map<ComponentID,std::vector<std::unique_ptr<Component>>>. 每个组件都有一个唯一的 ID(一个无符号整数),这是我从网上学到的一个简单模板技巧中获得的:

一个名为 GetUniqueComponentID 的函数:

using ComponentID = unsigned int;

inline ComponentID GetUniqueComponentID()
{
    static ComponentID id = 0;

    return id++;
}

包含一个简单地生成递增数字的计数器。我从一个名为 GetComponentID 的函数模板中调用此函数:

template <typename T>
ComponentID GetComponentID()
{
    static ComponentID id = GetUniqueComponentID();

    return id;
}

此模板为我添加到实体中的每个组件实例化一个不同的函数,因此需要检索组件的代码可以使用 索引映射GetComponentId<Component_type>,并将具体组件类型作为函数的模板参数。

实体类具有 AddComponent 和 GetComponent 等方法,它们分别创建组件并将其添加到实体中,并检索组件(如果存在):

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;
}

由于我想存储多个相同类型的组件,我将它们存储在std::map<ComponentID,std::vector<std::unique_ptr<Component>>>.

现在我的问题是:

我需要为一种组件类型创建组件层次结构:我有一个 ForceGenerator 组件,它是各种具体 ForceGenerator(弹簧、重力等)的(抽象)基类。所以我需要创建具体的组件,但我需要通过指向基类的指针多态地使用它们:我的物理子系统只需要关心指向基本 ForceGenerator 的指针,调用它的 Update() 方法来更新力.

我不能使用当前的方法,因为每次创建特定的 ForceGenerator 组件时我都会使用不同的类型调用 AddComponent,而我需要将它们存储在同一个数组中(映射到基本 ForceGenerator 的组件 ID)。

我该如何解决这个问题?

4

2 回答 2

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 类中,而不是放在实现中。我编辑了源代码。

于 2019-03-15T17:30:23.337 回答
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;
    }
};

现在,您需要使用 ComponenteIDGenerator::GetComponentID() 而不是使用 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)都返回“NotRemappedForceType”的 id

最后恢复原始功能:

template<typename T>
ComponentID GetComponentID() {
    return ComponentIDGenerator<T>::GetComponentID();
}
于 2019-03-15T17:47:42.187 回答