refactor: ECS 迁移到 entt,移除自研 Registry
- Entity = entt::entity(内部自带版本号),Registry = entt::registry - Scene 对外接口不变(AddComponent/GetComponent/Each/延迟销毁/UUID), Renderer 与 App 层零改动完成迁移 - Each 转发 entt view(自动最小池遍历),延迟销毁队列保留 - 删除自研 Registry.h/Entity.h
This commit is contained in:
+14
-13
@@ -9,32 +9,33 @@ namespace Engine::ECS {
|
||||
|
||||
Entity Scene::CreateEntity(std::string name)
|
||||
{
|
||||
Entity e = m_Registry.Create();
|
||||
Entity e = m_Registry.create();
|
||||
// IdComponent 自动挂载:每个实体出生即有稳定 UUID
|
||||
m_Registry.Add<IdComponent>(e, IdComponent{UUID::New()});
|
||||
m_Registry.emplace<IdComponent>(e, IdComponent{UUID::New()});
|
||||
if (!name.empty()) {
|
||||
m_Registry.Add<NameComponent>(e, NameComponent{std::move(name)});
|
||||
m_Registry.emplace<NameComponent>(e, NameComponent{std::move(name)});
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
Entity Scene::FindEntityByUUID(UUID uuid)
|
||||
{
|
||||
if (!uuid.Valid()) return {};
|
||||
if (!uuid.Valid()) return entt::null;
|
||||
|
||||
Entity found{};
|
||||
m_Registry.Each<IdComponent>([&](Entity e, IdComponent& id) {
|
||||
if (!found && id.uuid == uuid) found = e;
|
||||
Entity found = entt::null;
|
||||
m_Registry.view<IdComponent>().each([&](Entity e, IdComponent& id) {
|
||||
if (found == entt::null && id.uuid == uuid) found = e;
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
void Scene::DestroyEntity(Entity e)
|
||||
{
|
||||
if (!m_Registry.IsAlive(e)) {
|
||||
ENGINE_FATAL("Scene::DestroyEntity: stale entity ({},{})",
|
||||
e.index, e.generation);
|
||||
if (!m_Registry.valid(e)) {
|
||||
ENGINE_FATAL("Scene::DestroyEntity: stale entity ({})",
|
||||
static_cast<u32>(e));
|
||||
}
|
||||
// 延迟到帧尾,保证遍历期间池结构不变
|
||||
m_PendingDestroy.push_back(e);
|
||||
}
|
||||
|
||||
@@ -43,8 +44,8 @@ namespace Engine::ECS {
|
||||
if (m_PendingDestroy.empty()) return;
|
||||
|
||||
for (Entity e : m_PendingDestroy) {
|
||||
if (m_Registry.IsAlive(e)) {
|
||||
m_Registry.Destroy(e);
|
||||
if (m_Registry.valid(e)) {
|
||||
m_Registry.destroy(e); // entt 自动回收槽位并递增版本号
|
||||
}
|
||||
}
|
||||
m_PendingDestroy.clear();
|
||||
@@ -53,7 +54,7 @@ namespace Engine::ECS {
|
||||
CameraComponent* Scene::GetPrimaryCamera()
|
||||
{
|
||||
CameraComponent* found = nullptr;
|
||||
m_Registry.Each<CameraComponent>([&](Entity, CameraComponent& cc) {
|
||||
m_Registry.view<CameraComponent>().each([&](Entity, CameraComponent& cc) {
|
||||
if (!found && cc.primary) found = &cc;
|
||||
});
|
||||
return found;
|
||||
|
||||
Reference in New Issue
Block a user