Class: Dialogue::Speaker
- Inherits:
-
Object
- Object
- Dialogue::Speaker
- Defined in:
- lib/sapling/dialogue.rb
Overview
Spealer holds the functionality for viewing and going through a dialogue tree.
Instance Attribute Summary collapse
-
#debug ⇒ Object
Status of verbose/debug mode.
-
#file ⇒ Object
The file, which should be a dialogue tree YAML file.
Instance Method Summary collapse
-
#conversation ⇒ Object
Conversation handles navigating the tree, until the option to end is reached.
-
#initialize ⇒ Speaker
constructor
A new instance of Speaker.
-
#talk(branch) ⇒ Integer
Talk displays a branch, the options, and prompts for a response.
Constructor Details
#initialize ⇒ Speaker
Returns a new instance of Speaker
15 16 17 18 |
# File 'lib/sapling/dialogue.rb', line 15 def initialize @file = "" @debug = false end |
Instance Attribute Details
#debug ⇒ Object
Status of verbose/debug mode. True = on; false = off.
13 14 15 |
# File 'lib/sapling/dialogue.rb', line 13 def debug @debug end |
#file ⇒ Object
The file, which should be a dialogue tree YAML file.
10 11 12 |
# File 'lib/sapling/dialogue.rb', line 10 def file @file end |
Instance Method Details
#conversation ⇒ Object
Conversation handles navigating the tree, until the option to end is reached.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sapling/dialogue.rb', line 22 def conversation() 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 "\n#{tree[0]["desc"]}" exit end |
#talk(branch) ⇒ Integer
Talk displays a branch, the options, and prompts for a response
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sapling/dialogue.rb', line 41 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? puts "\n#{branch["desc"]}\n\n" return 0 end = branch["options"].keys.join(", ") 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[#{}]> " STDOUT.flush response = STDIN.gets.chomp.to_i until branch["options"].keys.include?(response) or response == 0 print "[## Invalid options. " print "Valid options are #{}, or 0 to exit." print "\n[#{}]> " 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 |