【问题标题】:Composite, component and entities in C++ game engineC++ 游戏引擎中的复合、组件和实体
【发布时间】:2013-12-09 14:14:49
【问题描述】:

我正在开发一个游戏的学校项目。我们使用由我们拥有的一个团队制造的引擎。引擎的构建对我来说不清楚,似乎是一种反模式。然而,似乎没有人能让我清楚地选择设计。该引擎应该使用“基于组件”的设计,但我没有看到它。下面是 Component、Composite 和 Entity 类的代码。简而言之,我的问题是:这段代码是否使用了有效的设计模式,还是只是为了“实现设计模式”而过于复杂,从而导致了反模式

组件.cpp:

#include "Engine\Component.h"
#include "Engine\Composite.h"

Component::Component(Composite* parent)

{
    this->parent = parent;
}

Component::~Component()
{
}

Entity.cpp

#include "Engine\Entity.h"
#include "Engine\Game.h"


Entity::Entity(Composite* parent):Composite(parent)
{
    this->mass = 1;
    node = NULL;
}

void Entity::update()
{
    Composite::update();

    this->angularVelocity += this->angularAccelaration;
    this->orientation += this->angularVelocity;

    this->accelaration = (1 / this->mass) * this->force;
    this->velocity += this->accelaration;
    this->position += this->velocity;
    if (node != NULL)
    {
        this->node->setPosition(this->position);
        this->node->setRotation(this->orientation);
    }
}

void Entity::draw()
{
    Composite::draw();

    if (node == NULL) return;
    if (!this->visible)
    {
        this->node->setVisible(false);
        return;
    }
    this->node->setVisible(true);

    this->node->render();
}

void Entity::createNode(std::string modelPath)
{
    // Get the mesh
    irr::scene::IAnimatedMesh* mesh = Game::getSceneManager()->getMesh(modelPath.c_str());

    // Create model entity
    this->node =  Game::getSceneManager()->addMeshSceneNode( mesh );
    this->node->setMaterialFlag(EMF_FOG_ENABLE, true);
}

Entity::~Entity()
{
    Composite::~Composite();
    if (node != NULL)
    {
        node->drop();
    }
}

Composite.cpp

#include "Engine\Composite.h"

Composite::Composite(Composite* parent):Component(parent)
{
}


Composite::~Composite()
{
    for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
    {
        delete (*i);
    }
    components.clear();
}

void Composite::handleMessage(unsigned int message, void* data)
{
    for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
    {
        (*i)->handleMessage(message, data);
    }
}

void Composite::update()
{
    for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
    {
        (*i)->update();
    }
}

void Composite::draw()
{
    for (std::list<Component*>::iterator i = components.begin(); i != components.end(); ++i)
    {
        (*i)->draw();
    }
}

void Composite::addComponent(Component* component)
{
    components.push_back(component);
}

void Composite::removeComponent(Component* component)
{
    components.remove(component);
    delete component;
}

下一段代码是 Player.cpp,它使用复合和实体作为混合类型的对象(我真的不明白逻辑)。

播放器.cpp

#include "Player.h"
#include "Messages.h"
#include <iostream>

Player::Player(Composite* parent) : Entity(parent)
{
    createNode("../assets/sydney.md2");
    //i = 0;
    //v3f = vector3df(0,0,0);
    /*networker = new NetworkComponent();
    addComponent(networker);
    networker->registerVar(&i);
    networker->registerVar(&v3f);*/
}

void Player::update() {
    Composite::update();
    //std::cout<<i<<std::endl;
    //std::cout<<"vectorx="<<v3f.X<<"\n";
}

void Player::handleMessage(unsigned int message, void* data) {
    switch(message) {
        case DAMAGE: /* Do something */;
    }
    delete data;
}

Player::~Player()
{
    Entity::~Entity();
}

我根本不相信这是基于组件的设计。不应该删除实体,只使用复合和组件。组件基类不应该是空的,从不直接使用吗?像一个名为“Rigidbody”的组件,它拥有一个用于刚体数据的数据结构和一些覆盖完全虚拟组件基类的函数?

【问题讨论】:

  • 感谢 Nathaniel Ford 的编辑,它应该让它更直接:)
  • 我看不出游戏组件设计和复合模式是一回事。 Player 也应该是一个组件,因为在复合模式中,它应该是。
  • @yngum 我认为两者确实完全不同。代码是否混合了两种不同的设计模式?如果“播放器”是一个组件,那么将从它继承什么?组件不应该是“玩家”将继承的“可控”之类的东西吗?至少我现在是这样理解复合的。

标签: c++ game-engine composite entity-system component-based


【解决方案1】:

发布的代码是composite pattern 的变体。这种设计模式是结构模式,用于允许客户以统一的方式处理单个对象和复杂对象,例如由多个对象组成的对象。例如,渲染循环可以遍历对象集合,在每个对象上调用draw()。由于这是一种结构模式,因此很难主观地回答这是否是 overengineering 的实例,因为这需要检查更多的类层次结构和架构。

然而,ComponentComposite 的类命名约定和复合设计模式的使用都没有暗示这是一个“基于组件”的设计。我不熟悉game programming component pattern,但它本质上似乎是在算法类中耦合状态的strategy pattern,从而导致strategycontext 之间的简化接口。在任何一种情况下,这两种模式都是行为模式,它们实现了可互换和封装的算法。因此,发布的代码没有实现“基于组件”的设计,因为ComponentCompositeEntityPlayer 类都没有提供以可互换方式封装算法的方法。例如,Entity::update() 将始终以相同的方式计算位置。如果Entity 需要使用不同的物理模型(考虑Entity 扭曲到具有不同物理集的行星的情况),这种耦合需要扩展Entity 类层次结构,而不是委托给@ 987654338@ 封装算法的类层次结构。

【讨论】:

  • 我明白你的意思,感谢您的洞察力。在那种情况下,我会问设计是否有效。我认为 Composite 和 Entity 是同一件事:组件的集合,它们以抽象的方式将功能添加到对象。
  • @RB-Develop:我已经根据对原始问题的更改更新了我的答案。实际问题是相当主观的,但希望答案能提供一些说明,让您自己做出决定。
  • 感谢您的更新,它确实让我更清楚了。事实证明,他们只使用 Component 和 Entity 来实现“基于组件的实体系统”(看起来像是名称奇特的策略模式)。后来一位老师提到复合是一个好主意,所以他们只是添加了它。我不认为它是过度设计的,我认为它只是一个损坏的设计。
猜你喜欢
  • 2017-07-27
  • 2017-08-01
  • 2018-01-07
  • 1970-01-01
  • 2010-12-26
  • 2012-01-14
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多