From adecc3b1936251556f720139a5422cba8786d77a Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 16:47:05 -0400 Subject: [PATCH] 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 --- lib/sapling/dialogue.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 711717f..bbae8b9 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -9,8 +9,12 @@ module Dialogue # The file, which should be a dialogue tree YAML file. attr_accessor :file + # Status of verbose/debug mode. True = on; false = off. + attr_accessor :debug + def initialize @file = "" + @debug = false end # Conversation handles navigating the tree, until the option to end is @@ -19,12 +23,14 @@ module Dialogue tree = Gardner.grow(@file) 10.times { print "*" } + puts "\n[ Branch: 1 ]" if @debug next_branch = talk(tree[1]) until next_branch == 0 do + puts "\n[ Branch: #{next_branch} ]" if @debug next_branch = talk(tree[next_branch]) end - puts tree[0]["desc"] + puts "\n#{tree[0]["desc"]}" exit end @@ -33,6 +39,7 @@ module Dialogue # @param branch [Hash] A branch data set # @return [Integer] The number of the next branch def talk(branch) + # If there are no options on this branch, we assume it's a terminal # branch. Return 0, and end the program. if branch["options"].empty? @@ -45,21 +52,24 @@ module Dialogue puts "\n#{branch["desc"]}\n\n" branch["options"].each_pair do |k,v| puts "\t#{k}: #{v.keys[0]}" + puts "\t\t [ Goes to branch #{v.values[0]} ]" if @debug end print "\n[#{valid_options}]> " STDOUT.flush 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 "Valid options are #{valid_options}, or 0 to exit." print "\n[#{valid_options}]> " response = STDIN.gets.chomp.to_i end + puts "\n" 10.times { print "*" } + return 0 if response == 0 puts "\n(Your choice: #{branch["options"][response].keys[0]})" return branch["options"][response].values[0].to_i end