Module: Gamefic::Scriptable::Scenes

Included in:
Narrative, Gamefic::Scriptable
Defined in:
lib/gamefic/scriptable/scenes.rb

Overview

Scriptable methods related to creating scenes.

Instance Method Summary collapse

Instance Method Details

#block(name, klass = Scene::Default, on_start: nil, on_finish: nil, &block) {|| ... } ⇒ Symbol Also known as: scene

Block a new scene.

Examples:

Prompt the player for a name

block :name_of_scene do |scene|
  # The scene's start occurs before the user gets prompted for input
  scene.on_start do |actor, props|
    props.prompt = 'What's your name?'
  end

  # The scene's finish is where you can process the user's input
  scene.on_finish do |actor, props|
    if props.input.empty?
      # You can use recue to start the scene again
      actor.recue
    else
      actor.tell "Hello, #{props.input}!"
    end
  end
end

Parameters:

  • name (Symbol)
  • klass (Class<Scene::Default>) (defaults to: Scene::Default)
  • on_start (Proc, nil) (defaults to: nil)
  • on_finish (Proc, nil) (defaults to: nil)
  • block (Proc)

Yield Parameters:

Returns:

  • (Symbol)


35
36
37
38
# File 'lib/gamefic/scriptable/scenes.rb', line 35

def block name, klass = Scene::Default, on_start: nil, on_finish: nil, &block
  rulebook.scenes.add klass.new(name, rulebook.narrative, on_start: on_start, on_finish: on_finish, &block)
  name
end

#conclusion(name) {|| ... } ⇒ Symbol

Create a conclusion. The game (or the character’s participation in it) will end after this scene is complete.

Examples:

conclusion :ending do |actor|
  actor.tell 'GAME OVER'
end

Parameters:

  • name (Symbol)

Yield Parameters:

Returns:

  • (Symbol)


151
152
153
154
155
# File 'lib/gamefic/scriptable/scenes.rb', line 151

def conclusion name, &start
  block name,
        Scene::Conclusion,
        on_start: start
end

#introduction {|, | ... } ⇒ Symbol

Add a block to be executed when a player is added to the game. Each Plot should only have one introduction.

Examples:

Welcome the player to the game

introduction do |actor|
  actor.tell "Welcome to the game!"
end

Yield Parameters:

Returns:

  • (Symbol)

Raises:

  • (ArgumentError)

    if an introduction already exists



54
55
56
57
58
59
# File 'lib/gamefic/scriptable/scenes.rb', line 54

def introduction(&start)
  rulebook.scenes
          .introduction Scene::Default.new nil,
                                           rulebook.narrative,
                                           on_start: proc { |actor, _props| instance_exec(actor, &start) }
end

#multiple_choice(name, choices = [], prompt = 'What is your choice?') {|, | ... } ⇒ Symbol

Create a multiple-choice scene. The user will be required to make a choice to continue. The scene will restart if the user input is not a valid choice.

Examples:

multiple_choice :go_somewhere, ['Go to work', 'Go to school'] do |actor, props|
  # Assuming the user selected the first choice:
  props.selection #=> 'Go to work'
  props.index     #=> 0
  props.number    #=> 1
end

Parameters:

  • name (Symbol)
  • choices (Array<String>) (defaults to: [])
  • prompt (String, nil) (defaults to: 'What is your choice?')
  • proc (Proc)

Yield Parameters:

Returns:

  • (Symbol)


80
81
82
83
84
85
86
87
88
# File 'lib/gamefic/scriptable/scenes.rb', line 80

def multiple_choice name, choices = [], prompt = 'What is your choice?', &block
  block name,
        Scene::MultipleChoice,
        on_start: proc { |_actor, props|
          props.prompt = prompt
          props.options.concat choices
        },
        on_finish: block
end

#pause(name, prompt: 'Press enter to continue...') {|| ... } ⇒ Symbol

Create a scene that pauses the game. This scene will execute the specified block and wait for input from the the user (e.g., pressing Enter) to continue.

Examples:

pause :wait do |actor|
  actor.tell "After you continue, you will be prompted for a command."
end

Parameters:

  • name (Symbol)
  • prompt (String, nil) (defaults to: 'Press enter to continue...')

    The text to display when prompting the user to continue

Yield Parameters:

Returns:

  • (Symbol)


130
131
132
133
134
135
136
137
# File 'lib/gamefic/scriptable/scenes.rb', line 130

def pause name, prompt: 'Press enter to continue...', &start
  block name,
        Scene::Pause,
        on_start: proc { |actor, props|
          props.prompt = prompt if prompt
          instance_exec(actor, props, &start)
        }
end

#scenesObject



157
158
159
# File 'lib/gamefic/scriptable/scenes.rb', line 157

def scenes
  rulebook.scenes.names
end

#yes_or_no(name, prompt = 'Answer:') {|, | ... } ⇒ Symbol

Create a yes-or-no scene. The user will be required to answer Yes or No to continue. The scene will restart if the user input is not a valid choice.

Examples:

yes_or_no :answer_scene, 'What is your answer?' do |actor, props|
  if props.yes?
    actor.tell "You said yes."
  else
    actor.tell "You said no."
  end
end

Parameters:

  • name (Symbol)
  • prompt (String, nil) (defaults to: 'Answer:')

Yield Parameters:

Returns:

  • (Symbol)


108
109
110
111
112
113
114
115
# File 'lib/gamefic/scriptable/scenes.rb', line 108

def yes_or_no name, prompt = 'Answer:', &block
  block name,
        Scene::YesOrNo,
        on_start: proc { |_actor, props|
          props.prompt = prompt
        },
        on_finish: block
end