【发布时间】:2018-01-02 01:47:18
【问题描述】:
我正在使用 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);
}
我不希望用户必须为他或她创建的每个脚本创建一个克隆方法,我认为它应该由引擎自动处理。但我无法弄清楚如何以不同的方式做到这一点。
【问题讨论】:
标签: c++ entity game-engine