ec

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: 13 Imported by: 5

Documentation

Overview

Package ec 提供了一个EC(Entity-Component)组件框架,用于帮助开发者组织代码架构。

在这个框架中,我们有以下核心概念:

  • Entity(实体):代表了应用程序中的一个实际对象。以游戏举例,可以是场景,也可以是玩家、NPC、物品等。实体本身只提供生命周期管理、组件管理两项核心能力,行为和属性将由不同的组件来提供。
  • Component(组件):为实体提供行为与属性,不同的组件可以为实体提供不同的行为和状态。以游戏举例,为实体添加兔子外观组件、飞行组件、挖洞组件,那么我们将会得到一个即会飞行又会挖洞的拥有兔子外观的实体。
  • Context(上下文):实体的运行环境,通常用于提供全局属性与方法。因为Golaxy主要用于服务器逻辑开发,需要支持多线程并发环境,所以提供了运行时上下文(runtime context)、服务上下文(service context)两个上下文,分别对应本地单线程环境与并发多线程环境。

EC组件框架基于组合模式,在大部分情况下优于继承模式,开发者可以更好地组织代码,实现高内聚和低耦合的代码架构。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEC = fmt.Errorf("%w: ec", exception.ErrCore) // EC错误
)
View Source
var With _Option

Functions

func BindEventComponentDestroySelf

func BindEventComponentDestroySelf(auto iAutoEventComponentDestroySelf, subscriber EventComponentDestroySelf, priority ...int32) event.Hook

func BindEventComponentMgrAddComponents added in v0.2.58

func BindEventComponentMgrAddComponents(auto iAutoEventComponentMgrAddComponents, subscriber EventComponentMgrAddComponents, priority ...int32) event.Hook

func BindEventComponentMgrFirstAccessComponent added in v0.2.58

func BindEventComponentMgrFirstAccessComponent(auto iAutoEventComponentMgrFirstAccessComponent, subscriber EventComponentMgrFirstAccessComponent, priority ...int32) event.Hook

func BindEventComponentMgrRemoveComponent added in v0.2.58

func BindEventComponentMgrRemoveComponent(auto iAutoEventComponentMgrRemoveComponent, subscriber EventComponentMgrRemoveComponent, priority ...int32) event.Hook

func BindEventEntityDestroySelf

func BindEventEntityDestroySelf(auto iAutoEventEntityDestroySelf, subscriber EventEntityDestroySelf, priority ...int32) event.Hook

func BindEventTreeNodeAddChild added in v0.2.58

func BindEventTreeNodeAddChild(auto iAutoEventTreeNodeAddChild, subscriber EventTreeNodeAddChild, priority ...int32) event.Hook

func BindEventTreeNodeEnterParent added in v0.2.58

func BindEventTreeNodeEnterParent(auto iAutoEventTreeNodeEnterParent, subscriber EventTreeNodeEnterParent, priority ...int32) event.Hook

func BindEventTreeNodeLeaveParent added in v0.2.58

func BindEventTreeNodeLeaveParent(auto iAutoEventTreeNodeLeaveParent, subscriber EventTreeNodeLeaveParent, priority ...int32) event.Hook

func BindEventTreeNodeRemoveChild added in v0.2.58

func BindEventTreeNodeRemoveChild(auto iAutoEventTreeNodeRemoveChild, subscriber EventTreeNodeRemoveChild, priority ...int32) event.Hook

func UnsafeComponent deprecated

func UnsafeComponent(comp Component) _UnsafeComponent

Deprecated: UnsafeComponent 访问组件内部函数

func UnsafeEntity deprecated

func UnsafeEntity(entity Entity) _UnsafeEntity

Deprecated: UnsafeEntity 访问实体内部函数

Types

type Component

type Component interface {
	concurrent.CurrentContextProvider
	fmt.Stringer

	// GetId 获取组件Id
	GetId() uid.Id
	// GetName 获取组件名称
	GetName() string
	// GetEntity 获取组件依附的实体
	GetEntity() Entity
	// GetState 获取组件状态
	GetState() ComponentState
	// GetReflected 获取反射值
	GetReflected() reflect.Value
	// DestroySelf 销毁自身
	DestroySelf()
	// contains filtered or unexported methods
}

Component 组件接口

type ComponentBehavior

type ComponentBehavior struct {
	// contains filtered or unexported fields
}

ComponentBehavior 组件行为,需要在开发新组件时,匿名嵌入至组件结构体中

func (*ComponentBehavior) DestroySelf

func (comp *ComponentBehavior) DestroySelf()

DestroySelf 销毁自身

func (*ComponentBehavior) GetConcurrentContext added in v0.2.32

func (comp *ComponentBehavior) GetConcurrentContext() iface.Cache

GetConcurrentContext 获取多线程安全的上下文

func (*ComponentBehavior) GetCurrentContext

func (comp *ComponentBehavior) GetCurrentContext() iface.Cache

GetCurrentContext 获取当前上下文

func (*ComponentBehavior) GetEntity

func (comp *ComponentBehavior) GetEntity() Entity

GetEntity 获取组件依附的实体

func (*ComponentBehavior) GetId

func (comp *ComponentBehavior) GetId() uid.Id

GetId 获取组件Id

func (*ComponentBehavior) GetName

func (comp *ComponentBehavior) GetName() string

GetName 获取组件名称

func (*ComponentBehavior) GetReflected added in v0.2.58

func (comp *ComponentBehavior) GetReflected() reflect.Value

GetReflected 获取反射值

func (*ComponentBehavior) GetState

func (comp *ComponentBehavior) GetState() ComponentState

GetState 获取组件状态

func (*ComponentBehavior) ManagedHooks added in v0.2.27

func (comp *ComponentBehavior) ManagedHooks(hooks ...event.Hook)

ManagedHooks 托管hook,在组件销毁时自动解绑定

func (*ComponentBehavior) String

func (comp *ComponentBehavior) String() string

String implements fmt.Stringer

type ComponentState

type ComponentState int8

ComponentState 组件状态

const (
	ComponentState_Birth  ComponentState = iota // 出生
	ComponentState_Attach                       // 附着
	ComponentState_Awake                        // 唤醒
	ComponentState_Start                        // 开始
	ComponentState_Living                       // 活跃
	ComponentState_Detach                       // 脱离
	ComponentState_Shut                         // 结束
	ComponentState_Death                        // 死亡
)

func (ComponentState) String

func (i ComponentState) String() string

type ConcurrentEntity

type ConcurrentEntity interface {
	concurrent.ConcurrentContextProvider
	fmt.Stringer

	// GetId 获取实体Id
	GetId() uid.Id
	// GetPrototype 获取实体原型
	GetPrototype() string
}

ConcurrentEntity 多线程安全的实体接口

type Entity

type Entity interface {
	concurrent.CurrentContextProvider
	reinterpret.CompositeProvider
	fmt.Stringer

	// GetId 获取实体Id
	GetId() uid.Id
	// GetPrototype 获取实体原型
	GetPrototype() string
	// GetScope 获取可访问作用域
	GetScope() Scope
	// GetState 获取实体状态
	GetState() EntityState
	// GetReflected 获取反射值
	GetReflected() reflect.Value
	// GetMeta 获取Meta信息
	GetMeta() Meta
	// DestroySelf 销毁自身
	DestroySelf()
	// contains filtered or unexported methods
}

Entity 实体接口

func NewEntity

func NewEntity(settings ...option.Setting[EntityOptions]) Entity

NewEntity 创建实体

func UnsafeNewEntity deprecated

func UnsafeNewEntity(options EntityOptions) Entity

Deprecated: UnsafeNewEntity 内部创建实体

type EntityBehavior

type EntityBehavior struct {
	// contains filtered or unexported fields
}

EntityBehavior 实体行为,在需要扩展实体能力时,匿名嵌入至实体结构体中

func (*EntityBehavior) AddComponent

func (entity *EntityBehavior) AddComponent(name string, components ...Component) error

AddComponent 添加组件,因为同个名称可以指向多个组件,所以名称指向的组件已存在时,不会返回错误

func (*EntityBehavior) ContainsComponent added in v0.2.58

func (entity *EntityBehavior) ContainsComponent(name string) bool

ContainsComponent 组件是否存在

func (*EntityBehavior) ContainsComponentById added in v0.2.58

func (entity *EntityBehavior) ContainsComponentById(id uid.Id) bool

ContainsComponentById 使用组件Id检测组件是否存在

func (*EntityBehavior) CountComponents

func (entity *EntityBehavior) CountComponents() int

CountComponents 统计所有组件数量

func (*EntityBehavior) DestroySelf

func (entity *EntityBehavior) DestroySelf()

DestroySelf 销毁自身

func (*EntityBehavior) EventComponentMgrAddComponents added in v0.2.58

func (entity *EntityBehavior) EventComponentMgrAddComponents() event.IEvent

EventComponentMgrAddComponents 事件:实体的组件管理器添加组件

func (*EntityBehavior) EventComponentMgrFirstAccessComponent added in v0.2.58

func (entity *EntityBehavior) EventComponentMgrFirstAccessComponent() event.IEvent

EventComponentMgrFirstAccessComponent 事件:实体的组件管理器首次访问组件

func (*EntityBehavior) EventComponentMgrRemoveComponent added in v0.2.58

func (entity *EntityBehavior) EventComponentMgrRemoveComponent() event.IEvent

EventComponentMgrRemoveComponent 事件:实体的组件管理器删除组件

func (*EntityBehavior) EventTreeNodeAddChild added in v0.2.58

func (entity *EntityBehavior) EventTreeNodeAddChild() event.IEvent

EventTreeNodeAddChild 事件:实体节点添加子实体

func (*EntityBehavior) EventTreeNodeEnterParent added in v0.2.58

func (entity *EntityBehavior) EventTreeNodeEnterParent() event.IEvent

EventTreeNodeEnterParent 事件:实体加入父实体节点

func (*EntityBehavior) EventTreeNodeLeaveParent added in v0.2.58

func (entity *EntityBehavior) EventTreeNodeLeaveParent() event.IEvent

EventTreeNodeLeaveParent 事件:实体离开父实体节点

func (*EntityBehavior) EventTreeNodeRemoveChild added in v0.2.58

func (entity *EntityBehavior) EventTreeNodeRemoveChild() event.IEvent

EventTreeNodeRemoveChild 事件:实体节点删除子实体

func (*EntityBehavior) FilterComponents added in v0.2.58

func (entity *EntityBehavior) FilterComponents(fun generic.Func1[Component, bool]) []Component

FilterComponents 过滤并获取组件

func (*EntityBehavior) GetComponent

func (entity *EntityBehavior) GetComponent(name string) Component

GetComponent 使用名称查询组件,同个名称指向多个组件时,返回首个组件

func (*EntityBehavior) GetComponentById

func (entity *EntityBehavior) GetComponentById(id uid.Id) Component

GetComponentById 使用组件Id查询组件

func (*EntityBehavior) GetComponents

func (entity *EntityBehavior) GetComponents() []Component

GetComponents 获取所有组件

func (*EntityBehavior) GetCompositeFaceCache added in v0.2.34

func (entity *EntityBehavior) GetCompositeFaceCache() iface.Cache

GetCompositeFaceCache 支持重新解释类型

func (*EntityBehavior) GetConcurrentContext

func (entity *EntityBehavior) GetConcurrentContext() iface.Cache

GetConcurrentContext 解析线程安全的上下文

func (*EntityBehavior) GetCurrentContext

func (entity *EntityBehavior) GetCurrentContext() iface.Cache

GetCurrentContext 获取当前上下文

func (*EntityBehavior) GetId

func (entity *EntityBehavior) GetId() uid.Id

GetId 获取实体Id

func (*EntityBehavior) GetMeta

func (entity *EntityBehavior) GetMeta() Meta

GetMeta 获取Meta信息

func (*EntityBehavior) GetPrototype

func (entity *EntityBehavior) GetPrototype() string

GetPrototype 获取实体原型

func (*EntityBehavior) GetReflected added in v0.2.58

func (entity *EntityBehavior) GetReflected() reflect.Value

GetReflected 获取反射值

func (*EntityBehavior) GetScope added in v0.2.23

func (entity *EntityBehavior) GetScope() Scope

GetScope 获取可访问作用域

func (*EntityBehavior) GetState

func (entity *EntityBehavior) GetState() EntityState

GetState 获取实体状态

func (*EntityBehavior) GetTreeNodeParent added in v0.2.58

func (entity *EntityBehavior) GetTreeNodeParent() (Entity, bool)

GetTreeNodeParent 获取在实体树中的父实体

func (*EntityBehavior) GetTreeNodeState added in v0.2.58

func (entity *EntityBehavior) GetTreeNodeState() TreeNodeState

GetTreeNodeState 获取实体树节点状态

func (*EntityBehavior) ManagedHooks added in v0.2.27

func (entity *EntityBehavior) ManagedHooks(hooks ...event.Hook)

ManagedHooks 托管hook,在实体销毁时自动解绑定

func (*EntityBehavior) RangeComponents

func (entity *EntityBehavior) RangeComponents(fun generic.Func1[Component, bool])

RangeComponents 遍历所有组件

func (*EntityBehavior) RemoveComponent

func (entity *EntityBehavior) RemoveComponent(name string)

RemoveComponent 使用名称删除组件,将会删除同个名称指向的多个组件

func (*EntityBehavior) RemoveComponentById

func (entity *EntityBehavior) RemoveComponentById(id uid.Id)

RemoveComponentById 使用组件Id删除组件

func (*EntityBehavior) ReversedRangeComponents added in v0.2.58

func (entity *EntityBehavior) ReversedRangeComponents(fun generic.Func1[Component, bool])

ReversedRangeComponents 反向遍历所有组件

func (*EntityBehavior) String

func (entity *EntityBehavior) String() string

String implements fmt.Stringer

type EntityOptions

type EntityOptions struct {
	CompositeFace      iface.Face[Entity] // 扩展者,在扩展实体自身能力时使用
	Prototype          string             // 实体原型名称
	Scope              Scope              // 可访问作用域
	PersistId          uid.Id             // 实体持久化Id
	AwakeOnFirstAccess bool               // 开启组件被首次访问时,检测并调用Awake()
	Meta               Meta               // Meta信息
}

EntityOptions 创建实体的所有选项

type EntityState

type EntityState int8

EntityState 实体状态

const (
	EntityState_Birth  EntityState = iota // 出生
	EntityState_Enter                     // 进入容器
	EntityState_Awake                     // 唤醒
	EntityState_Start                     // 开始
	EntityState_Living                    // 活跃
	EntityState_Leave                     // 离开容器
	EntityState_Shut                      // 结束
	EntityState_Death                     // 死亡
)

func (EntityState) String

func (i EntityState) String() string

type EventComponentDestroySelf

type EventComponentDestroySelf interface {
	OnComponentDestroySelf(comp Component)
}

EventComponentDestroySelf [EmitUnExport] 事件:组件销毁自身

type EventComponentDestroySelfHandler added in v0.2.51

type EventComponentDestroySelfHandler func(comp Component)

func HandleEventComponentDestroySelf added in v0.2.50

func HandleEventComponentDestroySelf(fun func(comp Component)) EventComponentDestroySelfHandler

func (EventComponentDestroySelfHandler) OnComponentDestroySelf added in v0.2.51

func (h EventComponentDestroySelfHandler) OnComponentDestroySelf(comp Component)

type EventComponentMgrAddComponents added in v0.2.58

type EventComponentMgrAddComponents interface {
	OnComponentMgrAddComponents(entity Entity, components []Component)
}

EventComponentMgrAddComponents [EmitUnExport] 事件:实体的组件管理器添加组件

type EventComponentMgrAddComponentsHandler added in v0.2.58

type EventComponentMgrAddComponentsHandler func(entity Entity, components []Component)

func HandleEventComponentMgrAddComponents added in v0.2.58

func HandleEventComponentMgrAddComponents(fun func(entity Entity, components []Component)) EventComponentMgrAddComponentsHandler

func (EventComponentMgrAddComponentsHandler) OnComponentMgrAddComponents added in v0.2.58

func (h EventComponentMgrAddComponentsHandler) OnComponentMgrAddComponents(entity Entity, components []Component)

type EventComponentMgrFirstAccessComponent added in v0.2.58

type EventComponentMgrFirstAccessComponent interface {
	OnComponentMgrFirstAccessComponent(entity Entity, component Component)
}

EventComponentMgrFirstAccessComponent [EmitUnExport] 事件:实体的组件管理器首次访问组件

type EventComponentMgrFirstAccessComponentHandler added in v0.2.58

type EventComponentMgrFirstAccessComponentHandler func(entity Entity, component Component)

func HandleEventComponentMgrFirstAccessComponent added in v0.2.58

func HandleEventComponentMgrFirstAccessComponent(fun func(entity Entity, component Component)) EventComponentMgrFirstAccessComponentHandler

func (EventComponentMgrFirstAccessComponentHandler) OnComponentMgrFirstAccessComponent added in v0.2.58

func (h EventComponentMgrFirstAccessComponentHandler) OnComponentMgrFirstAccessComponent(entity Entity, component Component)

type EventComponentMgrRemoveComponent added in v0.2.58

type EventComponentMgrRemoveComponent interface {
	OnComponentMgrRemoveComponent(entity Entity, component Component)
}

EventComponentMgrRemoveComponent [EmitUnExport] 事件:实体的组件管理器删除组件

type EventComponentMgrRemoveComponentHandler added in v0.2.58

type EventComponentMgrRemoveComponentHandler func(entity Entity, component Component)

func HandleEventComponentMgrRemoveComponent added in v0.2.58

func HandleEventComponentMgrRemoveComponent(fun func(entity Entity, component Component)) EventComponentMgrRemoveComponentHandler

func (EventComponentMgrRemoveComponentHandler) OnComponentMgrRemoveComponent added in v0.2.58

func (h EventComponentMgrRemoveComponentHandler) OnComponentMgrRemoveComponent(entity Entity, component Component)

type EventEntityDestroySelf

type EventEntityDestroySelf interface {
	OnEntityDestroySelf(entity Entity)
}

EventEntityDestroySelf [EmitUnExport] 事件:实体销毁自身

type EventEntityDestroySelfHandler added in v0.2.51

type EventEntityDestroySelfHandler func(entity Entity)

func HandleEventEntityDestroySelf added in v0.2.50

func HandleEventEntityDestroySelf(fun func(entity Entity)) EventEntityDestroySelfHandler

func (EventEntityDestroySelfHandler) OnEntityDestroySelf added in v0.2.51

func (h EventEntityDestroySelfHandler) OnEntityDestroySelf(entity Entity)

type EventTreeNodeAddChild added in v0.2.58

type EventTreeNodeAddChild interface {
	OnTreeNodeAddChild(parent, child Entity)
}

EventTreeNodeAddChild [EmitUnExport] 事件:实体节点添加子实体

type EventTreeNodeAddChildHandler added in v0.2.58

type EventTreeNodeAddChildHandler func(parent, child Entity)

func HandleEventTreeNodeAddChild added in v0.2.58

func HandleEventTreeNodeAddChild(fun func(parent, child Entity)) EventTreeNodeAddChildHandler

func (EventTreeNodeAddChildHandler) OnTreeNodeAddChild added in v0.2.58

func (h EventTreeNodeAddChildHandler) OnTreeNodeAddChild(parent, child Entity)

type EventTreeNodeEnterParent added in v0.2.58

type EventTreeNodeEnterParent interface {
	OnTreeNodeEnterParent(child, parent Entity)
}

EventTreeNodeEnterParent [EmitUnExport] 事件:实体加入父实体节点

type EventTreeNodeEnterParentHandler added in v0.2.58

type EventTreeNodeEnterParentHandler func(child, parent Entity)

func HandleEventTreeNodeEnterParent added in v0.2.58

func HandleEventTreeNodeEnterParent(fun func(child, parent Entity)) EventTreeNodeEnterParentHandler

func (EventTreeNodeEnterParentHandler) OnTreeNodeEnterParent added in v0.2.58

func (h EventTreeNodeEnterParentHandler) OnTreeNodeEnterParent(child, parent Entity)

type EventTreeNodeLeaveParent added in v0.2.58

type EventTreeNodeLeaveParent interface {
	OnTreeNodeLeaveParent(child, parent Entity)
}

EventTreeNodeLeaveParent [EmitUnExport] 事件:实体离开父实体节点

type EventTreeNodeLeaveParentHandler added in v0.2.58

type EventTreeNodeLeaveParentHandler func(child, parent Entity)

func HandleEventTreeNodeLeaveParent added in v0.2.58

func HandleEventTreeNodeLeaveParent(fun func(child, parent Entity)) EventTreeNodeLeaveParentHandler

func (EventTreeNodeLeaveParentHandler) OnTreeNodeLeaveParent added in v0.2.58

func (h EventTreeNodeLeaveParentHandler) OnTreeNodeLeaveParent(child, parent Entity)

type EventTreeNodeRemoveChild added in v0.2.58

type EventTreeNodeRemoveChild interface {
	OnTreeNodeRemoveChild(parent, child Entity)
}

EventTreeNodeRemoveChild [EmitUnExport] 事件:实体节点删除子实体

type EventTreeNodeRemoveChildHandler added in v0.2.58

type EventTreeNodeRemoveChildHandler func(parent, child Entity)

func HandleEventTreeNodeRemoveChild added in v0.2.58

func HandleEventTreeNodeRemoveChild(fun func(parent, child Entity)) EventTreeNodeRemoveChildHandler

func (EventTreeNodeRemoveChildHandler) OnTreeNodeRemoveChild added in v0.2.58

func (h EventTreeNodeRemoveChildHandler) OnTreeNodeRemoveChild(parent, child Entity)

type Meta

type Meta map[string]any

func (Meta) Get

func (m Meta) Get(k string) any

func (Meta) Range

func (m Meta) Range(fun func(k string, v any) bool)

type Scope

type Scope int32

Scope 可访问作用域

const (
	Scope_Local  Scope = iota // 本地可以访问
	Scope_Global              // 全局可以访问
)

func (Scope) String

func (i Scope) String() string

type TreeNodeState added in v0.2.58

type TreeNodeState int8

TreeNodeState 实体树节点状态

const (
	TreeNodeState_Freedom   TreeNodeState = iota // 自由实体
	TreeNodeState_Attaching                      // 正在加入实体树
	TreeNodeState_Attached                       // 在实体树中
	TreeNodeState_Detaching                      // 正在脱离实体树
)

func (TreeNodeState) String added in v0.2.58

func (i TreeNodeState) String() string

Jump to

Keyboard shortcuts

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