Documentation ¶
Overview ¶
Package pt 实体与组件原型,用于创建实例。
Index ¶
- Variables
- func As[T comparable](entity ec.Entity) (T, bool)
- func Cast[T comparable](entity ec.Entity) T
- func CompAlias(comp any, alias string) _CompAlias
- func CompInterface[FACE any](comp any) _CompAlias
- type ComponentCtor
- type ComponentLib
- type ComponentPT
- type Composite
- type ConstructEntityOptions
- type EntityCtor
- type EntityLib
- type EntityPT
- type EntityPTProvider
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrPt = fmt.Errorf("%w: pt", exception.ErrCore) // 原型错误
)
View Source
var With _Option
Functions ¶
func As ¶
func As[T comparable](entity ec.Entity) (T, bool)
As 从实体提取一些需要的组件接口,复合在一起直接使用。
示例:
type A interface { MethodA() } ... type B interface { MethodB() } ... type CompositeAB struct { A B } ... v, ok := As[CompositeAB](entity) if ok { v.MethodA() v.MethodB() }
注意:
1.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。 2.实体更新组件后,需要重新提取。 3.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
func Cast ¶
func Cast[T comparable](entity ec.Entity) T
Cast 从实体提取一些需要的组件接口,复合在一起直接使用,提取失败会panic。
示例:
type A interface { MethodA() } ... type B interface { MethodB() } ... type CompositeAB struct { A B } ... Cast[CompositeAB](entity).MethodA() Cast[CompositeAB](entity).MethodB()
注意:
1.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。 2.实体更新组件后,需要重新提取。 3.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
func CompInterface ¶ added in v0.2.39
CompInterface 组件与接口,用于注册实体原型时使用接口名作为别名
Types ¶
type ComponentCtor ¶
type ComponentLib ¶
type ComponentLib interface { // Register 注册组件原型 Register(comp any, aliases ...string) ComponentPT // Deregister 取消注册组件原型 Deregister(name string) // Get 获取组件原型 Get(name string) (ComponentPT, bool) // GetAlias 使用别名获取组件原型 GetAlias(alias string) []ComponentPT // Range 遍历所有已注册的组件原型 Range(fun generic.Func1[ComponentPT, bool]) }
ComponentLib 组件原型库
type ComponentPT ¶
type ComponentPT struct { Name string // 组件名称 // contains filtered or unexported fields }
ComponentPT 组件原型
type Composite ¶
type Composite[T comparable] struct { // contains filtered or unexported fields }
Composite 组件复合器,直接使用As()或Cast()时,无法检测提取后实体是否又更新组件,使用复合器可以解决此问题。
示例:
type A interface { MethodA() } ... type B interface { MethodB() } ... type CompositeAB struct { A B } ... cx := Compose[CompositeAB](entity) ... if v, ok := cx.As(); ok { v.MethodA() v.MethodB() ... entity.AddComponent(comp) ... if v, ok := cx.As(); ok { v.MethodA() v.MethodB() } } ... cx.Cast().MethodA() cx.Cast().MethodB() ... entity.AddComponent(comp) ... cx.Cast().MethodA() cx.Cast().MethodB()
注意:
1.如果组件使用自定义别名加入实体,那么无法使用此功能提取组件接口。
func Compose ¶ added in v0.2.39
func Compose[T comparable](entity ec.Entity) *Composite[T]
Compose 创建组件复合器
type ConstructEntityOptions ¶
type ConstructEntityOptions struct { ec.EntityOptions ComponentCtor ComponentCtor // 组件构造函数 EntityCtor EntityCtor // 实体构造函数 }
ConstructEntityOptions 创建实体的所有选项
type EntityCtor ¶
type EntityCtor = generic.DelegateAction1[ec.Entity] // 实体构造函数
type EntityLib ¶
type EntityLib interface { EntityPTProvider // Register 注册实体原型 Register(prototype string, comps ...any) EntityPT // Deregister 取消注册实体原型 Deregister(prototype string) // Get 获取实体原型 Get(prototype string) (EntityPT, bool) // Range 遍历所有已注册的实体原型 Range(fun generic.Func1[EntityPT, bool]) }
EntityLib 实体原型库
type EntityPT ¶
type EntityPT struct { Prototype string // 实体原型名称 // contains filtered or unexported fields }
EntityPT 实体原型
func (EntityPT) Assemble ¶
func (pt EntityPT) Assemble(entity ec.Entity, componentCtor ComponentCtor, entityCtor EntityCtor) ec.Entity
Assemble 向实体安装组件
func (EntityPT) UnsafeConstruct
deprecated
func (pt EntityPT) UnsafeConstruct(options ConstructEntityOptions) ec.Entity
Deprecated: UnsafeConstruct 内部创建实体
type EntityPTProvider ¶
type EntityPTProvider interface { // GetEntityLib 获取实体原型库 GetEntityLib() EntityLib }
EntityPTProvider 实体原型提供者
Click to show internal directories.
Click to hide internal directories.