pt

package
v0.3.91 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: LGPL-2.1 Imports: 11 Imported by: 3

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 any](entity ec.Entity) (*T, bool)

As 从实体中提取一些需要的组件,复合在一起直接使用。

示例:

type IA interface {
	MethodA()
}
...
type IB interface {
	MethodB()
}
...
type CompositeAB struct {
	IA `ec:"A"`			// 指定组件名
	IB `ec:",CompB"`	// 指定组件原型名
}
...
entity.AddComponent("A", compA)
entity.AddComponent("B", compB)
...
v, ok := As[CompositeAB](entity)
if ok {
	v.MethodA()
	v.MethodB()
}

注意:

1.类型参数必须为结构体类型,可以是匿名结构体。
2.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
3.实体更新组件后,需要重新提取。
4.需要使用tag标记组件名或组件原型名,若没有标记,将会尝试使用字段名作为组件名去查找。
5.提取失败会返回false。

func Cast

func Cast[T any](entity ec.Entity) *T

Cast 从实体中提取一些需要的组件,复合在一起直接使用,提取失败会panic。

示例:

type IA interface {
	MethodA()
}
...
type IB interface {
	MethodB()
}
...
type CompositeAB struct {
	IA `ec:"A"`			// 指定组件名
	IB `ec:",CompB"`	// 指定组件原型名
}
...
entity.AddComponent("A", compA)
entity.AddComponent("B", compB)
...
Cast[CompositeAB](entity).MethodA()
Cast[CompositeAB](entity).MethodB()

注意:

1.类型参数必须为结构体类型,可以是匿名结构体。
2.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
3.实体更新组件后,需要重新提取。
4.需要使用tag标记组件名或组件原型名,若没有标记,将会尝试使用字段名作为组件名去查找。
5.提取失败会panic。

func For

func For(provider EntityPTProvider, prototype string) ec.EntityPT

For 查询实体原型

func UnsafeEntityLib deprecated added in v0.3.70

func UnsafeEntityLib(entityLib EntityLib) _UnsafeEntityLib

Deprecated: UnsafeEntityLib 访问实体原型库的内部方法

Types

type ComponentAttribute

type ComponentAttribute struct {
	Instance  any                           // 组件实例(必填)
	Name      string                        // 组件名称
	Removable bool                          // 是否可以删除
	Extra     generic.SliceMap[string, any] // 自定义属性
}

ComponentAttribute 组件原型属性

func Component added in v0.3.83

func Component(instance any) ComponentAttribute

Component 创建组件原型属性,用于注册实体原型时自定义相关属性

func (ComponentAttribute) CombineExtra added in v0.3.83

func (atti ComponentAttribute) CombineExtra(extra map[string]any) ComponentAttribute

func (ComponentAttribute) OverrideExtra added in v0.3.83

func (atti ComponentAttribute) OverrideExtra(extra map[string]any) ComponentAttribute

func (ComponentAttribute) SetExtra added in v0.3.83

func (atti ComponentAttribute) SetExtra(extra map[string]any) ComponentAttribute

func (ComponentAttribute) SetName added in v0.3.83

func (atti ComponentAttribute) SetName(name string) ComponentAttribute

func (ComponentAttribute) SetRemovable added in v0.3.83

func (atti ComponentAttribute) SetRemovable(b bool) ComponentAttribute

type ComponentLib

type ComponentLib interface {
	// Declare 声明组件原型(可以重复声明)
	Declare(comp any) ec.ComponentPT
	// Undeclare 取消声明组件原型
	Undeclare(prototype string)
	// Get 获取组件原型
	Get(prototype string) (ec.ComponentPT, bool)
	// Range 遍历所有已注册的组件原型
	Range(fun generic.Func1[ec.ComponentPT, bool])
	// ReversedRange 反向遍历所有已注册的组件原型
	ReversedRange(fun generic.Func1[ec.ComponentPT, bool])
}

ComponentLib 组件原型库

func DefaultComponentLib

func DefaultComponentLib() ComponentLib

DefaultComponentLib 默认组件库

func NewComponentLib

func NewComponentLib() ComponentLib

NewComponentLib 创建组件原型库

type Composite

type Composite[T any] struct {
	// contains filtered or unexported fields
}

Composite 组件复合器,直接使用As()或Cast()时,无法检测提取后实体是否又更新组件,使用复合器可以解决此问题。

示例:

type IA interface {
	MethodA()
}
...
type IB interface {
	MethodB()
}
...
type CompositeAB struct {
	IA `ec:"A"`			// 指定组件名
	IB `ec:",CompB"`	// 指定组件原型名
}
...
entity.AddComponent("A", compA)
entity.AddComponent("B", compB)
...
cx := Compose[CompositeAB](entity)
if v, ok := cx.As(); ok {
	v.MethodA()
	v.MethodB()
}
cx.Cast().MethodA()
cx.Cast().MethodB()

func Compose

func Compose[T any](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 EntityAttribute

type EntityAttribute struct {
	Prototype                  string                        // 实体原型名称(必填)
	Instance                   any                           // 实体实例
	Scope                      *ec.Scope                     // 可访问作用域
	ComponentNameIndexing      *bool                         // 是否开启组件名称索引
	ComponentAwakeOnFirstTouch *bool                         // 当实体组件首次被访问时,生命周期是否进入唤醒(Awake)
	ComponentUniqueID          *bool                         // 是否为实体组件分配唯一Id
	Extra                      generic.SliceMap[string, any] // 自定义属性
}

EntityAttribute 实体原型属性

func Entity added in v0.3.83

func Entity(prototype string) EntityAttribute

Entity 创建实体原型属性,用于注册实体原型时自定义相关属性

func (EntityAttribute) CombineExtra added in v0.3.83

func (atti EntityAttribute) CombineExtra(extra map[string]any) EntityAttribute

func (EntityAttribute) OverrideExtra added in v0.3.83

func (atti EntityAttribute) OverrideExtra(extra map[string]any) EntityAttribute

func (EntityAttribute) SetComponentAwakeOnFirstTouch added in v0.3.83

func (atti EntityAttribute) SetComponentAwakeOnFirstTouch(b bool) EntityAttribute

func (EntityAttribute) SetComponentNameIndexing added in v0.3.84

func (atti EntityAttribute) SetComponentNameIndexing(b bool) EntityAttribute

func (EntityAttribute) SetComponentUniqueID added in v0.3.83

func (atti EntityAttribute) SetComponentUniqueID(b bool) EntityAttribute

func (EntityAttribute) SetExtra added in v0.3.83

func (atti EntityAttribute) SetExtra(extra map[string]any) EntityAttribute

func (EntityAttribute) SetInstance added in v0.3.83

func (atti EntityAttribute) SetInstance(instance any) EntityAttribute

func (EntityAttribute) SetScope added in v0.3.83

func (atti EntityAttribute) SetScope(scope ec.Scope) EntityAttribute

type EntityLib

type EntityLib interface {
	EntityPTProvider

	// GetComponentLib 获取组件原型库
	GetComponentLib() ComponentLib
	// Declare 声明实体原型
	Declare(prototype any, comps ...any) ec.EntityPT
	// Redeclare 重声明实体原型
	Redeclare(prototype any, comps ...any) ec.EntityPT
	// Undeclare 取消声明实体原型
	Undeclare(prototype string)
	// Get 获取实体原型
	Get(prototype string) (ec.EntityPT, bool)
	// Range 遍历所有已注册的实体原型
	Range(fun generic.Func1[ec.EntityPT, bool])
	// ReversedRange 反向遍历所有已注册的实体原型
	ReversedRange(fun generic.Func1[ec.EntityPT, bool])
	// contains filtered or unexported methods
}

EntityLib 实体原型库

func NewEntityLib

func NewEntityLib(compLib ComponentLib) EntityLib

NewEntityLib 创建实体原型库

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