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
|
||||
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
|
||||
# @return [Array] An array of numbered branches, with numbered leaves
|
||||
def self.prune_branches(tree)
|
||||
branches = { 0 => { "desc" => "Thanks for using Sapling!" } }
|
||||
tree.each do |b|
|
||||
branches[b["branch"]["number"]] = {
|
||||
"desc" => b["branch"]["text"],
|
||||
"options" => prune_leaves(b["branch"]["leaf"]) }
|
||||
# @param tree [File] The dialogue tree file
|
||||
def initialize(file)
|
||||
@tree = file
|
||||
prune_trunk
|
||||
prune_branches
|
||||
end
|
||||
|
||||
return branches
|
||||
# Parse the tree array into an array of numbered branches, and ordered
|
||||
# leaves.
|
||||
#
|
||||
# @param tree [File] The dialogue tree
|
||||
# @return [Array] An array of numbered branches, with numbered leaves
|
||||
def prune_branches
|
||||
@branches = { 0 => { 'desc' => 'Thanks for using Sapling!' } }
|
||||
@tree.each do |b|
|
||||
@branches[b['branch']['number']] = {
|
||||
'desc' => b['branch']['text'],
|
||||
'options' => prune_leaves(b['branch']['leaf'])
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# 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 self.prune_leaves(leaves)
|
||||
def prune_leaves(leaves)
|
||||
x = 1
|
||||
options = {}
|
||||
|
||||
return options if leaves.nil?
|
||||
|
||||
leaves.each do |l|
|
||||
options[x] = { l["text"] => l["branch"] }
|
||||
options[x] = { l['text'] => l['branch'] }
|
||||
x += 1
|
||||
end
|
||||
|
||||
return options
|
||||
options
|
||||
end
|
||||
|
||||
# Parse the trunk of the tree.
|
||||
#
|
||||
# @param tree [Hash] The entire tree
|
||||
# @return [Array] The trunk, and the remainder of the tree
|
||||
def self.prune_trunk(tree)
|
||||
trunk = tree.shift
|
||||
def prune_trunk
|
||||
@trunk = @tree.shift
|
||||
end
|
||||
end
|
||||
|
||||
return [trunk,tree]
|
||||
# Digiplot represents a Plot used for editing. The Digiplot functions exactly
|
||||
# like a Plot, except with additional functionality for over-writing existing
|
||||
# branches, leaves, and the trunk.
|
||||
class Digiplot < Plot
|
||||
# Duplicate the "old" trunk and branches, for restoration purposes
|
||||
attr_reader :old_branches, :old_trunk
|
||||
|
||||
# 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
|
||||
|
|
Loading…
Reference in a new issue