// // Created by atdunbg on 2026/9/22. // #include #include #include namespace Engine::ECS { EntityHandle Scene::CreateEntity(std::string name) { Entity e = m_Registry.create(); // IdComponent 自动挂载:每个实体出生即有稳定 UUID m_Registry.emplace(e, IdComponent{UUID::New()}); if (!name.empty()) { m_Registry.emplace(e, NameComponent{std::move(name)}); } return EntityHandle{this, e}; } Entity Scene::FindEntityByUUID(UUID uuid) { if (!uuid.Valid()) return entt::null; Entity found = entt::null; m_Registry.view().each([&](Entity e, IdComponent& id) { if (found == entt::null && id.uuid == uuid) found = e; }); return found; } void Scene::DestroyEntity(Entity e) { if (!m_Registry.valid(e)) { ENGINE_FATAL("Scene::DestroyEntity: stale entity ({})", static_cast(e)); } // 延迟到帧尾,保证遍历期间池结构不变 m_PendingDestroy.push_back(e); } void Scene::FlushDestroyQueue() { if (m_PendingDestroy.empty()) return; for (Entity e : m_PendingDestroy) { if (m_Registry.valid(e)) { m_Registry.destroy(e); // entt 自动回收槽位并递增版本号 } } m_PendingDestroy.clear(); } CameraComponent* Scene::GetPrimaryCamera() { CameraComponent* found = nullptr; m_Registry.view().each([&](Entity, CameraComponent& cc) { if (!found && cc.primary) found = &cc; }); return found; } } // namespace Engine::ECS