Class: Gamefic::World::Playbook

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/world/playbook.rb

Overview

A collection of rules for performing commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands: {}, syntaxes: [], before_actions: [], after_actions: []) ⇒ Playbook

Returns a new instance of Playbook.

Parameters:

  • commands (Hash) (defaults to: {})
  • syntaxes (Array<Syntax>, Set<Syntax>) (defaults to: [])
  • before_actions (Array) (defaults to: [])
  • after_actions (Array) (defaults to: [])


27
28
29
30
31
32
33
# File 'lib/gamefic/world/playbook.rb', line 27

def initialize commands: {}, syntaxes: [], before_actions: [], after_actions: []
  @commands = commands
  @syntax_set = syntaxes.to_set
  sort_syntaxes
  @before_actions = before_actions
  @after_actions = after_actions
end

Instance Attribute Details

#after_actionsArray<Proc> (readonly)

An array of blocks to execute after actions.

Returns:



21
22
23
# File 'lib/gamefic/world/playbook.rb', line 21

def after_actions
  @after_actions
end

#before_actionsArray<Proc> (readonly)

An array of blocks to execute before actions.

Returns:



16
17
18
# File 'lib/gamefic/world/playbook.rb', line 16

def before_actions
  @before_actions
end

#syntaxesArray<Gamefic::Syntax> (readonly)

An array of available syntaxes.

Returns:



11
12
13
# File 'lib/gamefic/world/playbook.rb', line 11

def syntaxes
  @syntaxes
end

Instance Method Details

#actionsArray<Gamefic::Action>

An array of available actions.

Returns:



38
39
40
# File 'lib/gamefic/world/playbook.rb', line 38

def actions
  @commands.values.flatten
end

#actions_for(verb) ⇒ Array<Class<Action>>

Get an Array of all Actions associated with the specified verb.

Parameters:

  • verb (Symbol)

    The Symbol for the verb (e.g., :go or :look)

Returns:



68
69
70
# File 'lib/gamefic/world/playbook.rb', line 68

def actions_for verb
  @commands[verb] || []
end

#after_action {|| ... } ⇒ Object

Add a proc to be evaluated after a character executes an action.

Yield Parameters:



60
61
62
# File 'lib/gamefic/world/playbook.rb', line 60

def after_action &block
  @after_actions.push block
end

#before_action {|| ... } ⇒ Object Also known as: validate

Add a proc to be evaluated before a character executes an action.

Yield Parameters:



52
53
54
# File 'lib/gamefic/world/playbook.rb', line 52

def before_action &block
  @before_actions.push block
end

#dispatch(actor, text) ⇒ Dispatcher

Get a Dispatcher to select actions that can potentially be executed from the specified command string.

Parameters:

Returns:



149
150
151
152
153
# File 'lib/gamefic/world/playbook.rb', line 149

def dispatch(actor, text)
  commands = Syntax.tokenize(text, actor.syntaxes)
  actions = commands.flat_map { |cmd| actions_for(cmd.verb).reject(&:hidden?) }
  Dispatcher.new(actor, commands, sort_and_reduce_actions(actions))
end

#dispatch_from_params(actor, verb, params) ⇒ Array<Gamefic::Action>

Get an array of actions, derived from the specified verb and params, that the actor can potentially execute.

Returns:



159
160
161
162
# File 'lib/gamefic/world/playbook.rb', line 159

def dispatch_from_params actor, verb, params
  available = actions_for(verb)
  Dispatcher.new(actor, [Command.new(verb, params)], sort_and_reduce_actions(available))
end

#dupPlaybook

Duplicate the playbook. This method will duplicate the commands hash and the syntax array so the new playbook can be modified without affecting the original.

Returns:



169
170
171
# File 'lib/gamefic/world/playbook.rb', line 169

def dup
  Playbook.new commands: @commands.dup, syntaxes: @syntaxes.dup
end

#freezeObject



173
174
175
176
# File 'lib/gamefic/world/playbook.rb', line 173

def freeze
  @commands.freeze
  @syntaxes.freeze
end

#interpret(input, translation) ⇒ Syntax

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

Examples:

Create a synonym for the Inventory Action.

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

Create a parameterized synonym for the Look Action.

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

Parameters:

  • input (String)

    The format of the original command

  • translation (String)

    The format of the translated command

Returns:

  • (Syntax)

    the Syntax object



137
138
139
140
141
# File 'lib/gamefic/world/playbook.rb', line 137

def interpret(input, translation)
  syn = Syntax.new(input, translation)
  add_syntax syn
  syn
end

#meta(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>

Create a Meta Action that responds to a command. Meta Actions are very similar to standard Actions, except the Plot understands them to be commands that operate above and/or outside of the actual game world. Examples of Meta Actions are commands that report the player’s current score, save and restore saved games, or list the game’s credits.

Examples:

A simple Meta Action

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

    Filters for the command’s tokens

Yield Parameters:

Returns:



117
118
119
120
121
# File 'lib/gamefic/world/playbook.rb', line 117

def meta(verb, *queries, &proc)
  act = Action.subclass verb, *queries, meta: true, &proc
  add_action act
  act
end

#respond(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>

Create an Action that responds to a command. An Action uses the command 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 contains the code to be executed when the input matches all of the Action’s criteria (i.e., verb and queries).

Examples:

A simple Action.

respond :salute do |actor|
  actor.tell "Hello, sir!"
end
# The command "salute" will respond "Hello, sir!"

An Action that accepts a Character

respond :salute, Use.visible(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>)

    Filters for the command’s tokens

Yield Parameters:

Returns:



95
96
97
98
99
# File 'lib/gamefic/world/playbook.rb', line 95

def respond(verb, *queries, &proc)
  act = Action.subclass verb, *queries, &proc
  add_action act
  act
end

#verbsArray<Symbol>

An array of recognized verbs.

Returns:



45
46
47
# File 'lib/gamefic/world/playbook.rb', line 45

def verbs
  @commands.keys
end