Skip to content

Commit

Permalink
整理原型代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Nov 15, 2024
1 parent 3d5e081 commit d6b0c82
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
4 changes: 2 additions & 2 deletions entityptcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func CreateEntityPT(svcCtx service.Context, prototype string) EntityPTCreator {
// EntityPTCreator 实体原型构建器
type EntityPTCreator struct {
svcCtx service.Context
atti pt.EntityAtti
atti pt.EntityAttribute
comps []any
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func (c EntityPTCreator) ComponentUniqueID(b bool) EntityPTCreator {
// AddComponent 添加组件
func (c EntityPTCreator) AddComponent(comp any, name ...string) EntityPTCreator {
switch v := comp.(type) {
case pt.ComponentAtti, *pt.ComponentAtti:
case pt.ComponentAttribute, *pt.ComponentAttribute:
c.comps = append(c.comps, v)
default:
c.comps = append(c.comps, pt.ComponentWith(comp, pie.First(name), true))
Expand Down
14 changes: 7 additions & 7 deletions pt/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ComponentDesc struct {
PT ComponentPT // 组件原型
Name string // 组件名称
NonRemovable bool // 不可删除
CustomAtti CustomAtti // 自定义原型属性
Extra Extra // 自定义原型属性
}

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

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

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

// Component 获取组件
Expand Down
18 changes: 9 additions & 9 deletions pt/entitylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {
lib.Lock()
defer lib.Unlock()

var entityAtti EntityAtti
var entityAtti EntityAttribute

switch v := prototype.(type) {
case EntityAtti:
case EntityAttribute:
entityAtti = v
case *EntityAtti:
case *EntityAttribute:
entityAtti = *v
case string:
entityAtti = EntityAtti{Prototype: v}
entityAtti = EntityAttribute{Prototype: v}
default:
exception.Panicf("%w: invalid prototype type: %T", ErrPt, prototype)
}
Expand All @@ -181,7 +181,7 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {
scope: entityAtti.Scope,
componentAwakeOnFirstTouch: entityAtti.ComponentAwakeOnFirstTouch,
componentUniqueID: entityAtti.ComponentUniqueID,
customAtti: entityAtti.CustomAtti,
extra: entityAtti.Extra,
}

if entityAtti.Instance != nil {
Expand Down Expand Up @@ -210,16 +210,16 @@ func (lib *_EntityLib) declare(re bool, prototype any, comps ...any) EntityPT {

retry:
switch v := comp.(type) {
case ComponentAtti:
case ComponentAttribute:
compDesc.Name = v.Name
compDesc.NonRemovable = v.NonRemovable
compDesc.CustomAtti = v.CustomAtti
compDesc.Extra = v.Extra
comp = v.Instance
goto retry
case *ComponentAtti:
case *ComponentAttribute:
compDesc.Name = v.Name
compDesc.NonRemovable = v.NonRemovable
compDesc.CustomAtti = v.CustomAtti
compDesc.Extra = v.Extra
comp = v.Instance
goto retry
case string:
Expand Down
45 changes: 23 additions & 22 deletions pt/entitylib_declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,48 @@ package pt
import (
"git.golaxy.org/core/ec"
"git.golaxy.org/core/utils/generic"
"github.com/elliotchance/pie/v2"
)

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

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

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

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

// ComponentWith 创建组件原型属性,用于注册实体原型时自定义相关属性
func ComponentWith(inst any, name string, nonRemovable bool, customAtti ...generic.KV[string, any]) ComponentAtti {
return ComponentAtti{
func ComponentWith(inst any, name string, nonRemovable bool, extra ...map[string]any) ComponentAttribute {
return ComponentAttribute{
Instance: inst,
Name: name,
NonRemovable: nonRemovable,
CustomAtti: generic.MakeSliceMap(customAtti...),
Extra: generic.MakeSliceMapFromGoMap(pie.First(extra)),
}
}

0 comments on commit d6b0c82

Please sign in to comment.