pt

package
v0.2.60 Latest Latest
Warning

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

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPt = fmt.Errorf("%w: pt", exception.ErrCore) // 原型错误
)

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 CompInfo added in v0.2.58

type CompInfo struct {
	PT    ComponentPT // 原型
	Alias string      // 别名
}

CompInfo 组件信息

type ComponentLib

type ComponentLib interface {
	// Declare 声明组件原型
	Declare(comp any, aliases ...string) ComponentPT
	// Undeclare 取消声明组件原型
	Undeclare(name string)
	// Get 获取组件原型
	Get(name string) (ComponentPT, bool)
	// GetAlias 使用别名获取组件原型
	GetAlias(alias string) []ComponentPT
	// Range 遍历所有已注册的组件原型
	Range(fun generic.Func1[ComponentPT, bool])
	// ReversedRange 反向遍历所有已注册的组件原型
	ReversedRange(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       // 组件名称
	RType reflect.Type // 反射类型
}

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 EntityLib

type EntityLib interface {
	EntityPTProvider

	// Declare 声明实体原型
	Declare(prototype string, comps ...any) EntityPT
	// Undeclare 取消声明实体原型
	Undeclare(prototype string)
	// Get 获取实体原型
	Get(prototype string) (EntityPT, bool)
	// Range 遍历所有已注册的实体原型
	Range(fun generic.Func1[EntityPT, bool])
	// ReversedRange 反向遍历所有已注册的实体原型
	ReversedRange(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     // 实体原型名称
	Components []CompInfo // 组件信息
}

EntityPT 实体原型

func For added in v0.2.58

func For(provider EntityPTProvider, prototype string) EntityPT

For 查询实体原型

func (EntityPT) Assemble

func (pt EntityPT) Assemble(entity ec.Entity) ec.Entity

Assemble 向实体安装组件

func (EntityPT) Construct

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

Construct 创建实体

func (EntityPT) UnsafeConstruct deprecated

func (pt EntityPT) UnsafeConstruct(options ec.EntityOptions) 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