C++中的实体系统

Entity System in C++

本文关键字:系统 实体 C++      更新时间:2023-10-16

我最近发现了实体系统体系结构,在C++实现中遇到了一些困难。

我如何看待实体系统:

组件:一个具有attributs、set和get的类。

  1. 精灵
  2. Physicbody
  3. 太空船

系统:一个包含组件列表的类。

  1. 列出项目
  2. EntityManager
  3. 渲染器
  4. 输入
  5. 摄像头

实体:只是一个包含组件列表的空类。

我所做的:

目前,我有一个程序允许我这样做:

// 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中只是为了快速调试^^)

问题

我想在一个带有X组件列表的系统中添加一个"T"组件

我尝试过的:

std::vector<Component*> elements;

但我想要这样的东西:

std::vector<T*> elements;

我的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中出现了以下错误:"将‘系统’重新定义为不同类型的符号"(指向SystemManager中的"class System"行)

你有解决这个问题的办法吗?我的EntitySystem方法好吗?

谢谢!

在您的代码中,System是一个模板,稍后在System manager类中,您将尝试使用System,就好像它不是一个模板一样。这是行不通的。您需要指定类型,使系统管理器成为模板并将类型参数传递给系统,或者不使系统成为模板。同样,作为一个非模板,前向声明模板也不会起作用。退一步,弄清楚模板在C++中是如何工作的。

更多http://gamedev.stackexchange.com有许多关于实体组件设计的问题和答案,您可能会发现它很有用。