Class: Gamefic::Scene::MultipleChoice
- Defined in:
- lib/gamefic/scene/multiple_choice.rb
Overview
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.
Defined Under Namespace
Classes: Input
Instance Method Summary collapse
- #finish(actor, input) ⇒ Object
-
#initialize(config = {}) ⇒ MultipleChoice
constructor
A new instance of MultipleChoice.
- #invalid_choice_message ⇒ Object
- #prompt ⇒ Object
- #start(actor) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(config = {}) ⇒ MultipleChoice
13 14 15 16 17 |
# File 'lib/gamefic/scene/multiple_choice.rb', line 13 def initialize config = {} super = (config[:options] || []) = config[:invalid_choice_message] end |
Instance Method Details
#finish(actor, input) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gamefic/scene/multiple_choice.rb', line 25 def finish actor, input this_scene = actor.scene index = nil choice = nil if input.strip =~ /[0-9]+/ and input.to_i > 0 index = input.to_i - 1 choice = [index] else index = 0 .each { |o| if o.casecmp(input).zero? choice = o break end index += 1 } end if choice.nil? actor.tell tell_choices actor else input_object = Input.new(input, index, choice) super actor, input_object end end |
#invalid_choice_message ⇒ Object
55 56 57 |
# File 'lib/gamefic/scene/multiple_choice.rb', line 55 def ||= "That's not a valid selection." end |
#prompt ⇒ Object
51 52 53 |
# File 'lib/gamefic/scene/multiple_choice.rb', line 51 def prompt @prompt ||= "Enter a choice:" end |
#start(actor) ⇒ Object
19 20 21 22 23 |
# File 'lib/gamefic/scene/multiple_choice.rb', line 19 def start actor = .clone @start.call(actor, ) unless @start.nil? tell_choices actor end |