class BehaviorTree::ControlNodeBase

A node that has children (abstract class).

Public Class Methods

new(children = []) click to toggle source
Calls superclass method
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 11
def initialize(children = [])
  raise IncorrectTraversalStrategyError, nil.class if traversal_strategy.nil?
  raise IncorrectTraversalStrategyError, traversal_strategy unless respond_to?(traversal_strategy, true)

  super()
  @children = children
end

Private Class Methods

children_traversal_strategy(traversal_strategy) click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 84
def children_traversal_strategy(traversal_strategy)
  @traversal_strategy = traversal_strategy
end
traversal_strategy() click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 71
def traversal_strategy
  @traversal_strategy ||= ancestors.find do |constant|
    next if constant == self
    next unless constant.is_a? Class
    next unless constant.respond_to? :traversal_strategy
    next if constant.traversal_strategy.nil?

    break constant.traversal_strategy
  end
end

Public Instance Methods

<<(child) click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 19
def <<(child)
  @children << child
  @children.flatten! # Accepts array of children too.
  @children.map!(&:chainable_node)
end
halt!() click to toggle source
Calls superclass method
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 25
def halt!
  validate_non_leaf!

  super

  @children.each(&:halt!)
end

Protected Instance Methods

on_tick() click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 35
def on_tick
  raise NotImplementedError, 'Must implement control logic'
end
tick_each_children(&block) click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 43
def tick_each_children(&block)
  return enum_for(:tick_each_children) unless block_given?

  validate_non_leaf!

  Enumerator.new do |y|
    enum = send(traversal_strategy)
    validate_enum!(enum)

    enum.each do |child|
      child.tick!
      y << child
    end
  end.each(&block)
end
validate_non_leaf!() click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 39
def validate_non_leaf!
  raise InvalidLeafNodeError if @children.empty?
end

Private Instance Methods

traversal_strategy() click to toggle source
# File lib/behavior_tree/control_nodes/control_node_base.rb, line 61
def traversal_strategy
  self.class.traversal_strategy
end
validate_enum!(enum) click to toggle source

Keep it simple, because it's executed everytime it ticks.

# File lib/behavior_tree/control_nodes/control_node_base.rb, line 66
def validate_enum!(enum)
  raise IncorrectTraversalStrategyError, enum unless enum.respond_to? :each
end