cocos实现unity组件式

用法灵感来源于unity,打算改造cocos

class SGComponent
{
public:
	virtual void update(float delta){};
	virtual void onStart(){}
	virtual void onDestroy(){};
	virtual void onCreate(){};
};

class AudioComponent :public SGComponent
{
public:
};
class RenderComponent :public SGComponent
{
public:
};


class GameObject
{
public:
	template<class T>
	SGComponent* GetComponent()
	{
	return nullptr;
	};

	template<>
	SGComponent* GetComponent<AudioComponent>()
	{
	return this->_audio;
	};

    template<class T>void AddComponent(T * t){};

	template<>void AddComponent(RenderComponent *t)
	{
		if (_render){}
		this->_render = t;
	};

	template<>void  AddComponent(AudioComponent *t)
	{
		if (_audio){}
		this->_audio = t;
	};
private:
	AudioComponent*_audio = nullptr;
	RenderComponent *_render = nullptr;
};




int main(int argc,char *argv[])
{
	_CrtDumpMemoryLeaks();

	GameObject obj;
	cout << obj.GetComponent<RenderComponent>();

	system("pause");
	return 0;
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...