31 lines
827 B
GDScript
31 lines
827 B
GDScript
extends Node
|
|
class_name BehaviorTree
|
|
|
|
func tick(actor, blackboard, debug = false) -> int:
|
|
var tick := Tick.new()
|
|
tick.tree = self
|
|
tick.actor = actor
|
|
tick.blackboard = blackboard
|
|
tick.debug = debug
|
|
|
|
var result := FAILED
|
|
|
|
for child in get_children():
|
|
var _result = child._execute(tick)
|
|
assert(typeof(_result) == TYPE_INT)
|
|
result = _result
|
|
|
|
# Close nodes from last tick, if needed
|
|
var last_open_nodes: Array = tick.blackboard.get('openNodes', self)
|
|
var current_open_nodes := tick.open_nodes
|
|
|
|
# If node isn't currently open, but was open during last tick, close it
|
|
for node in last_open_nodes:
|
|
if (not current_open_nodes.has(node)
|
|
and tick.blackboard.get('isOpen', tick.tree, node)):
|
|
node._close(tick)
|
|
|
|
# Populate the blackboard
|
|
blackboard.set('openNodes', current_open_nodes, self)
|
|
return result
|