Module: Gamefic::Active
- Included in:
- Actor
- Defined in:
- lib/gamefic/active.rb
Overview
The Active module gives entities the ability to perform actions and participate in scenes.
Instance Attribute Summary collapse
- #last_action ⇒ Gamefic::Action readonly
-
#next_scene ⇒ Object
readonly
Returns the value of attribute next_scene.
- #scene ⇒ Gamefic::Scene::Base readonly
- #user ⇒ Gamefic::User::Base readonly
Instance Method Summary collapse
- #accessible? ⇒ Boolean
-
#conclude(scene) ⇒ Object
Cue a conclusion.
-
#concluded? ⇒ Boolean
True if the character is in a conclusion.
- #connect(user) ⇒ Object
-
#cue(new_scene) ⇒ Object
Immediately start a new scene for the character.
-
#execute(verb, *params, quietly: false) ⇒ Gamefic::Action
Perform an action.
- #inspect ⇒ Object
-
#perform(*command) ⇒ Gamefic::Action
Perform a command.
- #performed(order) ⇒ Object
- #playbooks ⇒ Array<Gamefic::Plot::Playbook>
-
#prepare(s) ⇒ Object
Prepare a scene to be started for this character at the beginning of the next turn.
-
#proceed(quietly: false) ⇒ Object
Proceed to the next Action in the current stack.
-
#queue ⇒ Array<String>
An array of actions waiting to be performed.
-
#quietly(*command) ⇒ String
Quietly perform a command.
-
#state ⇒ Hash
A hash of values representing the state of a performing entity.
-
#stream(message) ⇒ Object
Send a message to the Character as raw text.
-
#tell(message) ⇒ Object
Send a message to the entity.
-
#will_cue?(scene) ⇒ Boolean
Return true if the character is expected to be in the specified scene on the next turn.
Instance Attribute Details
#last_action ⇒ Gamefic::Action (readonly)
10 11 12 |
# File 'lib/gamefic/active.rb', line 10 def last_action @last_action end |
#next_scene ⇒ Object (readonly)
Returns the value of attribute next_scene.
18 19 20 |
# File 'lib/gamefic/active.rb', line 18 def next_scene @next_scene end |
#scene ⇒ Gamefic::Scene::Base (readonly)
16 17 18 |
# File 'lib/gamefic/active.rb', line 16 def scene @scene end |
#user ⇒ Gamefic::User::Base (readonly)
13 14 15 |
# File 'lib/gamefic/active.rb', line 13 def user @user end |
Instance Method Details
#accessible? ⇒ Boolean
218 219 220 |
# File 'lib/gamefic/active.rb', line 218 def accessible? false end |
#conclude(scene) ⇒ Object
Cue a conclusion. This method works like #cue, except it will raise a NotConclusionError if the scene is not a Scene::Conclusion.
201 202 203 204 |
# File 'lib/gamefic/active.rb', line 201 def conclude scene raise NotConclusionError unless scene <= Scene::Conclusion cue scene end |
#concluded? ⇒ Boolean
True if the character is in a conclusion.
209 210 211 |
# File 'lib/gamefic/active.rb', line 209 def concluded? !scene.nil? and scene.kind_of?(Scene::Conclusion) end |
#connect(user) ⇒ Object
28 29 30 |
# File 'lib/gamefic/active.rb', line 28 def connect user @user = user end |
#cue(new_scene) ⇒ Object
Immediately start a new scene for the character. Use #prepare if you want to declare a scene to be started at the beginning of the next turn.
173 174 175 176 177 178 179 180 181 |
# File 'lib/gamefic/active.rb', line 173 def cue new_scene @next_scene = nil if new_scene.nil? @scene = nil else @scene = new_scene.new(self) @scene.start end end |
#execute(verb, *params, quietly: false) ⇒ Gamefic::Action
Perform an action. This is functionally identical to the ‘perform` method, except the action must be declared as a verb with a list of parameters. Use `perform` if you need to parse a string as a command.
The command will be executed immediately regardless of the entity’s state.
123 124 125 126 127 |
# File 'lib/gamefic/active.rb', line 123 def execute(verb, *params, quietly: false) actions = [] playbooks.reverse.each { |p| actions.concat p.dispatch_from_params(self, verb, params) } execute_stack actions, quietly: quietly end |
#inspect ⇒ Object
222 223 224 |
# File 'lib/gamefic/active.rb', line 222 def inspect to_s end |
#perform(*command) ⇒ Gamefic::Action
Perform a command. The command can be specified as a String or a verb with a list of parameters. Either form should yield the same result, but the verb/parameter form can yield better performance since it bypasses the parser.
The command will be executed immediately regardless of the entity’s state.
90 91 92 93 94 |
# File 'lib/gamefic/active.rb', line 90 def perform(*command) actions = [] playbooks.reverse.each { |p| actions.concat p.dispatch(self, *command) } execute_stack actions end |
#performed(order) ⇒ Object
213 214 215 216 |
# File 'lib/gamefic/active.rb', line 213 def performed order order.freeze @last_action = order end |
#playbooks ⇒ Array<Gamefic::Plot::Playbook>
24 25 26 |
# File 'lib/gamefic/active.rb', line 24 def playbooks @playbooks ||= [] end |
#prepare(s) ⇒ Object
Prepare a scene to be started for this character at the beginning of the next turn.
186 187 188 |
# File 'lib/gamefic/active.rb', line 186 def prepare s @next_scene = s end |
#proceed(quietly: false) ⇒ Object
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.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gamefic/active.rb', line 150 def proceed quietly: false #Director::Delegate.proceed_for self return if performance_stack.empty? a = performance_stack.last.shift unless a.nil? if quietly if @buffer_stack == 0 @buffer = "" end @buffer_stack += 1 end a.execute if quietly @buffer_stack -= 1 @buffer end end end |
#queue ⇒ Array<String>
An array of actions waiting to be performed.
35 36 37 |
# File 'lib/gamefic/active.rb', line 35 def queue @queue ||= [] end |
#quietly(*command) ⇒ 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 the user.
101 102 103 104 105 106 107 108 109 |
# File 'lib/gamefic/active.rb', line 101 def quietly(*command) if buffer_stack == 0 clear_buffer end set_buffer_stack buffer_stack + 1 self.perform *command set_buffer_stack buffer_stack - 1 buffer end |
#state ⇒ Hash
A hash of values representing the state of a performing entity.
42 43 44 45 46 47 |
# File 'lib/gamefic/active.rb', line 42 def state @state = {} @state.merge! scene.state unless scene.nil? @state[:output] = @state end |
#stream(message) ⇒ Object
Send a message to the Character as raw text. Unlike #tell, this method will not wrap the message in HTML paragraphs.
66 67 68 69 70 71 72 |
# File 'lib/gamefic/active.rb', line 66 def stream() if buffer_stack > 0 append_buffer else super end end |
#tell(message) ⇒ Object
Send a message to the entity. This method will automatically wrap the message in HTML paragraphs. To send a message without paragraph formatting, use #stream instead.
54 55 56 57 58 59 60 |
# File 'lib/gamefic/active.rb', line 54 def tell() if buffer_stack > 0 append_buffer else super end end |
#will_cue?(scene) ⇒ Boolean
Return true if the character is expected to be in the specified scene on the next turn.
194 195 196 |
# File 'lib/gamefic/active.rb', line 194 def will_cue? scene (@scene.class == scene and @next_scene.nil?) or @next_scene == scene end |