cocos实现unity组件式

前端之家收集整理的这篇文章主要介绍了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;
}
原文链接:https://www.f2er.com/cocos2dx/338964.html

猜你在找的Cocos2d-x相关文章