module Gamefic::Scriptable::Actions
Scriptable
methods related to creating actions.
Public Instance Methods
Add a proc to be evaluated after a character executes an action. When a verbs are specified, the proc will only be evaluated if the action’s verb matches them.
@param verbs [Array<Symbol>] @yieldparam [Gamefic::Action] @return [Action::Hook]
# File lib/gamefic/scriptable/actions.rb, line 75 def after_action *verbs, &block rulebook.hooks.after_action self, *verbs, &block end
Add a proc to be evaluated before a character executes an action. When verbs are specified, the proc will only be evaluated if the action’s verb matches them.
@param verbs [Array<Symbol>] @yieldparam [Gamefic::Action] @return [Action::Hook]
# File lib/gamefic/scriptable/actions.rb, line 64 def before_action *verbs, &block rulebook.hooks.before_action self, *verbs, &block end
Create an alternate Syntax
for a response. The command and its translation can be parameterized.
@example Create a synonym for an ‘inventory` response.
interpret "catalogue", "inventory" # The command "catalogue" will be translated to "inventory"
@example Create a parameterized synonym for a ‘look` response.
interpret "scrutinize :entity", "look :entity" # The command "scrutinize chair" will be translated to "look chair"
@param command [String] The format of the original command @param translation [String] The format of the translated command @return [Syntax] the Syntax
object
# File lib/gamefic/scriptable/actions.rb, line 93 def interpret command, translation rulebook.calls.add_syntax Syntax.new(command, translation) end
Create a meta response for a command. Meta responses are very similar to standard responses, except they’re flagged as meta (‘Response#meta?`) to indicate that they provide a feature that is not considered an in-game action, such as displaying help documentation or a scoreboard.
@example A simple meta Response
meta :credits do |actor| actor.tell "This game was written by John Smith." end
@param verb [Symbol] An imperative verb for the command @param args [Array<Object>] Filters for the command’s tokens @yieldparam [Gamefic::Actor] @return [Symbol]
# File lib/gamefic/scriptable/actions.rb, line 52 def meta(verb, *args, &proc) rulebook.calls.add_response Response.new(verb, self, *args, meta: true, &proc) verb end
Create a response to a command. A Response
uses the ‘verb` argument to identify the imperative verb that triggers the action. It can also accept queries to tokenize the remainder of the input and filter for particular entities or properties. The `block“ argument is the proc to execute when the input matches all of the Response’s criteria (i.e., verb and queries).
@example A simple Response
.
respond :wave do |actor| actor.tell "Hello!" end # The command "wave" will respond "Hello!"
@example A Response
that accepts a Character
respond :salute, available(Character) do |actor, character| actor.tell "#{The character} returns your salute." end
@param verb [Symbol] An imperative verb for the command @param args [Array<Object>] Filters for the command’s tokens @yieldparam [Gamefic::Actor] @return [Symbol]
# File lib/gamefic/scriptable/actions.rb, line 32 def respond(verb, *args, &proc) rulebook.calls.add_response Response.new(verb, self, *args, &proc) verb end
Synonyms are a combination of the rulebook’s concrete verbs plus the alternative variants defined in syntaxes.
@example
class MyPlot < Gamefic::Plot respond :think { |actor| actor.tell 'You think.' } interpret 'ponder', 'think' verbs #=> [:think] synonyms #=> [:think, :ponder] end end
@return [Array<Symbol>]
# File lib/gamefic/scriptable/actions.rb, line 127 def synonyms rulebook.synonyms end
@return [Array<Syntax>]
# File lib/gamefic/scriptable/actions.rb, line 132 def syntaxes rulebook.syntaxes end
Verbs are the symbols that have responses defined in the rulebook.
@example
class MyPlot < Gamefic::Plot script do respond :think { |actor| actor.tell 'You think.' } verbs #=> [:think] end end
@return [Array<Symbol>]
# File lib/gamefic/scriptable/actions.rb, line 109 def verbs rulebook.verbs end