首页
/ EnTT项目中的组(Group)和视图(View)封装实践

EnTT项目中的组(Group)和视图(View)封装实践

2025-05-21 15:02:52作者:蔡丛锟

引言

在基于EnTT的实体组件系统(ECS)开发中,组(Group)和视图(View)是两种核心的数据访问模式。它们提供了高效的方式来遍历和操作具有特定组件组合的实体。然而,在实际项目开发中,我们经常需要对这些基础结构进行封装,以实现更高级的功能或满足特定架构需求。

为什么需要封装

在游戏引擎开发中,直接使用EnTT的原生组和视图可能会遇到以下挑战:

  1. 延迟初始化需求:某些情况下,我们需要先声明组对象,稍后才能初始化它
  2. 类型安全:希望提供更友好的接口来创建和使用组
  3. 系统集成:需要将组与自定义系统架构集成
  4. 生命周期管理:确保组的创建和销毁与系统生命周期一致

封装方案设计

基础接口设计

首先,我们定义一个基础查询接口类,作为所有具体查询类型的基类:

class VQueryBase {
public:
    // 禁止堆分配
    void* operator new(std::size_t) = delete;
    void operator delete(void*) = delete;

    VQueryBase(VSystem* owningSys) {
        owningSys->RegisterQueryToSystem(this);
    }
    
    virtual ~VQueryBase() {
        m_OwningSys->UnRegisterQueryFromSystem(this);
    }

protected:
    VSystem* m_OwningSys;
    
private:
    virtual void InitializeQuery(VEntityManager& entity_manager) = 0;
    friend VSystem;
};

这个基类主要处理与系统的注册关系,并确保查询对象不会被错误地分配在堆上。

类型别名定义

为了简化模板参数的使用,我们定义一些类型别名:

template<typename... Type>
using Exclude_T = entt::exclude_t<Type...>;

template<typename... Type>
using Get_T = entt::get_t<Type...>;

template<typename... Type>
using Owned_T = entt::owned_t<Type...>;

核心封装实现

核心的组封装实现涉及一些高级模板技巧:

// 辅助函数推导组类型
template<class... TOwneds, class... TGets, class... TExcludes>
constexpr auto get_group_type(
    entt::owned_t<TOwneds...>, entt::get_t<TGets...>, entt::exclude_t<TExcludes...>) ->
        entt::basic_group< 
            entt::owned_t<entt::registry::storage_for_type<TOwneds>...>,
            entt::get_t<entt::registry::storage_for_type<TGets>...>,
            entt::exclude_t<entt::registry::storage_for_type<TExcludes>...>>;

// 类型别名简化
template<class TOwned, class TGet, class TExclude>
using group_type_t = decltype(get_group_type(
    std::declval<TOwned>(), std::declval<TGet>(), std::declval<TExclude>()));

// 主模板声明
template<class, class, class>
class VQuery;

// 具体实现
template<class TOwned, class TGet = Get_T<>, class TExclude = Exclude_T<>>
class VQuery : public VQueryBase {
public:
    VQuery(VSystem* owningSys): VQueryBase(owningSys) {}
    
    void InitializeQuery(VEntityManager& entity_manager) override {    
        m_Group = MakeGroup(entity_manager.GetNativeRegistry(), TOwned{});
    }

    auto& Group() {
        return m_Group;
    }

private:
    group_type_t<TOwned, TGet, TExclude> m_Group;
    
    template<class... TOwneds>
    static auto MakeGroup(
        entt::registry& entity_manager,
        Owned_T<TOwneds...> i_owned) {
        return entity_manager.group<TOwneds...>(TGet{}, TExclude{});
    }
};

使用示例

在实际系统中使用封装后的组:

class VTransformSystem : public VSystem {
public:
    void OnStartup(VEntityManager& manager) override;
    void OnExecute(VEntityManager& manager, const VSystemExecutionContext& context) override;
    
    // 其他成员函数...
    
    VQuery<Owned_T<VTransformComponent>> m_TransformQuery = VQuery<Owned_T<VTransformComponent>>(this);
};

关键点解析

  1. 类型推导:通过get_group_type辅助函数和group_type_t类型别名,我们能够正确推导出底层EnTT组的具体类型

  2. 延迟初始化:封装允许我们先创建查询对象,稍后在系统初始化时再实际创建EnTT组

  3. 类型安全:模板参数强制使用Owned_TGet_TExclude_T包装,确保类型正确性

  4. 生命周期管理:通过基类机制,确保查询对象与其所属系统生命周期一致

扩展思考

这种封装模式不仅适用于组,也可以类似地应用于视图(View)。在实际项目中,还可以进一步扩展:

  1. 添加缓存机制,避免频繁重建组
  2. 实现更复杂的查询组合逻辑
  3. 添加调试和性能分析功能
  4. 支持热重载时的查询更新

总结

通过合理的封装设计,我们可以在保持EnTT高性能特性的同时,获得更好的工程实践体验。这种封装既解决了延迟初始化的问题,又提供了更清晰的接口,同时与自定义引擎架构无缝集成。这种模式在复杂游戏引擎开发中特别有价值,能够显著提高代码的可维护性和可扩展性。

登录后查看全文
热门项目推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
863
511
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
259
300
kernelkernel
deepin linux kernel
C
22
5
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
596
57
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
332
1.08 K