Class: Gamefic::Plot::Playbook

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

Instance Method Summary collapse

Constructor Details

#initialize(commands: {}, syntaxes: [], validators: [], disambiguator: nil) ⇒ Playbook



5
6
7
8
9
10
# File 'lib/gamefic/plot/playbook.rb', line 5

def initialize commands: {}, syntaxes: [], validators: [], disambiguator: nil
  @commands = commands
  @syntaxes = syntaxes
  @validators = validators
  @disambiguator = disambiguator
end

Instance Method Details

#actionsObject



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

def actions
  @commands.values.flatten
end

#actions_for(verb) ⇒ Array<Action>

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



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

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

#disambiguate(&block) ⇒ Object



38
39
40
41
42
# File 'lib/gamefic/plot/playbook.rb', line 38

def disambiguate &block
  @disambiguator = Action.new(nil, Query::Base.new, &block)
  @disambiguator.meta = true
  @disambiguator
end

#disambiguatorObject



28
29
30
31
32
33
34
35
36
# File 'lib/gamefic/plot/playbook.rb', line 28

def disambiguator
  @disambiguator ||= Action.new(nil, Query::Base.new) do |actor, entities|
    definites = []
    entities.each { |entity|
      definites.push entity.definitely
    }
    actor.tell "I don't know which you mean: #{definites.join_or}."
  end
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.



130
131
132
# File 'lib/gamefic/plot/playbook.rb', line 130

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

#freezeObject



134
135
136
137
# File 'lib/gamefic/plot/playbook.rb', line 134

def freeze
  @commands.freeze
  @syntaxes.freeze
end

#interpret(*args) ⇒ 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"


119
120
121
122
123
# File 'lib/gamefic/plot/playbook.rb', line 119

def interpret(*args)
  syn = Syntax.new(*args)
  add_syntax syn
  syn
end

#meta(command, *queries) {|| ... } ⇒ Object

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

Yield Parameters:



99
100
101
102
103
# File 'lib/gamefic/plot/playbook.rb', line 99

def meta(command, *queries, &proc)
  act = respond(command, *queries, &proc)
  act.meta = true
  act
end

#respond(command, *queries) {|| ... } ⇒ Object

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

Yield Parameters:



78
79
80
81
82
# File 'lib/gamefic/plot/playbook.rb', line 78

def respond(command, *queries, &proc)
  act = Action.new(command, *queries, &proc)
  add_action act
  act
end

#syntaxesObject



12
13
14
# File 'lib/gamefic/plot/playbook.rb', line 12

def syntaxes
  @syntaxes
end

#validate(&block) ⇒ Object



44
45
46
# File 'lib/gamefic/plot/playbook.rb', line 44

def validate &block
  @validators.push block
end

#validatorsObject



24
25
26
# File 'lib/gamefic/plot/playbook.rb', line 24

def validators
  @validators
end

#verbsObject



20
21
22
# File 'lib/gamefic/plot/playbook.rb', line 20

def verbs
  @commands.keys
end