module Gamefic::Node

Public Instance Methods

accessible?() click to toggle source

Determine if external objects can interact with this object's children. For example, a game can designate that the contents of a bowl are accessible, while the contents of a locked safe are not.

@return [Boolean]

# File lib/gamefic/node.rb, line 63
def accessible?
  true
end
children() click to toggle source

An array of the object's children.

@return [Array]

# File lib/gamefic/node.rb, line 10
def children
  @children ||= []
  @children.clone
end
flatten() click to toggle source

Get a flat array of all descendants.

@return [Array]

# File lib/gamefic/node.rb, line 18
def flatten
  array = Array.new
  children.each { |child|
    array = array + recurse_flatten(child)
  }
  array
end
parent() click to toggle source

The object's parent.

@return [Object]

# File lib/gamefic/node.rb, line 29
def parent
  @parent
end
parent=(node) click to toggle source

Set the object's parent.

# File lib/gamefic/node.rb, line 35
def parent=(node)
  return if node == @parent 
  if node == self
    raise CircularNodeReferenceError.new("Node cannot be its own parent")
  end
  # Do not permit circular references
  if node != nil and node.parent == self
    node.parent = nil
  end
  if node != nil and flatten.include?(node)
    raise CircularNodeReferenceError.new("Node cannot be a child of a descendant")
  end
  if @parent != node
    if @parent != nil
      @parent.send(:rem_child, self)
    end
    @parent = node
    if @parent != nil
      @parent.send(:add_child, self)
    end
  end
end

Protected Instance Methods

add_child(node) click to toggle source
# File lib/gamefic/node.rb, line 69
def add_child(node)
  children
  @children.push(node)
end
concat_children(children) click to toggle source
# File lib/gamefic/node.rb, line 79
def concat_children(children)
  children.concat(children)
end
rem_child(node) click to toggle source
# File lib/gamefic/node.rb, line 74
def rem_child(node)
  children
  @children.delete(node)
end

Private Instance Methods

recurse_flatten(node) click to toggle source
# File lib/gamefic/node.rb, line 85
def recurse_flatten(node)
  array = Array.new
  array.push(node)
  node.children.each { |child|
    array = array + recurse_flatten(child)
  }
  return array
end