gardner.rb: Rubocop fixes and refactor changes
- Fix several rubocop problems - Update module to more fully encapsulate all dialogue tree handling - Add Plot class for storing all tree data - Add Digiplot class for (eventual) tree editing
This commit is contained in:
parent
8700f307a7
commit
6e16b250a5
1 changed files with 82 additions and 34 deletions
|
@ -2,47 +2,95 @@ require 'yaml'
|
||||||
|
|
||||||
# Gardner is the module for working with a dialogue tree file
|
# Gardner is the module for working with a dialogue tree file
|
||||||
module Gardner
|
module Gardner
|
||||||
|
# The Plot class handles a specific tree file. It provides functionality for
|
||||||
|
# parsing trunks and branches, and provides these as class attributes.
|
||||||
|
class Plot
|
||||||
|
# The trunk and branches instance variables
|
||||||
|
attr_reader :branches, :tree, :trunk
|
||||||
|
|
||||||
# Parse the tree array into an array of numbered branches, and ordered leaves.
|
# Initialize a new Plot from a tree file
|
||||||
#
|
#
|
||||||
# @param tree [Array] The dialogue tree
|
# @param tree [File] The dialogue tree file
|
||||||
# @return [Array] An array of numbered branches, with numbered leaves
|
def initialize(file)
|
||||||
def self.prune_branches(tree)
|
@tree = file
|
||||||
branches = { 0 => { "desc" => "Thanks for using Sapling!" } }
|
prune_trunk
|
||||||
tree.each do |b|
|
prune_branches
|
||||||
branches[b["branch"]["number"]] = {
|
|
||||||
"desc" => b["branch"]["text"],
|
|
||||||
"options" => prune_leaves(b["branch"]["leaf"]) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return branches
|
# Parse the tree array into an array of numbered branches, and ordered
|
||||||
end
|
# leaves.
|
||||||
|
#
|
||||||
# Parse the leaves of a branch into a numbered hash of options.
|
# @param tree [File] The dialogue tree
|
||||||
#
|
# @return [Array] An array of numbered branches, with numbered leaves
|
||||||
# @param leaves [Array] The option of leaf hashes
|
def prune_branches
|
||||||
# @return [Hash] A numbered hash of options
|
@branches = { 0 => { 'desc' => 'Thanks for using Sapling!' } }
|
||||||
def self.prune_leaves(leaves)
|
@tree.each do |b|
|
||||||
x = 1
|
@branches[b['branch']['number']] = {
|
||||||
options = {}
|
'desc' => b['branch']['text'],
|
||||||
|
'options' => prune_leaves(b['branch']['leaf'])
|
||||||
return options if leaves.nil?
|
}
|
||||||
|
end
|
||||||
leaves.each do |l|
|
|
||||||
options[x] = { l["text"] => l["branch"] }
|
|
||||||
x += 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return options
|
# Parse the leaves of a branch into a numbered hash of options.
|
||||||
|
#
|
||||||
|
# @param leaves [Array] The option of leaf hashes
|
||||||
|
# @return [Hash] A numbered hash of options
|
||||||
|
def prune_leaves(leaves)
|
||||||
|
x = 1
|
||||||
|
options = {}
|
||||||
|
|
||||||
|
return options if leaves.nil?
|
||||||
|
|
||||||
|
leaves.each do |l|
|
||||||
|
options[x] = { l['text'] => l['branch'] }
|
||||||
|
x += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parse the trunk of the tree.
|
||||||
|
#
|
||||||
|
# @return [Array] The trunk, and the remainder of the tree
|
||||||
|
def prune_trunk
|
||||||
|
@trunk = @tree.shift
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse the trunk of the tree.
|
# Digiplot represents a Plot used for editing. The Digiplot functions exactly
|
||||||
#
|
# like a Plot, except with additional functionality for over-writing existing
|
||||||
# @param tree [Hash] The entire tree
|
# branches, leaves, and the trunk.
|
||||||
# @return [Array] The trunk, and the remainder of the tree
|
class Digiplot < Plot
|
||||||
def self.prune_trunk(tree)
|
# Duplicate the "old" trunk and branches, for restoration purposes
|
||||||
trunk = tree.shift
|
attr_reader :old_branches, :old_trunk
|
||||||
|
|
||||||
return [trunk,tree]
|
# Enable editing for the trunk
|
||||||
|
attr_writer :trunk
|
||||||
|
|
||||||
|
# Initialize a Digiplot just like a Plot, but also copy the trunk and
|
||||||
|
# branches to "old" instance variables.
|
||||||
|
def initialize
|
||||||
|
super
|
||||||
|
@old_trunk = @trunk
|
||||||
|
@old_branches = @branches
|
||||||
|
end
|
||||||
|
|
||||||
|
# Change a branch
|
||||||
|
#
|
||||||
|
# @param branch [Integer] the number of the branch to be edited
|
||||||
|
def branch=(branch, text)
|
||||||
|
@branches[branch]['desc'] = text
|
||||||
|
end
|
||||||
|
|
||||||
|
# Change a leaf on a branch, grasshopper
|
||||||
|
#
|
||||||
|
# @param branch [Integer] the number of the branch to be edited
|
||||||
|
# @param leaf [Integer] the number of the leaf to be edited
|
||||||
|
# @param text [String] the new text for the leaf
|
||||||
|
# @param target [Integer] the branch number target for the leaf option
|
||||||
|
def leaf=(branch, leaf, text, target)
|
||||||
|
@branches[branch]['options'][leaf] = { text => target }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue