Class: Dialogue::Speaker
- Inherits:
-
Object
- Object
- Dialogue::Speaker
- Defined in:
- lib/sapling/dialogue.rb
Overview
Speaker holds the functionality for 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.
-
#get_response(branch) ⇒ Integer
Get a response for the displayed branch.
-
#initialize(file = "", debug = false) ⇒ Speaker
constructor
A new instance of Speaker.
-
#talk(branch, branch_no) ⇒ Integer
Talk displays a branch, the options, and prompts for a response.
Constructor Details
#initialize(file = "", debug = false) ⇒ Speaker
Returns a new instance of Speaker
40 41 42 43 |
# File 'lib/sapling/dialogue.rb', line 40 def initialize(file="", debug=false) @file = file @debug = debug end |
Instance Attribute Details
#debug ⇒ Object
Status of verbose/debug mode. True = on; false = off.
38 39 40 |
# File 'lib/sapling/dialogue.rb', line 38 def debug @debug end |
#file ⇒ Object
The file, which should be a dialogue tree YAML file.
36 37 38 |
# File 'lib/sapling/dialogue.rb', line 36 def file @file end |
Instance Method Details
#conversation ⇒ Object
Conversation handles navigating the tree, until the option to end is reached.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sapling/dialogue.rb', line 47 def conversation() tree = Gardner.prune_trunk(@file) Dialogue.display_trunk(tree[0], false) branches = Gardner.prune_branches(tree[1]) next_branch = 1 until next_branch == 0 do next_branch = talk(branches[next_branch], next_branch) end puts "\n#{branches[0]["desc"]}" exit end |
#get_response(branch) ⇒ Integer
Get a response for the displayed branch
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sapling/dialogue.rb', line 93 def get_response(branch) = branch["options"].keys.join(", ") 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 return response end |
#talk(branch, branch_no) ⇒ Integer
Talk displays a branch, the options, and prompts for a response.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sapling/dialogue.rb', line 67 def talk(branch, branch_no) # 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 Dialogue.display_branch(branch, branch_no, @debug) response = get_response(branch) unless response == 0 puts "\n" 10.times { print "*" } puts "\n(Your choice: #{branch["options"][response].keys[0]})" response = branch["options"][response].values[0].to_i end return response end |