Class: Gamefic::Props::MultipleChoice

Inherits:
Default
  • Object
show all
Defined in:
lib/gamefic/props/multiple_choice.rb

Overview

Props for MultipleChoice scenes.

Direct Known Subclasses

MultiplePartial

Instance Attribute Summary collapse

Attributes inherited from Default

#input, #prompt

Instance Method Summary collapse

Methods inherited from Default

#enter, #output

Instance Attribute Details

#invalid_messageString

A message to send the player for an invalid choice. A formatting token named ‘%<input>s` can be used to inject the user input.

Examples:

props.invalid_message = '"%<input>s" is not a valid choice.'

Returns:



25
26
27
# File 'lib/gamefic/props/multiple_choice.rb', line 25

def invalid_message
  @invalid_message ||= '"%<input>s" is not a valid choice.'
end

Instance Method Details

#indexInteger?

The zero-based index of the selected option.

Returns:

  • (Integer, nil)


32
33
34
35
36
# File 'lib/gamefic/props/multiple_choice.rb', line 32

def index
  return nil unless input

  @index ||= index_of(input)
end

#index_of(option) ⇒ Integer?

Get the index of an option using input criteria, e.g., a one-based number or the text of the option. The return value is the option’s zero-based index or nil.

Examples:

props = Gamefic::Props::MultipleChoice.new
props.options.push 'First choice', 'Second choice'

props.index_of(1)               # => 0
props.index_of('Second choice') # => 1

Parameters:

Returns:

  • (Integer, nil)


73
74
75
# File 'lib/gamefic/props/multiple_choice.rb', line 73

def index_of(option)
  index_by_number(option) || index_by_text(option)
end

#numberInteger?

The one-based index of the selected option.

Returns:

  • (Integer, nil)


41
42
43
44
45
# File 'lib/gamefic/props/multiple_choice.rb', line 41

def number
  return nil unless index

  index + 1
end

#optionsArray<String>

The array of available options.

Returns:



14
15
16
# File 'lib/gamefic/props/multiple_choice.rb', line 14

def options
  @options ||= []
end

#selected?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gamefic/props/multiple_choice.rb', line 56

def selected?
  !!index
end

#selectionString?

The full text of the selected option.

Returns:



50
51
52
53
54
# File 'lib/gamefic/props/multiple_choice.rb', line 50

def selection
  return nil unless index

  options[index]
end