Class: Gamefic::Plot

Inherits:
Object
  • Object
show all
Includes:
Stage
Defined in:
lib/gamefic/plot.rb

Defined Under Namespace

Modules: ArticleMount, CommandMount, EntityMount, QueryMount, SceneMount, Snapshot, YouMount

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Stage

#stage

Constructor Details

#initialize(source = nil) ⇒ Plot

Returns a new instance of Plot.

Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gamefic/plot.rb', line 36

def initialize(source = nil)
  @source = source || Source::Text.new({})
  @commands = {}
  @syntaxes = []
  @ready_procs = []
  @update_procs = []
  @player_ready = []
  @player_procs = []
  @working_scripts = []
  @imported_scripts = []
  @entities = []
  @players = []
  @asserts = {}
  @default_scene = :active
  @subplots = []
  post_initialize
end

Instance Attribute Details

#assertsObject (readonly)

Returns the value of attribute asserts.



21
22
23
# File 'lib/gamefic/plot.rb', line 21

def asserts
  @asserts
end

#commandsObject (readonly)

Returns the value of attribute commands.



21
22
23
# File 'lib/gamefic/plot.rb', line 21

def commands
  @commands
end

#default_sceneObject

TODO Metadata could use better protection



23
24
25
# File 'lib/gamefic/plot.rb', line 23

def default_scene
  @default_scene
end

#imported_scriptsArray<Script> (readonly)

Get an Array of all scripts that have been imported into the Plot.

Returns:



85
86
87
# File 'lib/gamefic/plot.rb', line 85

def imported_scripts
  @imported_scripts
end

#metadataObject

TODO Metadata could use better protection



23
24
25
# File 'lib/gamefic/plot.rb', line 23

def 
  @metadata
end

#rulesObject (readonly)

Returns the value of attribute rules.



21
22
23
# File 'lib/gamefic/plot.rb', line 21

def rules
  @rules
end

#sourceObject (readonly)

Returns the value of attribute source.



21
22
23
# File 'lib/gamefic/plot.rb', line 21

def source
  @source
end

Instance Method Details

#actionsArray<Action>

Get an Array of all Actions defined in the Plot.

Returns:



70
71
72
# File 'lib/gamefic/plot.rb', line 70

def actions
  @commands.values.flatten
end

#actions_with_verb(verb) ⇒ Array<Action>

Get an Array of all Actions associated with the specified verb.

Parameters:

  • verb (Symbol)

    The Symbol for the verb (e.g., :go or :look)

Returns:



78
79
80
# File 'lib/gamefic/plot.rb', line 78

def actions_with_verb(verb)
  @commands[verb].clone || []
end

#assert_action(name) {|The, The, The| ... } ⇒ Object

Add a Block to be executed for the given verb. If the block returns false, the Action is cancelled.

Examples:

Require the player to have a property enabled before performing the Action.

assert_action :authorize do |actor, verb, arguments|
  if actor[:can_authorize] == true
    true
  else
    actor.tell "You don't have permission to use the authorize command."
    false
  end
end

Yield Parameters:

  • The (Character)

    character performing the Action.

  • The (Symbol)

    verb associated with the Action.

  • The (Array)

    arguments that will be passed to the Action’s #execute method.



105
106
107
# File 'lib/gamefic/plot.rb', line 105

def assert_action name, &block
  @asserts[name] = Assert.new(name, &block)
end

#concluded?(actor) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/gamefic/plot.rb', line 63

def concluded?(actor)
  scenes[actor.scene].kind_of?(Scene::Conclusion)
end

#entitiesArray<Entity>

Get an Array of the Plot’s current Entities.

Returns:



116
117
118
# File 'lib/gamefic/plot.rb', line 116

def entities
  @entities.clone
end

#introduce(player) ⇒ Object

Introduce a player to the game. This method is typically called by the Engine that manages game execution.



171
172
173
174
175
176
# File 'lib/gamefic/plot.rb', line 171

def introduce(player)
  player.extend Subplot::Feature
  player.cue :active
  @players.push player
  @introduction.call(player) unless @introduction.nil?
end

#introduction {|| ... } ⇒ Object

Add a block to be executed when a player is added to the game. Each Plot can only have one introduction. Subsequent calls will overwrite the existing one.

Examples:

Welcome the player to the game

introduction do |actor|
  actor.tell "Welcome to the game!"
end

Yield Parameters:



165
166
167
# File 'lib/gamefic/plot.rb', line 165

def introduction (&proc)
  @introduction = proc
end

#on_player_ready {|| ... } ⇒ Object

Add a block to be executed for each player when the Plot prepares them for the next turn in the game.

Yield Parameters:



234
235
236
# File 'lib/gamefic/plot.rb', line 234

def on_player_ready &block
  @player_ready.push block
end

#on_player_update {|| ... } ⇒ Object

Add a block to be executed for each player after they have completed a turn in the game.

Yield Parameters:



242
243
244
# File 'lib/gamefic/plot.rb', line 242

def on_player_update &block
  @player_procs.push block
end

#on_ready(&block) ⇒ Object

Add a block to be executed on preparation of every turn. Each on_ready block is executed once per turn, as opposed to on_player_ready blocks, which are executed once for each player.

Examples:

Increment a turn counter

turn = 0
on_ready do
  turn += 1
end


144
145
146
# File 'lib/gamefic/plot.rb', line 144

def on_ready(&block)
  @ready_procs.push block
end

#on_update(&block) ⇒ Object

Add a block to be executed after the Plot is finished updating a turn. Each on_update block is executed once per turn, as opposed to on_player_update blocks, which are executed once for each player.



151
152
153
# File 'lib/gamefic/plot.rb', line 151

def on_update(&block)
  @update_procs.push block
end

#playersArray<Character>

Get an Array of current players.

Returns:



130
131
132
# File 'lib/gamefic/plot.rb', line 130

def players
  @players.clone
end

#post_initializeObject



109
110
111
# File 'lib/gamefic/plot.rb', line 109

def post_initialize
  # TODO: Should this method be required by extended classes?
end

#readyObject

Prepare the Plot for the next turn of gameplay. This method is typically called by the Engine that manages game execution.



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/gamefic/plot.rb', line 180

def ready
  @ready_procs.each { |p| p.call }
  # Prepare player scenes for the update.
  @players.each { |player|
    this_scene = player.next_scene || player.scene
    player.prepare nil
    player.cue this_scene unless player.scene == this_scene
    @player_ready.each { |block|
      block.call player
    }
  }
end

#scenesObject



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

def scenes
  if @scenes.nil?
    @scenes = {}
    @scenes[:active] = Scene::Active.new
    @scenes[:concluded] = Scene::Conclusion.new
  end
  @scenes
end

#script(path) ⇒ Boolean

Load a script into the current Plot. This method is similar to Kernel#require, except that the script is evaluated within the Plot’s context via #stage.

Parameters:

  • path (String)

    The path to the script being evaluated

Returns:

  • (Boolean)

    true if the script was loaded by this call or false if it was already loaded.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/gamefic/plot.rb', line 214

def script path
  imported_script = source.export(path)
  if imported_script.nil?
    raise "Script not found: #{path}"
  end
  if !@working_scripts.include?(imported_script) and !imported_scripts.include?(imported_script)
    @working_scripts.push imported_script
    stage imported_script.read, imported_script.absolute_path
    @working_scripts.pop
    imported_scripts.push imported_script
    true
  else
    false
  end
end

#syntaxesArray<Syntax>

Get an Array of the Plot’s current Syntaxes.

Returns:



123
124
125
# File 'lib/gamefic/plot.rb', line 123

def syntaxes
  @syntaxes.clone
end

#tell(entities, message, refresh = false) ⇒ Object



202
203
204
205
206
# File 'lib/gamefic/plot.rb', line 202

def tell entities, message, refresh = false
  entities.each { |entity|
    entity.tell message, refresh
  }
end

#updateObject

Update the Plot’s current turn of gameplay. This method is typically called by the Engine that manages game execution.



195
196
197
198
199
200
# File 'lib/gamefic/plot.rb', line 195

def update
  @players.each { |p| process_input p }
  @entities.each { |e| e.update }
  @players.each { |player| update_player player }
  @update_procs.each { |p| p.call }
end