Skip to content

Commit

Permalink
整理原型定义代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Nov 14, 2024
1 parent 2275cda commit cd311a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
8 changes: 0 additions & 8 deletions define/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ func Component[COMP any](name ...string) ComponentDefinition {
}
}

// ComponentInterface 定义有接口的组件
func ComponentInterface[COMP, COMP_IFACE any]() ComponentDefinition {
return ComponentDefinition{
Prototype: pt.DefaultComponentLib().Declare(types.ZeroT[COMP]()).Prototype(),
Name: types.FullNameT[COMP_IFACE](),
}
}

// ComponentDefinition 组件定义
type ComponentDefinition struct {
Prototype string // 组件原型名称
Expand Down
9 changes: 9 additions & 0 deletions pt/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ComponentDesc struct {
PT ComponentPT // 组件原型
Name string // 组件名称
NonRemovable bool // 不可删除
CustomAtti CustomAtti // 自定义原型属性
}

// EntityPT 实体原型接口
Expand All @@ -47,6 +48,8 @@ type EntityPT interface {
ComponentAwakeOnFirstTouch() *bool
// ComponentUniqueID 开启组件唯一Id
ComponentUniqueID() *bool
// CustomAtti 自定义原型属性
CustomAtti() CustomAtti
// CountComponents // 组件数量
CountComponents() int
// Component 获取组件
Expand All @@ -63,6 +66,7 @@ type _EntityPT struct {
scope *ec.Scope
componentAwakeOnFirstTouch *bool
componentUniqueID *bool
customAtti CustomAtti
components []ComponentDesc
}

Expand Down Expand Up @@ -96,6 +100,11 @@ func (pt *_EntityPT) CountComponents() int {
return len(pt.components)
}

// CustomAtti 自定义原型属性
func (pt *_EntityPT) CustomAtti() CustomAtti {
return pt.customAtti
}

// Component 获取组件
func (pt *_EntityPT) Component(idx int) ComponentDesc {
if idx < 0 || idx >= len(pt.components) {
Expand Down
3 changes: 3 additions & 0 deletions pt/entitylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {
scope: entityAtti.Scope,
componentAwakeOnFirstTouch: entityAtti.ComponentAwakeOnFirstTouch,
componentUniqueID: entityAtti.ComponentUniqueID,
customAtti: entityAtti.CustomAtti,
}

if entityAtti.Instance != nil {
Expand Down Expand Up @@ -205,11 +206,13 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {
case CompAtti:
compDesc.Name = v.Name
compDesc.NonRemovable = v.NonRemovable
compDesc.CustomAtti = v.CustomAtti
comp = v.Instance
goto retry
case *CompAtti:
compDesc.Name = v.Name
compDesc.NonRemovable = v.NonRemovable
compDesc.CustomAtti = v.CustomAtti
comp = v.Instance
goto retry
case string:
Expand Down
38 changes: 18 additions & 20 deletions pt/entitylib_declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,48 @@ package pt

import (
"git.golaxy.org/core/ec"
"git.golaxy.org/core/utils/types"
"git.golaxy.org/core/utils/generic"
)

// CustomAtti 自定义原型属性
type CustomAtti = generic.SliceMap[string, any]

// EntityAtti 实体原型属性
type EntityAtti struct {
Prototype string // 实体原型名称(必填)
Instance any // 实体实例
Scope *ec.Scope // 可访问作用域
ComponentAwakeOnFirstTouch *bool // 开启组件被首次访问时,检测并调用Awake()
ComponentUniqueID *bool // 开启组件唯一Id
Prototype string // 实体原型名称(必填)
Instance any // 实体实例
Scope *ec.Scope // 可访问作用域
ComponentAwakeOnFirstTouch *bool // 开启组件被首次访问时,检测并调用Awake()
ComponentUniqueID *bool // 开启组件唯一Id
CustomAtti CustomAtti // 自定义原型属性
}

// EntityWith 创建实体原型属性,用于注册实体原型时自定义相关属性
func EntityWith(prototype string, inst any, scope *ec.Scope, componentAwakeOnFirstTouch, componentUniqueID *bool) EntityAtti {
func EntityWith(prototype string, inst any, scope *ec.Scope, componentAwakeOnFirstTouch, componentUniqueID *bool, customAtti ...generic.KV[string, any]) EntityAtti {
return EntityAtti{
Prototype: prototype,
Instance: inst,
Scope: scope,
ComponentAwakeOnFirstTouch: componentAwakeOnFirstTouch,
ComponentUniqueID: componentUniqueID,
CustomAtti: generic.MakeSliceMap(customAtti...),
}
}

// CompAtti 组件原型属性
type CompAtti struct {
Instance any // 组件实例(必填)
Name string // 组件名称
NonRemovable bool // 是否不可删除
Instance any // 组件实例(必填)
Name string // 组件名称
NonRemovable bool // 是否不可删除
CustomAtti CustomAtti // 自定义原型属性
}

// CompWith 创建组件原型属性,用于注册实体原型时自定义相关属性
func CompWith(inst any, name string, nonRemovable bool) CompAtti {
func CompWith(inst any, name string, nonRemovable bool, customAtti ...generic.KV[string, any]) CompAtti {
return CompAtti{
Instance: inst,
Name: name,
NonRemovable: nonRemovable,
}
}

// CompInterfaceWith 创建组件原型属性,用于注册实体原型时使用接口名作为组件名称,并自定义相关属性
func CompInterfaceWith[FACE any](inst any, nonRemovable bool) CompAtti {
return CompAtti{
Instance: inst,
Name: types.FullNameT[FACE](),
NonRemovable: nonRemovable,
CustomAtti: generic.MakeSliceMap(customAtti...),
}
}

0 comments on commit cd311a7

Please sign in to comment.