class Dialogue::Speaker
Speaker
holds the functionality for going through a dialogue tree.
Attributes
debug[R]
Status of verbose/debug mode. True = on; false = off.
tree[R]
The tree, an instance of Gardner::Plot
Public Class Methods
new(tree, debug = false)
click to toggle source
# File lib/sapling/dialogue.rb, line 39 def initialize(tree, debug = false) @tree = tree @debug = debug end
Public Instance Methods
conversation()
click to toggle source
Conversation handles navigating the tree, until the option to end is reached.
# File lib/sapling/dialogue.rb, line 46 def conversation Dialogue.display_trunk(@tree.trunk, @debug) next_branch = 1 until next_branch.zero? next_branch = talk(@tree.branches[next_branch], next_branch) end puts "\n#{@tree.branches[0]['desc']}" exit end
get_response(branch)
click to toggle source
Get a response for the displayed branch
@param branch [Hash] A branch data set @return [Integer] the next branch
# File lib/sapling/dialogue.rb, line 82 def get_response(branch) valid_options = branch['options'].keys.join(', ') print "\n[#{valid_options}]> " STDOUT.flush response = STDIN.gets.chomp until valid_options.include?(response) || response.to_i.zero? print "[## Invalid options. Valid options are #{valid_options}," \ "or 0 to exit.\n[#{valid_options}]> " response = STDIN.gets.chomp end response.to_i end
talk(branch, branch_no)
click to toggle source
Talk displays a branch, the options, and prompts for a response.
@param branch [Hash] A branch data set @param branch_no [Integer] The branch number @return [Integer] The number of the next branch
# File lib/sapling/dialogue.rb, line 63 def talk(branch, branch_no) return 0 if terminal?(branch) Dialogue.display_branch(branch, branch_no, @debug) response = get_response(branch) unless response.zero? puts "(Your choice: #{branch['options'][response].keys[0]})" response = branch['options'][response].values[0].to_i end response end
terminal?(branch)
click to toggle source
Check if a branch is terminal
@param branch [Hash] A branch data set @return [Boolean] true if the branch is terminal, false otherwise
# File lib/sapling/dialogue.rb, line 102 def terminal?(branch) if branch['options'].empty? puts "\n#{branch['desc']}\n\n" return true end false end