Skip to content

Commit

Permalink
修改组件id为可选特性
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Nov 5, 2024
1 parent 8f5f25e commit 75454fc
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 141 deletions.
40 changes: 20 additions & 20 deletions ec/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,24 @@ type iEntity interface {
// EntityBehavior 实体行为,在扩展实体能力时,匿名嵌入至实体结构体中
type EntityBehavior struct {
context.Context
terminate context.CancelFunc
terminated chan struct{}
opts EntityOptions
context iface.Cache
components generic.List[Component]
state EntityState
reflected reflect.Value
treeNodeState TreeNodeState
treeNodeParent Entity
eventEntityDestroySelf event.Event
eventComponentMgrAddComponents event.Event
eventComponentMgrRemoveComponent event.Event
eventComponentMgrFirstAccessComponent event.Event
eventTreeNodeAddChild event.Event
eventTreeNodeRemoveChild event.Event
eventTreeNodeEnterParent event.Event
eventTreeNodeLeaveParent event.Event
managedHooks []event.Hook
terminate context.CancelFunc
terminated chan struct{}
opts EntityOptions
context iface.Cache
components generic.List[Component]
state EntityState
reflected reflect.Value
treeNodeState TreeNodeState
treeNodeParent Entity
eventEntityDestroySelf event.Event
eventComponentMgrAddComponents event.Event
eventComponentMgrRemoveComponent event.Event
eventComponentMgrFirstTouchComponent event.Event
eventTreeNodeAddChild event.Event
eventTreeNodeRemoveChild event.Event
eventTreeNodeEnterParent event.Event
eventTreeNodeLeaveParent event.Event
managedHooks []event.Hook
}

// GetId 获取实体Id
Expand Down Expand Up @@ -192,7 +192,7 @@ func (entity *EntityBehavior) init(opts EntityOptions) {
entity.eventEntityDestroySelf.Init(false, nil, event.EventRecursion_Discard)
entity.eventComponentMgrAddComponents.Init(false, nil, event.EventRecursion_Allow)
entity.eventComponentMgrRemoveComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventComponentMgrFirstAccessComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventComponentMgrFirstTouchComponent.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeAddChild.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeRemoveChild.Init(false, nil, event.EventRecursion_Allow)
entity.eventTreeNodeEnterParent.Init(false, nil, event.EventRecursion_Allow)
Expand Down Expand Up @@ -228,7 +228,7 @@ func (entity *EntityBehavior) setState(state EntityState) {
entity.eventEntityDestroySelf.Close()
entity.eventComponentMgrAddComponents.Close()
entity.eventComponentMgrRemoveComponent.Close()
entity.eventComponentMgrFirstAccessComponent.Close()
entity.eventComponentMgrFirstTouchComponent.Close()
case EntityState_Shut:
entity.eventTreeNodeAddChild.Close()
entity.eventTreeNodeRemoveChild.Close()
Expand Down
30 changes: 15 additions & 15 deletions ec/entity_componentmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ type iComponentMgr interface {
// RemoveComponentById 使用组件Id删除组件
RemoveComponentById(id uid.Id)

iAutoEventComponentMgrAddComponents // 事件:实体的组件管理器添加组件
iAutoEventComponentMgrRemoveComponent // 事件:实体的组件管理器删除组件
iAutoEventComponentMgrFirstAccessComponent // 事件:实体的组件管理器首次访问组件
iAutoEventComponentMgrAddComponents // 事件:实体的组件管理器添加组件
iAutoEventComponentMgrRemoveComponent // 事件:实体的组件管理器删除组件
iAutoEventComponentMgrFirstTouchComponent // 事件:实体的组件管理器首次访问组件
}

// GetComponent 使用名称查询组件,组件同名时,返回首个组件
func (entity *EntityBehavior) GetComponent(name string) Component {
if node, ok := entity.getComponentNode(name); ok {
return entity.accessComponent(node.V)
return entity.touchComponent(node.V)
}
return nil
}

// GetComponentById 使用组件Id查询组件
func (entity *EntityBehavior) GetComponentById(id uid.Id) Component {
if node, ok := entity.getComponentNodeById(id); ok {
return entity.accessComponent(node.V)
return entity.touchComponent(node.V)
}
return nil
}
Expand All @@ -91,7 +91,7 @@ func (entity *EntityBehavior) ContainsComponentById(id uid.Id) bool {
// RangeComponents 遍历所有组件
func (entity *EntityBehavior) RangeComponents(fun generic.Func1[Component, bool]) {
entity.components.Traversal(func(node *generic.Node[Component]) bool {
comp := entity.accessComponent(node.V)
comp := entity.touchComponent(node.V)
if comp == nil {
return true
}
Expand All @@ -102,7 +102,7 @@ func (entity *EntityBehavior) RangeComponents(fun generic.Func1[Component, bool]
// ReversedRangeComponents 反向遍历所有组件
func (entity *EntityBehavior) ReversedRangeComponents(fun generic.Func1[Component, bool]) {
entity.components.ReversedTraversal(func(node *generic.Node[Component]) bool {
comp := entity.accessComponent(node.V)
comp := entity.touchComponent(node.V)
if comp == nil {
return true
}
Expand All @@ -123,7 +123,7 @@ func (entity *EntityBehavior) FilterComponents(fun generic.Func1[Component, bool
})

for i := range components {
if entity.accessComponent(components[i]) == nil {
if entity.touchComponent(components[i]) == nil {
components[i] = nil
}
}
Expand All @@ -145,7 +145,7 @@ func (entity *EntityBehavior) GetComponents() []Component {
})

for i := range components {
if entity.accessComponent(components[i]) == nil {
if entity.touchComponent(components[i]) == nil {
components[i] = nil
}
}
Expand Down Expand Up @@ -251,9 +251,9 @@ func (entity *EntityBehavior) EventComponentMgrRemoveComponent() event.IEvent {
return &entity.eventComponentMgrRemoveComponent
}

// EventComponentMgrFirstAccessComponent 事件:实体的组件管理器首次访问组件
func (entity *EntityBehavior) EventComponentMgrFirstAccessComponent() event.IEvent {
return &entity.eventComponentMgrFirstAccessComponent
// EventComponentMgrFirstTouchComponent 事件:实体的组件管理器首次访问组件
func (entity *EntityBehavior) EventComponentMgrFirstTouchComponent() event.IEvent {
return &entity.eventComponentMgrFirstTouchComponent
}

func (entity *EntityBehavior) addComponent(name string, component Component) {
Expand Down Expand Up @@ -305,11 +305,11 @@ func (entity *EntityBehavior) getComponentNodeById(id uid.Id) (*generic.Node[Com
return compNode, compNode != nil
}

func (entity *EntityBehavior) accessComponent(comp Component) Component {
if entity.opts.AwakeOnFirstAccess && comp.GetState() == ComponentState_Attach {
func (entity *EntityBehavior) touchComponent(comp Component) Component {
if entity.opts.ComponentAwakeOnFirstTouch && comp.GetState() == ComponentState_Attach {
switch entity.GetState() {
case EntityState_Awake, EntityState_Start, EntityState_Alive:
_EmitEventComponentMgrFirstAccessComponent(entity, entity.opts.InstanceFace.Iface, comp)
_EmitEventComponentMgrFirstTouchComponent(entity, entity.opts.InstanceFace.Iface, comp)
}
}

Expand Down
28 changes: 14 additions & 14 deletions ec/entity_componentmgr_event.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ec/entity_componentmgr_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type EventComponentMgrRemoveComponent interface {
OnComponentMgrRemoveComponent(entity Entity, component Component)
}

// EventComponentMgrFirstAccessComponent 事件:实体的组件管理器首次访问组件
// EventComponentMgrFirstTouchComponent 事件:实体的组件管理器首次访问组件
// +event-gen:export=0
type EventComponentMgrFirstAccessComponent interface {
OnComponentMgrFirstAccessComponent(entity Entity, component Component)
type EventComponentMgrFirstTouchComponent interface {
OnComponentMgrFirstTouchComponent(entity Entity, component Component)
}
29 changes: 19 additions & 10 deletions ec/entity_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (

// EntityOptions 创建实体的所有选项
type EntityOptions struct {
InstanceFace iface.Face[Entity] // 实例,用于扩展实体能力
Prototype string // 实体原型名称
Scope Scope // 可访问作用域
PersistId uid.Id // 实体持久化Id
AwakeOnFirstAccess bool // 开启组件被首次访问时,检测并调用Awake()
Meta meta.Meta // Meta信息
InstanceFace iface.Face[Entity] // 实例,用于扩展实体能力
Prototype string // 实体原型名称
Scope Scope // 可访问作用域
PersistId uid.Id // 实体持久化Id
ComponentAwakeOnFirstTouch bool // 开启组件被首次访问时,检测并调用Awake()
ComponentUniqueID bool // 开启组件唯一Id
Meta meta.Meta // Meta信息
}

var With _Option
Expand All @@ -47,7 +48,8 @@ func (_Option) Default() option.Setting[EntityOptions] {
With.Prototype("")(o)
With.Scope(Scope_Global)(o)
With.PersistId(uid.Nil)(o)
With.AwakeOnFirstAccess(false)(o)
With.ComponentAwakeOnFirstTouch(false)(o)
With.ComponentUniqueID(false)(o)
With.Meta(nil)(o)
}
}
Expand Down Expand Up @@ -80,10 +82,17 @@ func (_Option) PersistId(id uid.Id) option.Setting[EntityOptions] {
}
}

// AwakeOnFirstAccess 开启组件被首次访问时,检测并调用Awake()
func (_Option) AwakeOnFirstAccess(b bool) option.Setting[EntityOptions] {
// ComponentAwakeOnFirstTouch 开启组件被首次访问时,检测并调用Awake()
func (_Option) ComponentAwakeOnFirstTouch(b bool) option.Setting[EntityOptions] {
return func(o *EntityOptions) {
o.AwakeOnFirstAccess = b
o.ComponentAwakeOnFirstTouch = b
}
}

// ComponentUniqueID 开启组件唯一Id
func (_Option) ComponentUniqueID(b bool) option.Setting[EntityOptions] {
return func(o *EntityOptions) {
o.ComponentUniqueID = b
}
}

Expand Down
12 changes: 9 additions & 3 deletions entitycreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ func (c EntityCreator) PersistId(id uid.Id) EntityCreator {
return c
}

// AwakeOnFirstAccess 设置开启组件被首次访问时,检测并调用Awake()
func (c EntityCreator) AwakeOnFirstAccess(b bool) EntityCreator {
c.settings = append(c.settings, ec.With.AwakeOnFirstAccess(b))
// ComponentAwakeOnFirstTouch 开启组件被首次访问时,检测并调用Awake()
func (c EntityCreator) ComponentAwakeOnFirstTouch(b bool) EntityCreator {
c.settings = append(c.settings, ec.With.ComponentAwakeOnFirstTouch(b))
return c
}

// ComponentUniqueID 开启组件唯一Id
func (c EntityCreator) ComponentUniqueID(b bool) EntityCreator {
c.settings = append(c.settings, ec.With.ComponentUniqueID(b))
return c
}

Expand Down
12 changes: 9 additions & 3 deletions entityptcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ func (c EntityPTCreator) Scope(scope ec.Scope) EntityPTCreator {
return c
}

// AwakeOnFirstAccess 设置开启组件被首次访问时,检测并调用Awake()
func (c EntityPTCreator) AwakeOnFirstAccess(b bool) EntityPTCreator {
c.atti.AwakeOnFirstAccess = &b
// ComponentAwakeOnFirstTouch 开启组件被首次访问时,检测并调用Awake()
func (c EntityPTCreator) ComponentAwakeOnFirstTouch(b bool) EntityPTCreator {
c.atti.ComponentAwakeOnFirstTouch = &b
return c
}

// ComponentUniqueID 开启组件唯一Id
func (c EntityPTCreator) ComponentUniqueID(b bool) EntityPTCreator {
c.atti.ComponentUniqueID = &b
return c
}

Expand Down
35 changes: 23 additions & 12 deletions pt/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type EntityPT interface {
InstanceRT() reflect.Type
// Scope 可访问作用域
Scope() *ec.Scope
// AwakeOnFirstAccess 设置开启组件被首次访问时,检测并调用Awake()
AwakeOnFirstAccess() *bool
// ComponentAwakeOnFirstTouch 开启组件被首次访问时,检测并调用Awake()
ComponentAwakeOnFirstTouch() *bool
// ComponentUniqueID 开启组件唯一Id
ComponentUniqueID() *bool
// CountComponents // 组件数量
CountComponents() int
// Component 获取组件
Expand All @@ -56,11 +58,12 @@ type EntityPT interface {
}

type _EntityPT struct {
prototype string
instanceRT reflect.Type
scope *ec.Scope
awakeOnFirstAccess *bool
components []ComponentDesc
prototype string
instanceRT reflect.Type
scope *ec.Scope
componentAwakeOnFirstTouch *bool
componentUniqueID *bool
components []ComponentDesc
}

// Prototype 实体原型名称
Expand All @@ -78,9 +81,14 @@ func (pt *_EntityPT) Scope() *ec.Scope {
return pt.scope
}

// AwakeOnFirstAccess 设置开启组件被首次访问时,检测并调用Awake()
func (pt *_EntityPT) AwakeOnFirstAccess() *bool {
return pt.awakeOnFirstAccess
// ComponentAwakeOnFirstTouch 开启组件被首次访问时,检测并调用Awake()
func (pt *_EntityPT) ComponentAwakeOnFirstTouch() *bool {
return pt.componentAwakeOnFirstTouch
}

// ComponentUniqueID 开启组件唯一Id
func (pt *_EntityPT) ComponentUniqueID() *bool {
return pt.componentUniqueID
}

// CountComponents // 组件数量
Expand Down Expand Up @@ -110,8 +118,11 @@ func (pt *_EntityPT) Construct(settings ...option.Setting[ec.EntityOptions]) ec.
if pt.scope != nil {
options.Scope = *pt.scope
}
if pt.awakeOnFirstAccess != nil {
options.AwakeOnFirstAccess = *pt.awakeOnFirstAccess
if pt.componentAwakeOnFirstTouch != nil {
options.ComponentAwakeOnFirstTouch = *pt.componentAwakeOnFirstTouch
}
if pt.componentUniqueID != nil {
options.ComponentUniqueID = *pt.componentUniqueID
}
options = option.Append(options, settings...)
options.Prototype = pt.prototype
Expand Down
7 changes: 4 additions & 3 deletions pt/entitylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {
}

entityPT := &_EntityPT{
prototype: entityAtti.Prototype,
scope: entityAtti.Scope,
awakeOnFirstAccess: entityAtti.AwakeOnFirstAccess,
prototype: entityAtti.Prototype,
scope: entityAtti.Scope,
componentAwakeOnFirstTouch: entityAtti.ComponentAwakeOnFirstTouch,
componentUniqueID: entityAtti.ComponentUniqueID,
}

if entityAtti.Instance != nil {
Expand Down
Loading

0 comments on commit 75454fc

Please sign in to comment.