Module: Gamefic::Active
- Included in:
- Actor
- Defined in:
- lib/gamefic/active.rb,
lib/gamefic/active/cue.rb,
lib/gamefic/active/messaging.rb,
lib/gamefic/active/narratives.rb
Overview
The Active module gives entities the ability to perform actions and participate in scenes. The Actor class, for example, is an Entity subclass that includes this module.
Defined Under Namespace
Modules: Messaging Classes: Cue, Narratives
Instance Attribute Summary collapse
-
#last_cue ⇒ Cue?
readonly
The most recently started cue.
-
#next_cue ⇒ Cue?
readonly
The cue that will be started on the next turn.
Instance Method Summary collapse
- #accessible ⇒ Object
-
#acting? ⇒ Boolean
True if the actor performed a command this turn.
-
#can?(verb) ⇒ Boolean
True if the actor can perform the verb (i.e., an active narrative understands it).
-
#concluding? ⇒ Boolean
True if the actor is ready to leave the game.
-
#cue(scene, **context) ⇒ Cue
(also: #prepare)
Cue a scene to start in the next turn.
-
#execute(verb, *params) ⇒ Command?
Perform an action.
-
#last_input ⇒ String?
The input from the last finished cue.
-
#narratives ⇒ Narratives
The narratives in which the entity is participating.
-
#output ⇒ Props::Output
Data that will be sent to the user.
-
#participating? ⇒ Boolean
True if the actor is participating in any narratives.
-
#perform(input) ⇒ Command?
Perform a command.
-
#proceed ⇒ Action?
Proceed to the next Action in the current stack.
-
#queue ⇒ Array<String>
An array of commands waiting to be executed.
-
#quietly(input) ⇒ String
Quietly perform a command.
-
#recue ⇒ Cue?
Restart the scene from the most recent cue.
-
#rotate_cue ⇒ Cue?
Move next_cue into last_cue.
Methods included from Messaging
#buffer, #flush, #messages, #messenger, #stream, #tell
Methods included from Logging
Instance Attribute Details
#last_cue ⇒ Cue? (readonly)
The most recently started cue.
20 21 22 |
# File 'lib/gamefic/active.rb', line 20 def last_cue @last_cue end |
#next_cue ⇒ Cue? (readonly)
The cue that will be started on the next turn.
25 26 27 |
# File 'lib/gamefic/active.rb', line 25 def next_cue @next_cue end |
Instance Method Details
#accessible ⇒ Object
159 160 161 |
# File 'lib/gamefic/active.rb', line 159 def accessible [] end |
#acting? ⇒ Boolean
True if the actor performed a command this turn. False if the actor has not performed a command yet or has only performed meta commands.
191 192 193 |
# File 'lib/gamefic/active.rb', line 191 def acting? @acting ||= false end |
#can?(verb) ⇒ Boolean
True if the actor can perform the verb (i.e., an active narrative understands it).
173 174 175 |
# File 'lib/gamefic/active.rb', line 173 def can?(verb) narratives.understand?(verb) end |
#concluding? ⇒ Boolean
True if the actor is ready to leave the game.
155 156 157 |
# File 'lib/gamefic/active.rb', line 155 def concluding? narratives.empty? || last_cue&.type == 'Conclusion' end |
#cue(scene, **context) ⇒ Cue Also known as: prepare
Cue a scene to start in the next turn.
137 138 139 140 141 142 143 |
# File 'lib/gamefic/active.rb', line 137 def cue scene, **context return @next_cue if @next_cue&.key == scene && @next_cue&.context == context logger.debug "Overwriting existing cue `#{@next_cue.key}` with `#{scene}`" if @next_cue @next_cue = Cue.new(self, scene, current, **context) end |
#execute(verb, *params) ⇒ Command?
Perform an action. This is functionally identical to the ‘perform` method, except the action must be declared as a verb with a list of arguments. Use `perform` if you need to parse a string as a command.
The command will be executed immediately, regardless of the entity’s state.
94 95 96 97 98 99 100 |
# File 'lib/gamefic/active.rb', line 94 def execute(verb, *params) dispatchers.push Dispatcher.new(Order.new(self, verb, params)) dispatchers.last.execute.tap do |command| dispatchers.pop @acting = true if command&.active? end end |
#last_input ⇒ String?
The input from the last finished cue.
198 199 200 |
# File 'lib/gamefic/active.rb', line 198 def last_input output.last_input end |
#narratives ⇒ Narratives
The narratives in which the entity is participating.
30 31 32 |
# File 'lib/gamefic/active.rb', line 30 def narratives @narratives ||= Narratives.new end |
#output ⇒ Props::Output
Data that will be sent to the user. The output is typically sent after a scene has started and before the user is prompted for input.
The output object attached to the actor is always frozen. Authors should use on_player_output blocks to modify output to be sent to the user.
48 49 50 |
# File 'lib/gamefic/active.rb', line 48 def output last_cue&.output || Props::Output::EMPTY end |
#participating? ⇒ Boolean
True if the actor is participating in any narratives.
165 166 167 |
# File 'lib/gamefic/active.rb', line 165 def participating? !narratives.empty? end |
#perform(input) ⇒ Command?
Perform a command.
The command’s action will be executed immediately, regardless of the entity’s state.
62 63 64 65 66 67 68 |
# File 'lib/gamefic/active.rb', line 62 def perform(input) dispatchers.push Dispatcher.new(Request.new(self, input)) dispatchers.last.execute.tap do |command| dispatchers.pop @acting = true if command&.active? end end |
#proceed ⇒ Action?
Proceed to the next Action in the current stack. This method is typically used in Action blocks to cascade through multiple implementations of the same verb.
126 127 128 |
# File 'lib/gamefic/active.rb', line 126 def proceed dispatchers.last&.proceed end |
#queue ⇒ Array<String>
An array of commands waiting to be executed.
37 38 39 |
# File 'lib/gamefic/active.rb', line 37 def queue @queue ||= [] end |
#quietly(input) ⇒ String
Quietly perform a command. This method executes the command exactly as #perform does, except it buffers the resulting output instead of sending it to messages.
76 77 78 |
# File 'lib/gamefic/active.rb', line 76 def quietly(input) messenger.buffer { perform input } end |
#recue ⇒ Cue?
Restart the scene from the most recent cue.
149 150 151 |
# File 'lib/gamefic/active.rb', line 149 def recue (@next_cue = @last_cue&.restart) || warn_nil('No scene to recue') end |
#rotate_cue ⇒ Cue?
Move next_cue into last_cue. This method is typically called by the narrator at the start of a turn. It returns the last cue.
181 182 183 184 185 186 |
# File 'lib/gamefic/active.rb', line 181 def rotate_cue @acting = false @last_cue = @next_cue @next_cue = nil @last_cue end |