实施实体组件系统的最佳方式是什么

What Is The Best Way to Implement Entity-Component System

最近我打算实现一个 Entity-Component-System 类似守望先锋。

我项目中的主要挑战(和困难)是,我的引擎允许用户定义自定义地图,其中允许用户定义自定义单位。换句话说,用户可以select实体类型

需要哪个组件

例如

type Component interface {
    ComponentName() string
}

type HealthComponent struct {
    HP uint
}

type ManaComponent struct {
    MP uint
}

type ChiComponent struct{
        Chi uint
}
// assuming each component has implemented Component interface

对应的Entity定义为:

type Entity struct {
    ID         EID
    EntityName string
    Components map[string]Component
}

用户将有一些 JSON 格式的实体定义:

{
 "EntityName": "FootMan",
 "Components": {
  "HealthComponent": {
   "HP": 500
  }
 }
}
---------------------------------------------
{
 "EntityName": "Warlock",
 "Components": {
  "HealthComponent": {
   "HP": 250
  },
  "ManaComponent": {
   "MP": 100
  }
 }
}
---------------------------------------------
{
 "EntityName": "Monk",
 "Components": {
  "HealthComponent": {
   "HP": 250
  },
  "ChiComponent": {
   "Chi": 100
  }
 }
}

请注意ID不包含在JSON中,因为我们需要在运行-time

初始化它

那么问题来了:
构建具有给定 JSON 定义的此类实体的最有效方法是什么? 目前我的解决方案是使用注册表来维护结构类型和组件名称之间的映射

var componentRegistry = make(map[string]reflect.Type)
func init() {

    componentRegistry["ChiComponent"] = reflect.TypeOf(ChiComponent{})
    componentRegistry["HealthComponent"] = reflect.TypeOf(HealthComponent{})
    componentRegistry["ManaComponent"] = reflect.TypeOf(ManaComponent{})

}

生成器代码是

func ComponentBuilder(name string) Component {

    v := reflect.New(componentRegistry[name])
    fmt.Println(v)
    return v.Interface().(Component)
}

func EntityBuilder(EntityName string, RequireComponent []string) *Entity {

    var entity = new(Entity)
    entity.ID = getNextAvailableID()
    entity.EntityName = EntityName
    entity.Components = make(map[string]Component)
    for _, v := range RequireComponent {
        entity.Components[v] = ComponentBuilder(v)
    }

    return entity
}

对于每个想要访问此实体中组件的系统,需要执行以下操作:

var d = monk_entity.Components["ChiComponent"].(*ChiComponent)
d.Chi = 13

var h = monk_entity.Components["HealthComponent"].(*HealthComponent)
h.HP = 313

它有效,但我在这种方法中使用了过多的反射,我无法为存储在用户定义的 JSON 文件中的实体分配初始值。有没有更好的方法呢?

有一件事,你可以只使用函数而不是反射:

type componentMaker func() Component  // Define generator signature

var componentRegistry = make(map[string]componentMaker) // Update map type

componentRegistry["ChiComponent"] = func() Component { return new(ChiComponent) } // Define generators

entity.Components[v] = componentRegistry[v]() // Call generator

等等。无需反思。