Module: Gamefic::Scriptable::Hooks

Included in:
Gamefic::Scriptable
Defined in:
lib/gamefic/scriptable/hooks.rb

Overview

Scriptable hook methods are class methods that define procs to be executed in response to various game events.

Instance Method Summary collapse

Instance Method Details

#after_command(*verbs) {|, | ... } ⇒ Object

Define a callback to be executed after a command is processed. The callback accepts two parameters, the Actor and the Command.

The optional verbs parameter can be used to restrict the callback to commands that use one of the specified verbs. If no verbs are provided, the callback will be executed after every command.

Parameters:

Yield Parameters:



47
48
49
50
51
# File 'lib/gamefic/scriptable/hooks.rb', line 47

def after_command(*verbs, &block)
  after_commands.push(proc do |actor, command|
    instance_exec(actor, command, &block) if verbs.empty? || verbs.include?(command.verb)
  end)
end

#after_commandsArray<Proc>

Returns:



131
132
133
# File 'lib/gamefic/scriptable/hooks.rb', line 131

def after_commands
  @after_commands ||= []
end

#before_command(*verbs) {|, | ... } ⇒ Object

Define a callback to be executed before a command is processed. The callback accepts two parameters, the Actor and the Command.

The optional verbs parameter can be used to restrict the callback to commands that use one of the specified verbs. If no verbs are provided, the callback will be executed before every command.

Calling Command#cancel will prevent the command from being performed.

Examples:

Cancel non-meta commands when the actor does not have a parent

before_command do |actor, command|
  next if actor.parent || command.meta?

  actor.tell "You can't do anything while you're in limbo."
  command.cancel
end

Parameters:

Yield Parameters:



30
31
32
33
34
# File 'lib/gamefic/scriptable/hooks.rb', line 30

def before_command(*verbs, &block)
  before_commands.push(proc do |actor, command|
    instance_exec(actor, command, &block) if verbs.empty? || verbs.include?(command.verb)
  end)
end

#before_commandsArray<Proc>

Returns:



126
127
128
# File 'lib/gamefic/scriptable/hooks.rb', line 126

def before_commands
  @before_commands ||= []
end

#conclude_blocksArray<Proc>

Returns:



151
152
153
# File 'lib/gamefic/scriptable/hooks.rb', line 151

def conclude_blocks
  @conclude_blocks ||= []
end

#on_conclude(&block) ⇒ Object

Define a callback that gets executed when a narrative reaches a conclusion.



109
110
111
# File 'lib/gamefic/scriptable/hooks.rb', line 109

def on_conclude(&block)
  conclude_blocks.push(block)
end

#on_player_conclude {|| ... } ⇒ Object

Note:

A player can conclude participation in a narrative without the narrative itself concluding.

Define a callback that gets executed when a player reaches a conclusion.

Yield Parameters:



121
122
123
# File 'lib/gamefic/scriptable/hooks.rb', line 121

def on_player_conclude(&block)
  player_conclude_blocks.push(block)
end

#on_player_output {|, | ... } ⇒ Object

Define a callback that modifies the output sent to the player at the beginning of a game turn. The callback accepts two parameters, the Actor and the Props::Output.

Narrators execute player_output blocks after starting a scene and executing ready blocks. The output gets sent to players’ game clients as JSON objects to be rendered before prompting the player for input.

Examples:

Add a player’s parent to the output as a custom hash value.

on_player_output do |actor, output|
  output[:parent_name] = actor.parent&.name
end

Yield Parameters:



101
102
103
# File 'lib/gamefic/scriptable/hooks.rb', line 101

def on_player_output(&block)
  player_output_blocks.push(block)
end

#on_player_ready {|| ... } ⇒ Object

Define a callback to be executed for each participating player after a scene starts.

Yield Parameters:



65
66
67
# File 'lib/gamefic/scriptable/hooks.rb', line 65

def on_player_ready(&block)
  ready_blocks.push(proc { players.each { |player| instance_exec(player, &block) } })
end

#on_player_update {|| ... } ⇒ Object

Define a callback to be executed for each participating player after a scene finishes.

Yield Parameters:



81
82
83
# File 'lib/gamefic/scriptable/hooks.rb', line 81

def on_player_update(&block)
  update_blocks.push(proc { players.each { |player| instance_exec(player, &block) } })
end

#on_ready(&block) ⇒ Object

Define a callback to be executed after a scene starts.



56
57
58
# File 'lib/gamefic/scriptable/hooks.rb', line 56

def on_ready(&block)
  ready_blocks.push block
end

#on_update(&block) ⇒ Object

Define a callback to be executed after a scene finishes.



72
73
74
# File 'lib/gamefic/scriptable/hooks.rb', line 72

def on_update(&block)
  update_blocks.push block
end

#player_conclude_blocksArray<Proc>

Returns:



156
157
158
# File 'lib/gamefic/scriptable/hooks.rb', line 156

def player_conclude_blocks
  @player_conclude_blocks ||= []
end

#player_output_blocksArray<Proc>

Returns:



146
147
148
# File 'lib/gamefic/scriptable/hooks.rb', line 146

def player_output_blocks
  @player_output_blocks ||= []
end

#ready_blocksArray<Proc>

Returns:



136
137
138
# File 'lib/gamefic/scriptable/hooks.rb', line 136

def ready_blocks
  @ready_blocks ||= []
end

#update_blocksArray<Proc>

Returns:



141
142
143
# File 'lib/gamefic/scriptable/hooks.rb', line 141

def update_blocks
  @update_blocks ||= []
end