Class: Dialogue::Speaker
- Inherits:
-
Object
- Object
- Dialogue::Speaker
- Defined in:
- lib/sapling.rb
Overview
Spealer holds the functionality for viewing and going through a dialogue tree.
Instance Attribute Summary collapse
-
#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
106 107 108 |
# File 'lib/sapling.rb', line 106 def initialize @file = "" end |
Instance Attribute Details
#file ⇒ Object
The file, which should be a dialogue tree YAML file.
104 105 106 |
# File 'lib/sapling.rb', line 104 def file @file end |
Instance Method Details
#conversation ⇒ Object
Conversation handles navigating the tree, until the option to end is reached.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sapling.rb', line 112 def conversation() tree = Gardner.grow(@file) 10.times { print "*" } next_branch = talk(tree[1]) until next_branch == 0 do next_branch = talk(tree[next_branch]) end puts tree[0]["desc"] exit end |
#talk(branch) ⇒ Integer
Talk displays a branch, the options, and prompts for a response
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sapling.rb', line 129 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]}" end print "\n[#{}]> " STDOUT.flush response = STDIN.gets.chomp.to_i until branch["options"].keys.include?(response) 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 "*" } puts "\n(Your choice: #{branch["options"][response].keys[0]})" return branch["options"][response].values[0].to_i end |