How to index all the derived components in a base component list in Entity

99封情书 提交于 2019-12-06 11:43:16

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!