如何在实体的基本组件列表中索引所有派生组件

如何在实体的基本组件列表中索引所有派生组件

本文介绍了如何在实体的基本组件列表中索引所有派生组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行仿真的实体组件系统设计.这就是现在让我感到困惑的地方.我正在尝试制作一个实体类

I am trying to do a entity component system design for simulation. Here is what confuses me now.I am trying to make a Entity class

Entity.h
class Entity
{
public:
    Entity();
    virtual ~Entity() = 0;
    //---------------------Methods---------------------//
    void AddComponent(const shared_ptr<Component> component);

    template<class T>
    T* GetComponent() const;

    //---------------------Members---------------------//
    vector<shared_ptr<Component>>   m_components;
}

Entity.cpp
template<typename T>
T* Entity::GetComponent() const
{
    Component::component_type_t typeIndex = /*T::component_type*/
    T* returnPtr = dynamic_pointer_cast<T>(m_components[component_type].get());
    return returnPtr;
}

Component类看起来像这样

And the Component class looks like this

class Component
{
public:
    Component();
    virtual ~Component() = 0;
    //---------------------Methods---------------------//

    //---------------------Members---------------------//
    typedef enum component_type_t
    {
        MESH_T,
        RIGIDBODY_T,
        TRANSFORM_T,
        NUM_TYPES
    };
    component_type_t componentType;
};

我要使用GetComponent()的方式与Unity3D中的方式相同

They way I want to use GetComponent() is the same as in Unity3D

Transform* t = GetComponent<Transform>()

但是正如您所看到的,我还没有找到一种方法来做到这一点.我以前做的方式是

But as you can see I cannot find a way to do that yet. The way I used to do it was

class Entity
{
    .....
    Component* GetComponent(Component::component_type_t componentType) const
    {
      return m_components[component_type].get()
    };
}

我只能将其用作

Transform* t = dynamic_pointer_cast<Transform>(GetComponent(Component::component_type_t::TRANSFORM_T));

显然这很乏味.

所以我的问题是我可以使用这种形式吗?

So my question is can I use some form as this?

class Entity
{
    .....
    template<class T>
    T* GetComponent() const
    {
    Component::component_type_t typeIndex = /*T::component_type*/
    T* returnPtr = dynamic_pointer_cast<T>(m_components[component_type].get());
    return returnPtr;
    };
}

我感觉保持使用std::vector<>来存储所有组件指针的简洁性和速度,这也是有效的设计吗?

I feel the cleanness and the speed to keep using std::vector<> for storing all my component pointers, is that also a valid design?

推荐答案

很久以前也做过类似的事情.我认为您正在这样做.您可以轻松地修改下面的代码以适合您的需要. Components是继承的.您可以删除继承并将其替换为类内部的类或Component类内部的所有组件类.

Did something similar long time ago. I think you are doing this the hard way. You can easily modify the code below to suit your need. The Components are inherited. You can remove the inheritance and replace it with class inside class or all component classes inside the Component class.

#include <iostream>

struct Vector3
{
    float x, y, z;
    Vector3(float x, float y){}
    Vector3(float x, float y,float z){}
};

template <class T>
class Component
{
public:
    T t;
    void adNewComponent(){

    }
};

class Mesh{
public:
    Mesh(){}
};

class Rigidbody{
public:
    Rigidbody(){}
    void AddForce(float x,float y, float z){}
    void AddForce(Vector3 force){}
};


class Transform{
public:
    Transform(){}
};

class Object{

};

class GameObject:Object,
    Component<Mesh>,
    Component<Rigidbody>,
    Component<Transform>
{
public:
     template <class T>
     T &GetComponent()
    {
        return this->Component<T>::t;
    }

     template <class T>
     T &AddComponent(){
         this->Component<T>::adNewComponent();
         return this->Component<T>::t;
     }
};


int _tmain(int argc, _TCHAR* argv[])
{

    GameObject gameObject;
    gameObject.AddComponent<Rigidbody>();
    Rigidbody rigidBody = gameObject.GetComponent<Rigidbody>();
    rigidBody.AddForce(9,0,0);

    std::cin.get();
    return 0;
}

这篇关于如何在实体的基本组件列表中索引所有派生组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:09