Class: Gamefic::Scene::MultipleChoice

Inherits:
Custom
  • Object
show all
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

Methods inherited from Base

#state

Constructor Details

#initialize(config = {}) ⇒ MultipleChoice



13
14
15
16
17
# File 'lib/gamefic/scene/multiple_choice.rb', line 13

def initialize config = {}
  super
  @options = (config[:options] || [])
  @invalid_choice_message = 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 = @current_options[index]
  else
    index = 0
    @current_options.each { |o|
      if o.casecmp(input).zero?
        choice = o
        break
      end
      index += 1
    }
  end
  if choice.nil?
    actor.tell invalid_choice_message
    tell_choices actor
  else
    input_object = Input.new(input, index, choice)
    super actor, input_object
  end
end

#invalid_choice_messageObject



55
56
57
# File 'lib/gamefic/scene/multiple_choice.rb', line 55

def invalid_choice_message
  @invalid_choice_message ||= "That's not a valid selection."
end

#promptObject



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
  @current_options = @options.clone
  @start.call(actor, @current_options) unless @start.nil?
  tell_choices actor
end