Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Jan 10, 2025
1 parent ce360ee commit 05e4c36
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions addins/goscr/ec_comp.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,27 @@ func ComponentScriptT[T any](script string) pt.ComponentAttribute {

return pt.Component(types.ZeroT[T]()).SetName(scriptIdent).SetExtra(map[string]any{"script_pkg": scriptPkg, "script_ident": scriptIdent})
}

// GetComponentScript 获取组件脚本
func GetComponentScript(entity ec.Entity, name string) func() *ComponentBehavior {
return GetComponentScriptT[*ComponentBehavior](entity, name)
}

// GetComponentScriptT 获取组件脚本
func GetComponentScriptT[T interface{ This() func() T }](entity ec.Entity, name string) func() T {
if entity == nil {
panic(fmt.Errorf("%s: entity is nil", exception.ErrArgs))
}

comp := entity.GetComponent(name)
if comp == nil {
return nil
}

behavior, ok := comp.(T)
if !ok {
return nil
}

return behavior.This()
}
19 changes: 19 additions & 0 deletions addins/goscr/ec_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,22 @@ func EntityScriptT[T any](prototype, script string) pt.EntityAttribute {

return pt.Entity(prototype).SetExtra(map[string]any{"script_pkg": scriptPkg, "script_ident": scriptIdent})
}

// GetEntityScript 获取实体脚本
func GetEntityScript(entity ec.Entity) func() *EntityBehavior {
return GetEntityScriptT[*EntityBehavior](entity)
}

// GetEntityScriptT 获取实体脚本
func GetEntityScriptT[T interface{ This() func() T }](entity ec.Entity) func() T {
if entity == nil {
panic(fmt.Errorf("%s: entity is nil", exception.ErrArgs))
}

behavior, ok := entity.(T)
if !ok {
return nil
}

return behavior.This()
}

0 comments on commit 05e4c36

Please sign in to comment.