ECS框架

简介

ECS 全称EntityComponentSystem。在GDC2017[OverWatch GanePlay Architecture and Netcode]的分享会上,暴雪的Tim Ford介绍了守望先锋游戏架构与网络同步的设计

  • Entity
    • Entity是有一个数据的容器,代表游戏中某个对象,它应该只有数据,没有方法,你可以以组件的形式(IComponent)添加、替代、删除这些数据。
  • Context
    • Context是一个工厂,用来创建和销毁Entity,可以使用它来过滤出所感兴趣的Entity
  • Group
    • 在Context中可以对Entity进行快速过滤,它能不断的更新以保持当前的组中的Entity是最新的。假设Context有上千个Entities,但只有两个Entities拥有PositionComponent,那只要向Context询问特定的组就能立刻获取到所有符合的entity
    • Group和Group所过滤到的entities会呗缓存下来,所以即使多次调用GetGroup方法,也是非常高效的
    • 内部也是通过Group的方式实现。Groups拥有以下的事件,onEntityAdded,onEntityRemoved和onEntityUpdated来直接响应Entity变化
  • Matcher
    • 通过代码生成器产生的
    • 通常用于从Context中获取感兴趣的Groups
  • Collector
    • 提供了一种简单的方法来处理Group中entity变化的反应
  • Systems
    • 四种不同类型的Systems
      • IInitializeSystem 执行一次
      • IExecuteSystem 每帧执行
      • ICleanup在别的systems完成后,每帧执行
      • ReactiveSystem 当Group有变化时执行

Entitas

Entitas是一个超快的实体组件系统框架(ECS),专门为c#和Unity。内部缓存和惊人的快速组件访问使它首屈一指。为了在垃圾收集环境中实现最佳工作,并减轻垃圾收集器的负担,已经做出了几个设计决策。Entitas附带了一个可选的代码生成器,它从根本上减少了您必须编写的代码量,并使您的代码读起来像写得很好的散文。

Entitas-python

Entitas的python实现

Snipaste_2021-07-07_09-40-42.png
Entity

实体,组件的集合

  • _components组件

  • on_component_added Event

    • Context._comp_added_or_removed
  • on_component_removed Event

    • Context._comp_added_or_removed
  • on_component_replaced Event

    • _comp_replaced
  • add

    • 构建组件,并回调on_component_added
  • replace

    • _replace
      • 替换组件,回调on_component_replaced
      • args is None 删除组件,回调on_component_removed
    • add
  • remove

    • _replace(None)
Context

管理entities的数据结构。

  • _entities 缓存所有的entity
  • _reusable_entities 可重用的entity
  • _entity_index
  • _groups
  • _entity_indices
Matcher

查看entity的组件是否匹配

  • _all 必须有的组件
  • _any 有其中的任意一个都可以
  • _none不能出现的组件
Group

跟matcher匹配的所有的entity的集合

  • handle_entity 根据entity和component从entities添加或者删除entity
  • update_entity 更新entity的组件
    • 删除旧组件对应和的entity
    • 增加新组建
    • 更新entity组件
  • on_entity_added Event
  • on_entity_removed Event
  • on_entity_updated Event
Collector
  • _collected_entities 当entity增加component的时候,会更新到group,然后从group在更新到Collector,前提是Collector先activate
  • _groups group的集合
  • activate
    • 遍历group
    • 更新group对应的事件
      • group.on_entity_added
        • Collector._add_entity
      • group.on_entity_removed
        • Collector._add_entity
  • deactivate
    • 遍历group
    • 取消group对Collector中的listener依赖
      • group.on_entity_added
      • group.on_entity_removed
ExecuteProcessor 对应system
  • 创建的从自身的trigger中读取matcher,和group_event 然后从Context中找到对应的Group,然后把group和group_event添加进colletor
  • activate
    • 调用Collector的activate
  • execute
    • 遍历Collector的collected_entities
  • deactivate
    • 调用Collector的deactivate

用法

context = Context()

processors = Processors()
processors.add(StartGame(context))
processors.add(InputProcessors(context))
processors.add(RenderDisplay())
processors.add(DestroyEntity(context))

processors.initialize()
processors.activate_reactive_processors()

# main loop
running = True
while running:
  processors.execute()
  processors.cleanup()

  if EmitInput.quit:
      break

processors.clear_reactive_processors()
processors.tear_down()

quit()

自定义一些Processor,然后添加到processors中

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 框架介绍 entitas是一个超快、超轻量的c# Entity-Component-System (ECS)框架,...
    猿学阅读 2,477评论 0 3
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,605评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,227评论 4 8
  • 步骤:发微博01-导航栏内容 -> 发微博02-自定义TextView -> 发微博03-完善TextView和...
    dibadalu阅读 3,182评论 1 3
  • 回这一趟老家,心里多了两个疙瘩。第一是堂姐现在谈了一个有妇之夫,在她的语言中感觉,她不打算跟他有太长远的计划,这让...
    安九阅读 3,533评论 2 4