Documentation ¶
Overview ¶
Package ec 提供了一个EC(Entity-Component)组件框架,用于帮助开发者组织代码架构。
在这个框架中,我们有以下核心概念:
- Entity(实体):代表了应用程序中的一个实际对象。以游戏举例,可以是场景,也可以是玩家、NPC、物品等。实体本身只提供生命周期管理、组件管理两项核心能力,行为和属性将由不同的组件来提供。
- Component(组件):为实体提供行为与属性,不同的组件可以为实体提供不同的行为和状态。以游戏举例,为实体添加兔子外观组件、飞行组件、挖洞组件,那么我们将会得到一个即会飞行又会挖洞的拥有兔子外观的实体。
- Context(上下文):实体的运行环境,通常用于提供全局属性与方法。因为Golaxy主要用于服务器逻辑开发,需要支持多线程并发环境,所以提供了运行时上下文(runtime context)、服务上下文(service context)两个上下文,分别对应本地单线程环境与并发多线程环境。
EC组件框架基于组合模式,在大部分情况下优于继承模式,开发者可以更好地组织代码,实现高内聚和低耦合的代码架构。
Index ¶
- Variables
- func BindEventCompMgrAddComponents(auto _AutoEventCompMgrAddComponents, subscriber EventCompMgrAddComponents, ...) event.Hook
- func BindEventCompMgrFirstAccessComponent(auto _AutoEventCompMgrFirstAccessComponent, ...) event.Hook
- func BindEventCompMgrRemoveComponent(auto _AutoEventCompMgrRemoveComponent, subscriber EventCompMgrRemoveComponent, ...) event.Hook
- func BindEventComponentDestroySelf(auto _AutoEventComponentDestroySelf, subscriber EventComponentDestroySelf, ...) event.Hook
- func BindEventEntityDestroySelf(auto _AutoEventEntityDestroySelf, subscriber EventEntityDestroySelf, ...) event.Hook
- func UnsafeComponent(comp Component) _UnsafeComponentdeprecated
- func UnsafeEntity(entity Entity) _UnsafeEntitydeprecated
- type Component
- type ComponentBehavior
- func (comp *ComponentBehavior) DestroySelf()
- func (comp *ComponentBehavior) GetConcurrentContext() iface.Cache
- func (comp *ComponentBehavior) GetCurrentContext() iface.Cache
- func (comp *ComponentBehavior) GetEntity() Entity
- func (comp *ComponentBehavior) GetId() uid.Id
- func (comp *ComponentBehavior) GetName() string
- func (comp *ComponentBehavior) GetState() ComponentState
- func (comp *ComponentBehavior) ManagedHooks(hooks ...event.Hook)
- func (comp *ComponentBehavior) String() string
- type ComponentState
- type ConcurrentEntity
- type ECNodeState
- type Entity
- type EntityBehavior
- func (entity *EntityBehavior) AddComponent(name string, component Component) error
- func (entity *EntityBehavior) AddComponents(name string, components []Component) error
- func (entity *EntityBehavior) CountComponents() int
- func (entity *EntityBehavior) DestroySelf()
- func (entity *EntityBehavior) EventCompMgrAddComponents() event.IEvent
- func (entity *EntityBehavior) EventCompMgrFirstAccessComponent() event.IEvent
- func (entity *EntityBehavior) EventCompMgrRemoveComponent() event.IEvent
- func (entity *EntityBehavior) GetComponent(name string) Component
- func (entity *EntityBehavior) GetComponentById(id uid.Id) Component
- func (entity *EntityBehavior) GetComponents(name string) []Component
- func (entity *EntityBehavior) GetCompositeFaceCache() iface.Cache
- func (entity *EntityBehavior) GetConcurrentContext() iface.Cache
- func (entity *EntityBehavior) GetCurrentContext() iface.Cache
- func (entity *EntityBehavior) GetECNodeState() ECNodeState
- func (entity *EntityBehavior) GetECParent() (Entity, bool)
- func (entity *EntityBehavior) GetId() uid.Id
- func (entity *EntityBehavior) GetMeta() Meta
- func (entity *EntityBehavior) GetPrototype() string
- func (entity *EntityBehavior) GetScope() Scope
- func (entity *EntityBehavior) GetState() EntityState
- func (entity *EntityBehavior) ManagedHooks(hooks ...event.Hook)
- func (entity *EntityBehavior) RangeComponents(fun generic.Func1[Component, bool])
- func (entity *EntityBehavior) RemoveComponent(name string)
- func (entity *EntityBehavior) RemoveComponentById(id uid.Id)
- func (entity *EntityBehavior) ReverseRangeComponents(fun generic.Func1[Component, bool])
- func (entity *EntityBehavior) String() string
- type EntityOptions
- type EntityState
- type EventCompMgrAddComponents
- type EventCompMgrAddComponentsHandler
- type EventCompMgrFirstAccessComponent
- type EventCompMgrFirstAccessComponentHandler
- type EventCompMgrRemoveComponent
- type EventCompMgrRemoveComponentHandler
- type EventComponentDestroySelf
- type EventComponentDestroySelfHandler
- type EventEntityDestroySelf
- type EventEntityDestroySelfHandler
- type Meta
- type Scope
Constants ¶
This section is empty.
Variables ¶
var (
ErrEC = fmt.Errorf("%w: ec", exception.ErrCore) // EC错误
)
var With _Option
Functions ¶
func BindEventCompMgrAddComponents ¶
func BindEventCompMgrAddComponents(auto _AutoEventCompMgrAddComponents, subscriber EventCompMgrAddComponents, priority ...int32) event.Hook
func BindEventCompMgrFirstAccessComponent ¶
func BindEventCompMgrFirstAccessComponent(auto _AutoEventCompMgrFirstAccessComponent, subscriber EventCompMgrFirstAccessComponent, priority ...int32) event.Hook
func BindEventCompMgrRemoveComponent ¶
func BindEventCompMgrRemoveComponent(auto _AutoEventCompMgrRemoveComponent, subscriber EventCompMgrRemoveComponent, priority ...int32) event.Hook
func BindEventComponentDestroySelf ¶
func BindEventComponentDestroySelf(auto _AutoEventComponentDestroySelf, subscriber EventComponentDestroySelf, priority ...int32) event.Hook
func BindEventEntityDestroySelf ¶
func BindEventEntityDestroySelf(auto _AutoEventEntityDestroySelf, subscriber EventEntityDestroySelf, 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 // 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) 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 // GetId 获取实体Id GetId() uid.Id // GetPrototype 获取实体原型 GetPrototype() string }
ConcurrentEntity 多线程安全的实体接口
type ECNodeState ¶
type ECNodeState int8
ECNodeState EC节点状态
const ( ECNodeState_Detached ECNodeState = iota // 已从EC树中脱离 ECNodeState_Attached // 在EC树中 ECNodeState_Detaching // 正在脱离 )
func (ECNodeState) String ¶
func (i ECNodeState) String() string
type Entity ¶
type Entity interface { concurrent.CurrentContextProvider reinterpret.CompositeProvider fmt.Stringer // GetId 获取实体Id GetId() uid.Id // GetPrototype 获取实体原型 GetPrototype() string // GetScope 获取可访问作用域 GetScope() Scope // GetECNodeState 获取EC节点状态 GetECNodeState() ECNodeState // GetECParent 获取在EC树中的父实体 GetECParent() (Entity, bool) // GetState 获取实体状态 GetState() EntityState // GetMeta 获取Meta信息 GetMeta() Meta // DestroySelf 销毁自身 DestroySelf() // contains filtered or unexported methods }
Entity 实体接口
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, component Component) error
AddComponent 添加单个组件,因为同个名称可以指向多个组件,所以名称指向的组件已存在时,不会返回错误
func (*EntityBehavior) AddComponents ¶
func (entity *EntityBehavior) AddComponents(name string, components []Component) error
AddComponents 使用同个名称添加多个组件,一般情况下名称指组件接口名称,也可以自定义名称
func (*EntityBehavior) CountComponents ¶
func (entity *EntityBehavior) CountComponents() int
CountComponents 统计所有组件数量
func (*EntityBehavior) EventCompMgrAddComponents ¶
func (entity *EntityBehavior) EventCompMgrAddComponents() event.IEvent
EventCompMgrAddComponents 事件:实体的组件管理器加入一些组件
func (*EntityBehavior) EventCompMgrFirstAccessComponent ¶
func (entity *EntityBehavior) EventCompMgrFirstAccessComponent() event.IEvent
EventCompMgrFirstAccessComponent 事件:实体的组件管理器首次访问组件
func (*EntityBehavior) EventCompMgrRemoveComponent ¶
func (entity *EntityBehavior) EventCompMgrRemoveComponent() event.IEvent
EventCompMgrRemoveComponent 事件:实体的组件管理器删除组件
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(name string) []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) GetECNodeState ¶
func (entity *EntityBehavior) GetECNodeState() ECNodeState
GetECNodeState 获取EC节点状态
func (*EntityBehavior) GetECParent ¶
func (entity *EntityBehavior) GetECParent() (Entity, bool)
GetECParent 获取在EC树中的父实体
func (*EntityBehavior) GetPrototype ¶
func (entity *EntityBehavior) GetPrototype() string
GetPrototype 获取实体原型
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) 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) ReverseRangeComponents ¶
func (entity *EntityBehavior) ReverseRangeComponents(fun generic.Func1[Component, bool])
ReverseRangeComponents 反向遍历所有组件
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信息 FaceAnyAllocator container.Allocator[iface.FaceAny] // 自定义FaceAny内存分配器,用于提高性能,通常传入运行时上下文中的FaceAnyAllocator HookAllocator container.Allocator[event.Hook] // 自定义Hook内存分配器,用于提高性能,通常传入运行时上下文中的HookAllocator GCCollector container.GCCollector // 自定义GC收集器,通常不传或者传入运行时上下文 }
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 EventCompMgrAddComponents ¶
type EventCompMgrAddComponents interface {
OnCompMgrAddComponents(entity Entity, components []Component)
}
EventCompMgrAddComponents [EmitUnExport] 事件:实体的组件管理器加入一些组件
type EventCompMgrAddComponentsHandler ¶ added in v0.2.51
func HandleEventCompMgrAddComponents ¶ added in v0.2.50
func HandleEventCompMgrAddComponents(fun func(entity Entity, components []Component)) EventCompMgrAddComponentsHandler
func (EventCompMgrAddComponentsHandler) OnCompMgrAddComponents ¶ added in v0.2.51
func (h EventCompMgrAddComponentsHandler) OnCompMgrAddComponents(entity Entity, components []Component)
type EventCompMgrFirstAccessComponent ¶
type EventCompMgrFirstAccessComponent interface {
OnCompMgrFirstAccessComponent(entity Entity, component Component)
}
EventCompMgrFirstAccessComponent [EmitUnExport] 事件:实体的组件管理器首次访问组件
type EventCompMgrFirstAccessComponentHandler ¶ added in v0.2.51
func HandleEventCompMgrFirstAccessComponent ¶ added in v0.2.50
func HandleEventCompMgrFirstAccessComponent(fun func(entity Entity, component Component)) EventCompMgrFirstAccessComponentHandler
func (EventCompMgrFirstAccessComponentHandler) OnCompMgrFirstAccessComponent ¶ added in v0.2.51
func (h EventCompMgrFirstAccessComponentHandler) OnCompMgrFirstAccessComponent(entity Entity, component Component)
type EventCompMgrRemoveComponent ¶
type EventCompMgrRemoveComponent interface {
OnCompMgrRemoveComponent(entity Entity, component Component)
}
EventCompMgrRemoveComponent [EmitUnExport] 事件:实体的组件管理器删除组件
type EventCompMgrRemoveComponentHandler ¶ added in v0.2.51
func HandleEventCompMgrRemoveComponent ¶ added in v0.2.50
func HandleEventCompMgrRemoveComponent(fun func(entity Entity, component Component)) EventCompMgrRemoveComponentHandler
func (EventCompMgrRemoveComponentHandler) OnCompMgrRemoveComponent ¶ added in v0.2.51
func (h EventCompMgrRemoveComponentHandler) OnCompMgrRemoveComponent(entity Entity, component Component)
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 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)
Source Files ¶
- component.go
- component_event.go
- component_event_code.go
- component_hooks.go
- componentstate.go
- componentstate_string.go
- concurrententity.go
- doc.go
- ecnodestate.go
- ecnodestate_string.go
- entity.go
- entity_componentmgr.go
- entity_componentmgr_event.go
- entity_componentmgr_event_code.go
- entity_event.go
- entity_event_code.go
- entity_hooks.go
- entity_options.go
- entitystate.go
- entitystate_string.go
- errors.go
- meta.go
- scope.go
- scope_string.go
- unsafe_component.go
- unsafe_entity.go