多態性是你想要的。 簡單地使所有組件這樣的:
public Object
{
enum TYPE{
COMPONENT,,
GAMEOBJECT,
ETC,,,
};
Object(TYPE id){m_id = id;}
protected:
TYPE m_id;
virtual void Update(void) = 0;
virtual void Render(void) = 0;
public:
void GetTypeOf(void)const{return m_id;}
};
class Component : Object
{
enum COMPONENT_TYPE
{
COLLIDER,
RENDERER,
ETC,,,,
};
Component() : Object (COMPONENT){/**/}
virtual void Update(void){return;}
virtual void Render(void){return;}
};
class BoxCollider : Component
{
BoxCollider(void) : Component(BOXCOLLIDER){/**/}
void Update(void)
{
//to do
}
void Render(void)
{
//to do
}
};
,那麼你可以簡單地擁有對象*或組件* 的數據結構,你可以通過這種方式遍歷:
std::vector<Component*>::iterator components = ComponentVector.begin();
for(;components != ComponentVector.end() ; ++components)
{
*(component)->Update();
*(component)->Render();
std::cout << "id" << *component->getTypeOf() << std::endl;
}
你能有更多的代碼說明你的要求,比如?我可能失去了一些東西,因爲如果你只需要簡單的繪製對象組件使用從物理學的協調,只是喜歡寫東西'無效對象::更新(){這個 - >畫(*此);}',假設你有' void Drawable :: draw(const Physics&)'。只要右邊的'Drawable'和'Physics'處於可變元件'Components ...'中,就會工作 – 2015-03-23 20:51:10