Class: Gamefic::Plot::Playbook
- Inherits:
-
Object
- Object
- Gamefic::Plot::Playbook
- Defined in:
- lib/gamefic/plot/playbook.rb
Instance Method Summary collapse
- #actions ⇒ Object
-
#actions_for(verb) ⇒ Array<Action>
Get an Array of all Actions associated with the specified verb.
- #disambiguate(&block) ⇒ Object
- #disambiguator ⇒ Object
-
#dup ⇒ Playbook
Duplicate the playbook.
- #freeze ⇒ Object
-
#initialize(commands: {}, syntaxes: [], validators: [], disambiguator: nil) ⇒ Playbook
constructor
A new instance of Playbook.
-
#interpret(*args) ⇒ Syntax
Create an alternate Syntax for an Action.
-
#meta(command, *queries) {|| ... } ⇒ Object
Create a Meta Action that responds to a command.
-
#respond(command, *queries) {|| ... } ⇒ Object
Create an Action that responds to a command.
- #syntaxes ⇒ Object
- #validate(&block) ⇒ Object
- #validators ⇒ Object
- #verbs ⇒ Object
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
#actions ⇒ Object
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. = true @disambiguator end |
#disambiguator ⇒ Object
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 |
#dup ⇒ Playbook
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 |
#freeze ⇒ Object
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.
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.
99 100 101 102 103 |
# File 'lib/gamefic/plot/playbook.rb', line 99 def (command, *queries, &proc) act = respond(command, *queries, &proc) act. = 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).
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 |
#syntaxes ⇒ Object
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 |
#validators ⇒ Object
24 25 26 |
# File 'lib/gamefic/plot/playbook.rb', line 24 def validators @validators end |
#verbs ⇒ Object
20 21 22 |
# File 'lib/gamefic/plot/playbook.rb', line 20 def verbs @commands.keys end |