Class: Dialogue::Speaker

Inherits:
Object
  • Object
show all
Defined in:
lib/sapling/dialogue.rb

Overview

Spealer holds the functionality for viewing and going through a dialogue tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpeaker

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

#debugObject

Status of verbose/debug mode. True = on; false = off.



13
14
15
# File 'lib/sapling/dialogue.rb', line 13

def debug
  @debug
end

#fileObject

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

#conversationObject

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

Parameters:

  • branch (Hash)

    A branch data set

Returns:

  • (Integer)

    The number of the next branch



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

  valid_options = 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[#{valid_options}]> "
  STDOUT.flush
  response = STDIN.gets.chomp.to_i

  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