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

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

Methods included from Proxy

#proxy, #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:



77
78
79
# File 'lib/gamefic/scriptable/actions.rb', line 77

def after_action *verbs, &block
  rulebook.hooks.after_action *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:



66
67
68
# File 'lib/gamefic/scriptable/actions.rb', line 66

def before_action *verbs, &block
  rulebook.hooks.before_action *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



95
96
97
# File 'lib/gamefic/scriptable/actions.rb', line 95

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

#meta(verb, *queries) {|| ... } ⇒ 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

  • queries (Array<Query::Base, Query::Text>)

    Filters for the command’s tokens

Yield Parameters:

Returns:

  • (Symbol)


53
54
55
56
57
# File 'lib/gamefic/scriptable/actions.rb', line 53

def meta(verb, *queries, &proc)
  args = map_response_args(queries)
  rulebook.calls.add_response Response.new(verb, rulebook.narrative, *args, meta: true, &proc)
  verb
end

#respond(verb, *queries) {|| ... } ⇒ 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

  • queries (Array<Query::Base, Query::Text>)

    Filters for the command’s tokens

Yield Parameters:

Returns:

  • (Symbol)


32
33
34
35
36
# File 'lib/gamefic/scriptable/actions.rb', line 32

def respond(verb, *queries, &proc)
  args = map_response_args(queries)
  rulebook.calls.add_response Response.new(verb, rulebook.narrative, *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:



129
130
131
# File 'lib/gamefic/scriptable/actions.rb', line 129

def synonyms
  rulebook.synonyms
end

#syntaxesArray<Syntax>

Returns:



134
135
136
# File 'lib/gamefic/scriptable/actions.rb', line 134

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:



111
112
113
# File 'lib/gamefic/scriptable/actions.rb', line 111

def verbs
  rulebook.verbs
end