Module: Gamefic::Node

Included in:
Entity
Defined in:
lib/gamefic/node.rb

Instance Method Summary collapse

Instance Method Details

#accessible?Boolean

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.

Returns:

  • (Boolean)


63
64
65
# File 'lib/gamefic/node.rb', line 63

def accessible?
  true
end

#childrenArray

An array of the object’s children.

Returns:



10
11
12
13
# File 'lib/gamefic/node.rb', line 10

def children
  @children ||= []
  @children.clone
end

#flattenArray

Get a flat array of all descendants.

Returns:



18
19
20
21
22
23
24
# File 'lib/gamefic/node.rb', line 18

def flatten
  array = Array.new
  children.each { |child|
    array = array + recurse_flatten(child)
  }
  array
end

#parentObject

The object’s parent.

Returns:



29
30
31
# File 'lib/gamefic/node.rb', line 29

def parent
  @parent
end

#parent=(node) ⇒ Object

Set the object’s parent.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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