-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_precondition.go
62 lines (52 loc) · 1.39 KB
/
script_precondition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package decorators
import "github.com/gorustyt/go-behavior/core"
func init() {
core.SetPorts(&PreconditionNode{}, core.InputPortWithDefaultValue("if", ""))
core.SetPorts(&PreconditionNode{}, core.InputPortWithDefaultValue("else", core.NodeStatus_FAILURE, "Return status if condition is false"))
}
type PreconditionNode struct {
*core.DecoratorNode
_script string
_executor core.ScriptFunction
}
func NewPreconditionNode(name string, cfg *core.NodeConfig, args ...interface{}) core.ITreeNode {
n := &PreconditionNode{
DecoratorNode: core.NewDecoratorNode(name, cfg),
}
n.loadExecutor()
return n
}
func (n *PreconditionNode) Tick() core.NodeStatus {
n.loadExecutor()
var else_return core.NodeStatus
if v, err := n.GetInput("else", 0); err != nil {
panic("Missing parameter [else] in Precondition")
} else {
else_return = core.NodeStatus(v.(int))
}
if n._executor(n.Config().Blackboard, n.Config().Enums) {
child_status := n.Child().ExecuteTick()
if core.IsStatusCompleted(child_status) {
n.ResetChild()
}
return child_status
} else {
return else_return
}
}
func (n *PreconditionNode) loadExecutor() {
var script string
if v, err := n.GetInput("if", script); err != nil {
panic("Missing parameter [if] in Precondition")
} else {
script = v.(string)
}
if script == n._script {
return
}
if n._executor == nil {
panic("not impl")
} else {
n._script = script
}
}