dialogue.rb: Add debug mode and results

- Dialogue can show some additional, generally hidden details about what's on
  screen.
- New attribute :debug (Boolean)
- Added debug information
This commit is contained in:
Bill Niblock 2017-05-07 16:47:05 -04:00
parent 520aad9740
commit adecc3b193

View file

@ -9,8 +9,12 @@ module Dialogue
# The file, which should be a dialogue tree YAML file. # The file, which should be a dialogue tree YAML file.
attr_accessor :file attr_accessor :file
# Status of verbose/debug mode. True = on; false = off.
attr_accessor :debug
def initialize def initialize
@file = "" @file = ""
@debug = false
end end
# Conversation handles navigating the tree, until the option to end is # Conversation handles navigating the tree, until the option to end is
@ -19,12 +23,14 @@ module Dialogue
tree = Gardner.grow(@file) tree = Gardner.grow(@file)
10.times { print "*" } 10.times { print "*" }
puts "\n[ Branch: 1 ]" if @debug
next_branch = talk(tree[1]) next_branch = talk(tree[1])
until next_branch == 0 do until next_branch == 0 do
puts "\n[ Branch: #{next_branch} ]" if @debug
next_branch = talk(tree[next_branch]) next_branch = talk(tree[next_branch])
end end
puts tree[0]["desc"] puts "\n#{tree[0]["desc"]}"
exit exit
end end
@ -33,6 +39,7 @@ module Dialogue
# @param branch [Hash] A branch data set # @param branch [Hash] A branch data set
# @return [Integer] The number of the next branch # @return [Integer] The number of the next branch
def talk(branch) def talk(branch)
# If there are no options on this branch, we assume it's a terminal # If there are no options on this branch, we assume it's a terminal
# branch. Return 0, and end the program. # branch. Return 0, and end the program.
if branch["options"].empty? if branch["options"].empty?
@ -45,21 +52,24 @@ module Dialogue
puts "\n#{branch["desc"]}\n\n" puts "\n#{branch["desc"]}\n\n"
branch["options"].each_pair do |k,v| branch["options"].each_pair do |k,v|
puts "\t#{k}: #{v.keys[0]}" puts "\t#{k}: #{v.keys[0]}"
puts "\t\t [ Goes to branch #{v.values[0]} ]" if @debug
end end
print "\n[#{valid_options}]> " print "\n[#{valid_options}]> "
STDOUT.flush STDOUT.flush
response = STDIN.gets.chomp.to_i response = STDIN.gets.chomp.to_i
until branch["options"].keys.include?(response) until branch["options"].keys.include?(response) or response == 0
print "[## Invalid options. " print "[## Invalid options. "
print "Valid options are #{valid_options}, or 0 to exit." print "Valid options are #{valid_options}, or 0 to exit."
print "\n[#{valid_options}]> " print "\n[#{valid_options}]> "
response = STDIN.gets.chomp.to_i response = STDIN.gets.chomp.to_i
end end
puts "\n" puts "\n"
10.times { print "*" } 10.times { print "*" }
return 0 if response == 0
puts "\n(Your choice: #{branch["options"][response].keys[0]})" puts "\n(Your choice: #{branch["options"][response].keys[0]})"
return branch["options"][response].values[0].to_i return branch["options"][response].values[0].to_i
end end