Class: Gamefic::World::Playbook
Overview
A collection of rules for performing commands.
Instance Attribute Summary collapse
-
#after_actions ⇒ Array<Proc>
readonly
An array of blocks to execute after actions.
-
#before_actions ⇒ Array<Proc>
readonly
An array of blocks to execute before actions.
-
#syntaxes ⇒ Array<Gamefic::Syntax>
readonly
An array of available syntaxes.
Instance Method Summary collapse
-
#actions ⇒ Array<Gamefic::Action>
An array of available actions.
-
#actions_for(verb) ⇒ Array<Class<Action>>
Get an Array of all Actions associated with the specified verb.
-
#after_action {|| ... } ⇒ Object
Add a proc to be evaluated after a character executes an action.
-
#before_action {|| ... } ⇒ Object
(also: #validate)
Add a proc to be evaluated before a character executes an action.
-
#dispatch(actor, text) ⇒ Dispatcher
Get a Dispatcher to select actions that can potentially be executed from the specified command string.
-
#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.
-
#dup ⇒ Playbook
Duplicate the playbook.
- #freeze ⇒ Object
-
#initialize(commands: {}, syntaxes: [], before_actions: [], after_actions: []) ⇒ Playbook
constructor
A new instance of Playbook.
-
#interpret(input, translation) ⇒ Syntax
Create an alternate Syntax for an Action.
-
#meta(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>
Create a Meta Action that responds to a command.
-
#respond(verb, *queries) {|| ... } ⇒ Class<Gamefic::Action>
Create an Action that responds to a command.
-
#verbs ⇒ Array<Symbol>
An array of recognized verbs.
Constructor Details
#initialize(commands: {}, syntaxes: [], before_actions: [], after_actions: []) ⇒ Playbook
Returns a new instance of Playbook.
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_actions ⇒ Array<Proc> (readonly)
An array of blocks to execute after actions.
21 22 23 |
# File 'lib/gamefic/world/playbook.rb', line 21 def after_actions @after_actions end |
#before_actions ⇒ Array<Proc> (readonly)
An array of blocks to execute before actions.
16 17 18 |
# File 'lib/gamefic/world/playbook.rb', line 16 def before_actions @before_actions end |
#syntaxes ⇒ Array<Gamefic::Syntax> (readonly)
An array of available syntaxes.
11 12 13 |
# File 'lib/gamefic/world/playbook.rb', line 11 def syntaxes @syntaxes end |
Instance Method Details
#actions ⇒ Array<Gamefic::Action>
An array of available actions.
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.
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.
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.
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.
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.
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 |
#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.
169 170 171 |
# File 'lib/gamefic/world/playbook.rb', line 169 def dup Playbook.new commands: @commands.dup, syntaxes: @syntaxes.dup end |
#freeze ⇒ Object
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.
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.
117 118 119 120 121 |
# File 'lib/gamefic/world/playbook.rb', line 117 def (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).
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 |