修改场景实体查询为unordered_set存储,提升查询效率, 修改Physics的SyncScene也为unordered_set

This commit is contained in:
2026-10-04 19:12:53 +08:00
parent b78d9fb4c9
commit c1854453e5
5 changed files with 47 additions and 16 deletions
+10
View File
@@ -97,6 +97,10 @@ namespace Engine::ECS {
// 按稳定 UUID 查实体(序列化/跨引用场景);找不到返回 entt::null
Entity FindEntityByUUID(UUID uuid);
// 全量重建 UUID → Entity 索引(序列化加载完调一次;运行期勿调,
// CreateEntity / FlushDestroyQueue 已增量维护)
void RebuildUuidIndex();
Registry& GetRegistry() { return m_Registry; }
const Registry& GetRegistry() const { return m_Registry; } // 序列化等只读遍历
@@ -137,6 +141,12 @@ namespace Engine::ECS {
Registry m_Registry;
std::vector<Entity> m_PendingDestroy;
Settings m_Settings;
// UUID → Entity 索引(O(1) FindEntityByUUID;SyncConstraints 每帧调用)
// CreateEntity / FlushDestroyQueue 增量维护;序列化加载后由
// RebuildUuidIndex() 全量重建一次(SceneSerializer 直接改 IdComponent.uuid
// 绕过 CreateEntity,仅靠增量会不同步)
std::unordered_map<UUID, Entity> m_UUIDToEntity;
};
} // namespace Engine::ECS
+2 -1
View File
@@ -39,6 +39,7 @@
#include <glm/gtc/quaternion.hpp>
#include <mutex>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace Engine {
@@ -126,7 +127,7 @@ namespace Engine {
ECS::TransformComponent& tr);
// 约束对账:owner 孤儿清理 / body 重建传染失效 / 建缺失与变更重建
// invalidated = 本帧 SyncScene 中被销毁或重建 body 的实体位模式
void SyncConstraints(const std::vector<u32>& invalidated);
void SyncConstraints(const std::unordered_set<u32>& invalidated);
void CreateConstraint(u32 ownerBits, u32 targetBits,
const ECS::JointComponent& jc);
void RemoveConstraint(u32 ownerBits);