我正在尝试在 go 中实现一个行为树,并且我正在努力解决它的组合功能。基本上,我需要Tick()
在下面实现来调用由它嵌入的位置定义的方法。
这里是behavior.go
:
type IBehavior interface {
Tick() Status
Update() Status
}
type Behavior struct {
Status Status
}
func (n *Behavior) Tick() Status {
fmt.Println("ticking!")
if n.Status != RUNNING { n.Initialize() }
status := n.Update()
if n.Status != RUNNING { n.Terminate(status) }
return status
}
func (n *Behavior) Update() Status {
fmt.Println("This update is being called")
return n.Status
}
这是Behavior
嵌入的结构:
type IBehaviorTree interface {
IBehavior
}
type BehaviorTree struct {
Behavior
Root IBehavior
}
func (n *BehaviorTree) Update() Status {
fmt.Printf("Tree tick! %#v\n", n.Root)
return n.Root.Tick()
}
让这个例子有意义的更多文件:
type ILeaf interface {
IBehavior
}
type Leaf struct {
Behavior
}
和这个:
type Test struct {
Leaf
Status Status
}
func NewTest() *Test {
return &Test{}
}
func (n Test) Update() Status {
fmt.Println("Testing!")
return SUCCESS
}
这是它的用法示例:
tree := ai.NewBehaviorTree()
test := ai.NewTest()
tree.Root = test
tree.Tick()
我期待树通过打印这个来正常滴答作响:
ticking!
Tree tick!
但相反,我得到:
ticking!
This update is being called
谁能帮我解决这个问题?
编辑:添加了一些额外的文件来说明问题。另外,我不明白反对票。我有一个诚实的问题。我只应该问对我有意义的问题吗?