class Gamefic::Scene::MultipleChoice
Provide a list of options and process the selection in the scene's finish block. After the scene is finished, the :active scene will be cued unless some other scene has already been prepared or cued.
The finish block's input parameter receives a MultipleChoice::Input object instead of a String
.
Attributes
index[R]
The zero-based index of the selected option.
@return [Integer]
invalid_message[W]
number[R]
The one-based index of the selected option.
@return [Integer]
selection[R]
The full text of the selected option.
@return [String]
Public Instance Methods
finish()
click to toggle source
Calls superclass method
Gamefic::Scene::Base#finish
# File lib/gamefic/scene/multiple_choice.rb, line 32 def finish get_choice if selection.nil? actor.tell invalid_message else super end end
invalid_message()
click to toggle source
The text to display when an invalid selection is received.
@return [String]
# File lib/gamefic/scene/multiple_choice.rb, line 51 def invalid_message @invalid_message ||= 'That is not a valid choice.' end
options()
click to toggle source
The array of available options.
@return [Array<String>]
# File lib/gamefic/scene/multiple_choice.rb, line 44 def options @options ||= [] end
post_initialize()
click to toggle source
# File lib/gamefic/scene/multiple_choice.rb, line 27 def post_initialize self.type = 'MultipleChoice' self.prompt = 'Enter a choice:' end
state()
click to toggle source
Calls superclass method
Gamefic::Scene::Base#state
# File lib/gamefic/scene/multiple_choice.rb, line 55 def state super.merge options: options end
Private Instance Methods
get_choice()
click to toggle source
# File lib/gamefic/scene/multiple_choice.rb, line 61 def get_choice if input.strip =~ /^[0-9]+$/ and input.to_i > 0 @number = input.to_i @index = number - 1 @selection = options[index] else i = 0 options.each { |o| if o.casecmp(input).zero? @selection = o @index = i @number = index + 1 break end i += 1 } end end