【发布时间】:2012-09-01 09:08:12
【问题描述】:
我最近发现了实体系统架构,但我在 C++ 中遇到了一些困难/理解实现。
我如何看待实体系统:
组件:具有属性、设置和获取的类。
- 雪碧
- 实体
- 宇宙飞船
- ...
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,那么每个 System 都有自己的组件列表,可以在其中工作。 (组件列表或实体列表,这就是问题)
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 中只是为了快速调试 ^^)
问题
我想在系统中添加一个带有 X 组件列表的“T”组件
我尝试过的:
std::vector<Component*> elements;
但我想要这样的东西:
std::vector<T*> elements;
我的 System 类是抽象的。我的 System 子类需要有这个列表,并且是自己的类型。
解决办法:
我试图让我的 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;
};
系统管理器代码:
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 中出现了这个错误: “将‘系统’重新定义为不同类型的符号” (指向 SystemManager 中的“class System”行)
你有解决这个问题的办法吗?我的 EntitySystem 方法好吗?
谢谢!
【问题讨论】:
-
请提供问题的确切信息(编译器错误等)“但我的 SystemManager 不喜欢此代码”是不够的。此外,“我的方法好吗”这个问题不适合像 stackoverflow 这样的常见问题解答服务。
-
感谢您的回复,我已经编辑了我的帖子以提供更多信息。
-
您的
#include <System.h>是否包含在您的SystemManager标头中?如果是,则不应在SystemManager标头中使用class System;重新定义System,因为该符号已定义。 -
我已经删除了“class System”,包括“System.h”,并且我已经将方法更改为:
template<typename T> bool addSystem( System<T>* system);但编译器不想要std::vector<System*> systemList;,我已经尝试过std::vector<System<T>*> systemList;但这是不可能的。
标签: c++ architecture entity-system component-based