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

Instance Method Summary collapse

Instance Attribute Details

#last_actionGamefic::Action (readonly)

Returns:



10
11
12
# File 'lib/gamefic/active.rb', line 10

def last_action
  @last_action
end

#next_sceneObject (readonly)

Returns the value of attribute next_scene.



18
19
20
# File 'lib/gamefic/active.rb', line 18

def next_scene
  @next_scene
end

#sceneGamefic::Scene::Base (readonly)



16
17
18
# File 'lib/gamefic/active.rb', line 16

def scene
  @scene
end

#userGamefic::User::Base (readonly)

Returns:



13
14
15
# File 'lib/gamefic/active.rb', line 13

def user
  @user
end

Instance Method Details

#accessible?Boolean

Returns:

  • (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.

Raises:



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.

Returns:

  • (Boolean)


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.

Examples:

character.execute :take, @key

Returns:



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

#inspectObject



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.

Examples:

Send a command as a string

character.perform "take the key"

Send a command as a verb with parameters

character.perform :take, @key

Returns:



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

#playbooksArray<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.

Examples:

Proceed through two implementations of a verb

introduction do |actor|
  actor[:has_eaten] = false # Initial value
end
respond :eat do |actor|
  actor.tell "You eat something."
  actor[:has_eaten] = true
end
respond :eat do |actor|
  # This version will be executed first because it was implemented last
  if actor[:has_eaten]
    actor.tell "You already ate."
  else
    actor.proceed # Execute the previous implementation
  end
end


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

#queueArray<String>

An array of actions waiting to be performed.

Returns:



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.

Returns:

  • (String)

    The output that resulted from performing the command.



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

#stateHash

A hash of values representing the state of a performing entity.

Returns:

  • (Hash)


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] = messages
  @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.

Parameters:



66
67
68
69
70
71
72
# File 'lib/gamefic/active.rb', line 66

def stream(message)
  if buffer_stack > 0
    append_buffer message
  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.

Parameters:



54
55
56
57
58
59
60
# File 'lib/gamefic/active.rb', line 54

def tell(message)
  if buffer_stack > 0
    append_buffer message
  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.

Returns:

  • (Boolean)


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