游戏引擎设计问题

时间:2017-07-26 15:58:12

标签: c++ entity game-engine

我正在使用ECS开发游戏引擎。我的问题是我如何产生一个实体。我的想法是我有一个方法,它接受一个实体作为参数,创建一个这个实体的克隆,从克隆中检索所有指针组件,并将它们放在各自的系统中进行更新:

Entity & Scene::spawnEntity(Entity entity) {

    Entity clone = Entity(entity);

    Transform* transform = clone.getComponent<Transform>();
    Drawable* drawable = clone.getComponent<Drawable>();
    Collidable* collidable = clone.getComponent<Collidable>();
    Scriptable* scriptable = clone.getComponent<Scriptable>();

    if (transform != nullptr) {
        _transformSystem.add(*transform, _currentId);
    }
    if (drawable != nullptr) {
        _drawableSystem.add(*drawable, _currentId);
    }
    if (collidable != nullptr) {
        _collidableSystem.add(*collidable, _currentId);
    }
    if (scriptable != nullptr) {
        scriptable->assignCallbacks([&](Entity entity)                              -> Entity&              { spawnEntity(entity); },
                                    [&](Entity entity)                                                      { destroyEntity(entity); },
                                    [&](std::vector<std::string> tags)              -> Entity&              { findEntity(tags); },
                                    [&](std::vector<std::vector<std::string>> tags) -> std::vector<Entity>& { findEntities(tags); });
        _scriptableSystem.add(scriptable, _currentId);
    }

    _entities.push_back(clone);

    _currentId++;
}

这里的问题是其中一个组件,即脚本表是一个纯抽象类(它有一个启动方法和更新方法,开发人员用它来创建派生类中的行为)。这使得引擎无法自动克隆脚本化类,克隆必须在派生类中完成,例如:

class PlayerScript : public Scriptable
{
public:
    void init() override;
    void update() override;
    PlayerScript* clone() override;
};


PlayerScript * PlayerScript::clone()
{
    return new PlayerScript(*this);
}

我不希望用户必须为他或她创建的每个脚本创建克隆方法,我认为它应该由引擎自动处理。但我无法弄清楚如何以不同的方式做到这一点。

1 个答案:

答案 0 :(得分:2)

只要玩家&#39;脚本是可复制构造的,您可以通过复制构造函数克隆它们,您可以使用CRTP和这样的中间类(最小的工作示例):

{
    "token_type": "Bearer", // or other method
    "expires_in": 31536000,
    "access_token": "eyJ0eXAiOiJKV....WaYC14FGOPuhViVIE",
    "refresh_token": "XhZKHr2S2Ikp...wMKWfmksñT/MxVQvg="
}

如果复制构造函数不足以克隆播放器脚本,那么这意味着您没有足够的信息来实际克隆它。因此,定义脚本的开发人员还必须定义一个克隆例程来为您提供正确的副本。