Module: Gamefic::Scriptable::Actions

Includes:
Queries
Included in:
Narrative, Gamefic::Scriptable
Defined in:
lib/gamefic/scriptable/actions.rb

Overview

Scriptable methods related to creating actions.

Instance Method Summary collapse

Methods included from Queries

#abstract, #anywhere, #available, #children, #descendants, #myself, #parent, #plaintext, #siblings

Methods included from Proxies

#unproxy

Instance Method Details

#after_action(*verbs) {|| ... } ⇒ Action::Hook

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.

Parameters:

Yield Parameters:

Returns:



75
76
77
# File 'lib/gamefic/scriptable/actions.rb', line 75

def after_action *verbs, &block
  rulebook.hooks.after_action self, *verbs, &block
end

#before_action(*verbs) {|| ... } ⇒ Action::Hook

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.

Parameters:

Yield Parameters:

Returns:



64
65
66
# File 'lib/gamefic/scriptable/actions.rb', line 64

def before_action *verbs, &block
  rulebook.hooks.before_action self, *verbs, &block
end

#interpret(command, translation) ⇒ Syntax

Create an alternate Syntax for a response. The command and its translation can be parameterized.

Examples:

Create a synonym for an ‘inventory` response.

interpret "catalogue", "inventory"
# The command "catalogue" will be translated to "inventory"

Create a parameterized synonym for a ‘look` response.

interpret "scrutinize :entity", "look :entity"
# The command "scrutinize chair" will be translated to "look chair"

Parameters:

  • command (String)

    The format of the original command

  • translation (String)

    The format of the translated command

Returns:

  • (Syntax)

    the Syntax object



93
94
95
# File 'lib/gamefic/scriptable/actions.rb', line 93

def interpret command, translation
  rulebook.calls.add_syntax Syntax.new(command, translation)
end

#meta(verb, *args) {|| ... } ⇒ Symbol

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.

Examples:

A simple meta Response

meta :credits do |actor|
  actor.tell "This game was written by John Smith."
end

Parameters:

  • verb (Symbol)

    An imperative verb for the command

  • args (Array<Object>)

    Filters for the command’s tokens

Yield Parameters:

Returns:

  • (Symbol)


52
53
54
55
# 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

#respond(verb, *args) {|| ... } ⇒ Symbol

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).

Examples:

A simple Response.

respond :wave do |actor|
  actor.tell "Hello!"
end
# The command "wave" will respond "Hello!"

A Response that accepts a Character

respond :salute, available(Character) do |actor, character|
  actor.tell "#{The character} returns your salute."
end

Parameters:

  • verb (Symbol)

    An imperative verb for the command

  • args (Array<Object>)

    Filters for the command’s tokens

Yield Parameters:

Returns:

  • (Symbol)


32
33
34
35
# 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

#synonymsArray<Symbol>

Synonyms are a combination of the rulebook’s concrete verbs plus the alternative variants defined in syntaxes.

Examples:

class MyPlot < Gamefic::Plot
    respond :think { |actor| actor.tell 'You think.' }
    interpret 'ponder', 'think'

    verbs #=> [:think]
    synonyms #=> [:think, :ponder]
  end
end

Returns:



127
128
129
# File 'lib/gamefic/scriptable/actions.rb', line 127

def synonyms
  rulebook.synonyms
end

#syntaxesArray<Syntax>

Returns:



132
133
134
# File 'lib/gamefic/scriptable/actions.rb', line 132

def syntaxes
  rulebook.syntaxes
end

#verbsArray<Symbol>

Verbs are the symbols that have responses defined in the rulebook.

Examples:

class MyPlot < Gamefic::Plot
  script do
    respond :think { |actor| actor.tell 'You think.' }

    verbs #=> [:think]
  end
end

Returns:



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

def verbs
  rulebook.verbs
end