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

#accessibleArray<Entity>

Get an array of children that are accessible to external entities.

A child is considered accessible if external entities can interact with it. For Example, an author can designate that the contents of a bowl are accessible, while the contents of a locked safe are not. All of an entity’s children are accessible by default. Authors should override this method if they need custom behavior.

Returns:



88
89
90
# File 'lib/gamefic/node.rb', line 88

def accessible
  children
end

#adjacent?(other) ⇒ Boolean

True if this node and the other node have the same parent.

Parameters:

Returns:

  • (Boolean)


102
103
104
# File 'lib/gamefic/node.rb', line 102

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)


95
96
97
# File 'lib/gamefic/node.rb', line 95

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

#put(parent, relation = nil) ⇒ Object Also known as: place



73
74
75
76
# File 'lib/gamefic/node.rb', line 73

def put(parent, relation = nil)
  self.parent = parent
  @relation = relation
end

#relationSymbol?

The node’s relation to its parent.

The inherently supported relations are ‘:in` and `:on`, but authors are free to define their own.

Returns:

  • (Symbol, nil)


52
53
54
# File 'lib/gamefic/node.rb', line 52

def relation
  @relation ||= (parent ? :in : nil)
end

#relation=(symbol) ⇒ Object

Parameters:

  • symbol (Symbol, nil)

Raises:



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

def relation=(symbol)
  raise NodeError, "Invalid relation #{symbol.inspect} on #{inspect} without parent" unless parent || !symbol

  @relation = symbol
end

#take(*children, relation: nil) ⇒ Array<Node>

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

Parameters:

Returns:



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

def take *children, relation: nil
  children.flatten.each { |child| child.put self, relation }
  children
end