最近,我发现了实体系统架构,并且在C++ /理解实现中遇到了一些困难。
我如何看待实体系统:
Components:一个具有属性、set和get的类。
System:一个包含组件列表的类。
Entity:只是一个包含组件列表的空类。
我所做的:
目前,我有一个允许我这样做的程序:
// Create a new entity/
Entity* entity = game.createEntity();
// Add some components.
entity->addComponent( new TransformableComponent() )
->setPosition( 15, 50 )
->setRotation( 90 )
->addComponent( new PhysicComponent() )
->setMass( 70 )
->addComponent( new SpriteComponent() )
->setTexture( "name.png" )
->addToSystem( new RendererSystem() );如果我正确地理解了EntitySystem,那么每个系统都有自己的组件列表。(组件列表或实体列表,即问题)
class Component;
/////////////////////////////////////////////////
/// \brief An abstract system. (Interface)
///
/////////////////////////////////////////////////
class System
{
public:
/////////////////////////////////////////////////
/// \brief Call when process is created.
///
/////////////////////////////////////////////////
virtual bool start() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is updated.
///
/////////////////////////////////////////////////
virtual void update() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is removed.
///
/////////////////////////////////////////////////
virtual void end() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is removed.
///
/////////////////////////////////////////////////
virtual void addComponent( Component* component )
{
elements.push_back( component );
}
protected:
std::vector<Component*> elements;
};(我把代码放在.h中只是为了快速调试^^)
问题所在
我希望在一个系统中添加一个"T“组件,其中包含X组件列表
我试过的是:
std::vector<Component*> elements;但我想要这样的东西:
std::vector<T*> elements;我的系统类是抽象的。我的系统子类需要有这个列表和自己的类型。
解决办法:
我尝试将我的系统类放入一个模板类,所以我只需要做:类Renderer : System
但是我的SystemManager不喜欢这个代码:std::vector<System> systems。
具有T类型的系统类:
template<class T>
class System
{
public:
/////////////////////////////////////////////////
/// \brief Call when process is created.
///
/////////////////////////////////////////////////
virtual bool start() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is updated.
///
/////////////////////////////////////////////////
virtual void update() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is removed.
///
/////////////////////////////////////////////////
virtual void end() = 0;
/////////////////////////////////////////////////
/// \brief Call when process is removed.
///
/////////////////////////////////////////////////
virtual void addComponent( T* component )
{
elements.push_back( component );
}
protected:
std::vector<T*> elements;
};SystemManager代码:
class System;
class SystemManager
{
public:
/////////////////////////////////////////////////
/// \brief Default constructor.
///
/////////////////////////////////////////////////
SystemManager();
/////////////////////////////////////////////////
/// \brief Call when system is created.
/// \param system A system to add.
///
/////////////////////////////////////////////////
bool addSystem( System* system);
/////////////////////////////////////////////////
/// \brief Call when system is updated.
///
/////////////////////////////////////////////////
void update();
/////////////////////////////////////////////////
/// \brief Call when system is removed.
/// \param system A system to remove.
///
/////////////////////////////////////////////////
void removeSystem( System* system );
private:
std::vector<System*> systemList;
};这样,我的SystemManager中就出现了一个错误:“将'System‘重新定义为不同类型的符号”(指向SystemManager中的“类系统”行)。
你有解决这个问题的办法吗?我的EntitySystem方法好吗?
谢谢!
发布于 2012-09-07 15:07:26
在代码系统中是一个模板,在后面的系统管理器类中,您尝试使用系统,就像它不是模板一样。这不管用。您需要指定类型、使系统管理器成为模板并将类型参数传递给system,或者不让系统成为模板。另外,作为一个非模板,正向声明模板也是不起作用的。后退一步,找出模板在C++中的工作方式。
此外,http://gamedev.stackexchange.com还有许多关于实体组件设计的问题和答案,您可能会发现它很有用。
https://stackoverflow.com/questions/12318840
复制相似问题