pt

package
v0.2.57 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: LGPL-2.1 Imports: 13 Imported by: 1

Documentation

Overview

Package pt 实体与组件原型,用于创建实例。

Index

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 CompAlias added in v0.2.39

func CompAlias(comp any, alias string) _CompAlias

CompAlias 组件与别名,用于注册实体原型时自定义组件别名

func CompInterface added in v0.2.39

func CompInterface[FACE any](comp any) _CompAlias

CompInterface 组件与接口,用于注册实体原型时使用接口名作为别名

Types

type ComponentCtor

type ComponentCtor = generic.DelegateAction2[ec.Entity, ec.Component] // 组件构造函数

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 组件原型库

func DefaultComponentLib

func DefaultComponentLib() ComponentLib

DefaultComponentLib 默认组件库

func NewComponentLib

func NewComponentLib() ComponentLib

NewComponentLib 创建组件原型库

type ComponentPT

type ComponentPT struct {
	Name string // 组件名称
	// contains filtered or unexported fields
}

ComponentPT 组件原型

func (ComponentPT) Construct

func (pt ComponentPT) Construct() ec.Component

Construct 创建组件

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 创建组件复合器

func (*Composite[T]) As

func (c *Composite[T]) As() (T, bool)

As 从实体提取一些需要的组件接口,复合在一起直接使用(实体更新组件后,会自动重新提取)

func (*Composite[T]) Cast

func (c *Composite[T]) Cast() T

Cast 从实体提取一些需要的组件接口,复合在一起直接使用,提取失败会panic(实体更新组件后,会自动重新提取)

func (*Composite[T]) Changed

func (c *Composite[T]) Changed() bool

Changed 实体是否已更新组件

func (*Composite[T]) Entity

func (c *Composite[T]) Entity() ec.Entity

Entity 实体

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 实体原型库

func DefaultEntityLib

func DefaultEntityLib() EntityLib

DefaultEntityLib 默认实体库

func NewEntityLib

func NewEntityLib(compLib ComponentLib) EntityLib

NewEntityLib 创建实体原型库

type EntityPT

type EntityPT struct {
	Prototype string // 实体原型名称
	// contains filtered or unexported fields
}

EntityPT 实体原型

func Using

func Using(entityPTProvider EntityPTProvider, prototype string) EntityPT

Using 使用实体原型

func (EntityPT) Assemble

func (pt EntityPT) Assemble(entity ec.Entity, componentCtor ComponentCtor, entityCtor EntityCtor) ec.Entity

Assemble 向实体安装组件

func (EntityPT) Construct

func (pt EntityPT) Construct(settings ...option.Setting[ConstructEntityOptions]) ec.Entity

Construct 创建实体

func (EntityPT) UnsafeConstruct deprecated

func (pt EntityPT) UnsafeConstruct(options ConstructEntityOptions) ec.Entity

Deprecated: UnsafeConstruct 内部创建实体

type EntityPTProvider

type EntityPTProvider interface {
	// GetEntityLib 获取实体原型库
	GetEntityLib() EntityLib
}

EntityPTProvider 实体原型提供者

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL