c++ - 游戏引擎设计问题

标签 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);
}

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

最佳答案

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

struct BaseScriptable {
    virtual ~BaseScriptable() = default;
    virtual BaseScriptable * clone() = 0;
    virtual void update() = 0;
};

template<typename D>
struct Scriptable: BaseScriptable {
    BaseScriptable * clone() override final {
        return new D{*static_cast<D *>(this)};
    }
};

struct PlayerScript: Scriptable<PlayerScript> {
    void update() override {}
};

int main() {
    BaseScriptable *script = new  PlayerScript;
    BaseScriptable *clone = script->clone();
}

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

关于c++ - 游戏引擎设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332006/

相关文章:

java - 从数据库为 JPA 实体生成自定义代码

ios - SpriteKit : Should I pan the camera or move the background?

java - 线程间共享数据和强制锁

C++ 将日期时间字符串干净地转换为纪元

c++ - 在 C 和 C++ 中实现字符串文字连接

java - JPA实体没有主键?

unity3d - 无法与 Unity 包管理器本地服务器进程建立连接 - Unity?

c++ - std::cout 是否保证被初始化?

c++ - 在 C++ 中分配内存然后在 Cython 中释放它?

java - 如何从单个数据定义生成 Java 和 PHP 中的实体?