【问题标题】:Entity System in C++C++中的实体系统
【发布时间】:2012-09-01 09:08:12
【问题描述】:

我最近发现了实体系统架构,但我在 C++ 中遇到了一些困难/理解实现。

我如何看待实体系统:

组件:具有属性、设置和获取的类。

  1. 雪碧
  2. 实体
  3. 宇宙飞船
  4. ...

System:具有组件列表的类。

  1. 列表项
  2. 实体管理器
  3. 渲染器
  4. 输入
  5. 相机
  6. ...

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&lt;System&gt; 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 &lt;System.h&gt; 是否包含在您的SystemManager 标头中?如果是,则不应在 SystemManager 标头中使用 class System; 重新定义 System,因为该符号已定义。
  • 我已经删除了“class System”,包括“System.h”,并且我已经将方法更改为:template&lt;typename T&gt; bool addSystem( System&lt;T&gt;* system); 但编译器不想要std::vector&lt;System*&gt; systemList;,我已经尝试过std::vector&lt;System&lt;T&gt;*&gt; systemList; 但这是不可能的。

标签: c++ architecture entity-system component-based


【解决方案1】:

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

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

【讨论】:

  • 确实,这是架构的问题,我会在gamedev频道问:)。谢谢!
猜你喜欢
  • 2018-01-05
  • 2012-11-23
  • 2016-09-01
  • 2013-02-14
  • 2014-04-03
  • 2016-11-05
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多