Class: Gamefic::Character
- Includes:
- State
- Defined in:
- lib/gamefic/character.rb,
lib/gamefic/character/state.rb
Defined Under Namespace
Modules: State
Constant Summary
Constants included from Matchable
Instance Attribute Summary collapse
- #last_action ⇒ Gamefic::Action readonly
- #last_object ⇒ Entity? readonly
-
#next_scene ⇒ Object
readonly
Returns the value of attribute next_scene.
-
#object_of_pronoun ⇒ Object
Returns the value of attribute object_of_pronoun.
-
#playbook ⇒ Object
Returns the value of attribute playbook.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#scene ⇒ Object
readonly
Returns the value of attribute scene.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Attributes inherited from Entity
Attributes included from Grammar::Plural
Attributes included from Grammar::Person
Attributes included from Grammar::Gender
Attributes included from Describable
#definite_article, #indefinite_article, #name, #synonyms
Instance Method Summary collapse
-
#accessible? ⇒ String
Get the prompt that the user should see for the current scene.
-
#conclude(scene) ⇒ Object
Cue a conclusion.
-
#concluded? ⇒ Boolean
True if the character is in a conclusion.
-
#connect(user) ⇒ Object
Connect a User.
-
#cue(new_scene) ⇒ Object
Immediately start a new scene for the character.
-
#disconnect ⇒ Object
Disconnect the current User.
-
#initialize(args = {}) ⇒ Character
constructor
A new instance of Character.
- #inspect ⇒ Object
-
#perform(*command) ⇒ Object
Perform a command.
- #performed(order) ⇒ Object
-
#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.
-
#quietly(*command) ⇒ String
Quietly perform a command.
-
#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.
Methods included from State
Methods inherited from Entity
#[], #[]=, #parent=, #post_initialize, #pre_initialize, #uid, #update
Methods included from Grammar::WordAdapter
Methods included from Grammar::Plural
Methods included from Messaging
Methods included from Describable
default_description, default_description=, #definitely, #description, #description=, #has_description?, #indefinitely, #keywords, #proper_named=, #proper_named?, #to_s
Methods included from Matchable
Methods included from Node
#children, #flatten, #parent, #parent=
Constructor Details
#initialize(args = {}) ⇒ Character
23 24 25 26 27 28 29 |
# File 'lib/gamefic/character.rb', line 23 def initialize(args = {}) super @queue = Array.new = '' @buffer_stack = 0 @buffer = "" end |
Instance Attribute Details
#last_action ⇒ Gamefic::Action (readonly)
13 14 15 |
# File 'lib/gamefic/character.rb', line 13 def last_action @last_action end |
#last_object ⇒ Entity? (readonly)
15 16 17 |
# File 'lib/gamefic/character.rb', line 15 def last_object @last_object end |
#next_scene ⇒ Object (readonly)
Returns the value of attribute next_scene.
18 19 20 |
# File 'lib/gamefic/character.rb', line 18 def next_scene @next_scene end |
#object_of_pronoun ⇒ Object
Returns the value of attribute object_of_pronoun.
16 17 18 |
# File 'lib/gamefic/character.rb', line 16 def object_of_pronoun @object_of_pronoun end |
#playbook ⇒ Object
Returns the value of attribute playbook.
19 20 21 |
# File 'lib/gamefic/character.rb', line 19 def playbook @playbook end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
11 12 13 |
# File 'lib/gamefic/character.rb', line 11 def queue @queue end |
#scene ⇒ Object (readonly)
Returns the value of attribute scene.
17 18 19 |
# File 'lib/gamefic/character.rb', line 17 def scene @scene end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
11 12 13 |
# File 'lib/gamefic/character.rb', line 11 def user @user end |
Instance Method Details
#accessible? ⇒ String
Get the prompt that the user should see for the current scene.
def prompt
scene.nil? ? '>' : scene.prompt
end
213 214 215 |
# File 'lib/gamefic/character.rb', line 213 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.
189 190 191 192 |
# File 'lib/gamefic/character.rb', line 189 def conclude scene raise NotConclusionError unless scene <= Scene::Conclusion cue scene end |
#concluded? ⇒ Boolean
True if the character is in a conclusion.
197 198 199 |
# File 'lib/gamefic/character.rb', line 197 def concluded? !scene.nil? and scene.kind_of?(Scene::Conclusion) end |
#connect(user) ⇒ Object
Connect a User.
34 35 36 |
# File 'lib/gamefic/character.rb', line 34 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.
161 162 163 164 165 166 167 168 169 |
# File 'lib/gamefic/character.rb', line 161 def cue new_scene @next_scene = nil if new_scene.nil? @scene = nil else @scene = new_scene.new(self) @scene.start end end |
#disconnect ⇒ Object
Disconnect the current User.
40 41 42 |
# File 'lib/gamefic/character.rb', line 40 def disconnect @user = nil end |
#inspect ⇒ Object
217 218 219 |
# File 'lib/gamefic/character.rb', line 217 def inspect to_s end |
#perform(*command) ⇒ Object
Perform a command. The command can be specified as a String or a set of tokens. Either form should yield the same result, but using tokens can yield better performance since it bypasses the parser.
The command will be executed immediately regardless of game state.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gamefic/character.rb', line 82 def perform(*command) #Director.dispatch(self, *command) actions = playbook.dispatch(self, *command) a = actions.first okay = true unless a. playbook.validators.each { |v| result = v.call(self, a.verb, a.parameters) okay = (result != false) break if not okay } end if okay performance_stack.push actions proceed performance_stack.pop end a end |
#performed(order) ⇒ Object
201 202 203 204 |
# File 'lib/gamefic/character.rb', line 201 def performed order order.freeze @last_action = order end |
#prepare(s) ⇒ Object
Prepare a scene to be started for this character at the beginning of the next turn.
174 175 176 |
# File 'lib/gamefic/character.rb', line 174 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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/gamefic/character.rb', line 138 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 |
#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.
107 108 109 110 111 112 113 114 115 |
# File 'lib/gamefic/character.rb', line 107 def quietly(*command) if @buffer_stack == 0 @buffer = "" end @buffer_stack += 1 self.perform *command @buffer_stack -= 1 @buffer 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.
61 62 63 64 65 66 67 |
# File 'lib/gamefic/character.rb', line 61 def stream() if @buffer_stack > 0 @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.
49 50 51 52 53 54 55 |
# File 'lib/gamefic/character.rb', line 49 def tell() if @buffer_stack > 0 @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.
182 183 184 |
# File 'lib/gamefic/character.rb', line 182 def will_cue? scene (@scene.class == scene and @next_scene.nil?) or @next_scene == scene end |