Module: Gamefic::Node

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

Overview

Parent/child relationships for objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentNode?

The object’s parent.

Returns:



16
17
18
# File 'lib/gamefic/node.rb', line 16

def parent
  @parent
end

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)


59
60
61
# File 'lib/gamefic/node.rb', line 59

def accessible?
  true
end

#adjacent?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/gamefic/node.rb', line 70

def adjacent?(other)
  other.parent == parent
end

#childrenArray<Node>

An array of the object’s children.

Returns:



21
22
23
# File 'lib/gamefic/node.rb', line 21

def children
  child_set.to_a.freeze
end

#flattenArray<Node>

Get a flat array of all descendants.

Returns:



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

def flatten
  children.flat_map { |child| [child] + child.flatten }
end

#include?(other) ⇒ Boolean

True if this node is the other’s parent.

Parameters:

Returns:

  • (Boolean)


66
67
68
# File 'lib/gamefic/node.rb', line 66

def include?(other)
  other.parent == self
end

#take(*children) ⇒ Array<Node>

Add children to the node. Return all the node’s children.

Parameters:

Returns:



49
50
51
52
# File 'lib/gamefic/node.rb', line 49

def take *children
  children.flatten.each { |child| child.parent = self }
  children
end