Class: Gamefic::Plot

Inherits:
Object
  • Object
show all
Includes:
Gamefic, Articles, Callbacks, Commands, Entities, Host, Players, Scenes, Snapshot, Theater, YouMount, Tester
Defined in:
lib/gamefic/plot.rb,
lib/gamefic/plot/entities.rb,
lib/gamefic/plot/playbook.rb

Defined Under Namespace

Modules: Articles, Callbacks, Commands, Entities, Host, Players, Scenes, Snapshot, Theater, YouMount Classes: Playbook

Constant Summary

Constants included from Gamefic

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Articles

#A, #An, #The, #a, #an, #the

Methods included from YouMount

#you

Methods included from Snapshot

#restore, #save

Methods included from Host

#branch, #in_subplot?, #subplot_for, #subplots

Methods included from Callbacks

#before_player_update, #on_player_ready, #on_player_update, #on_ready, #on_update

Methods included from Tester

#on_test, #run_test, #test_procs

Methods included from Players

#players

Methods included from Scenes

#conclusion, #custom, #default_conclusion, #default_scene, #introduce, #introduction, #multiple_choice, #multiple_scene, #pause, #question, #yes_or_no

Methods included from Commands

#action, #actions, #disambiguate, #interpret, #meta, #respond, #validate, #verbs, #xlate

Methods included from Entities

#destroy, #entities, #make, #pick, #players

Methods included from Theater

#stage, #theater

Constructor Details

#initialize(source = nil) ⇒ Plot

Returns a new instance of Plot.

Parameters:



31
32
33
34
35
36
37
# File 'lib/gamefic/plot.rb', line 31

def initialize(source = nil)
  @source = source || Source::Text.new({})
  @working_scripts = []
  @imported_scripts = []
  @running = false
  post_initialize
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

#imported_scriptsArray<Script> (readonly)

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

Returns:



51
52
53
# File 'lib/gamefic/plot.rb', line 51

def imported_scripts
  @imported_scripts
end

#metadataObject

TODO: Metadata could use better protection



25
26
27
# File 'lib/gamefic/plot.rb', line 25

def 
  @metadata
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#playbookGamefic::Plot::Playbook



40
41
42
# File 'lib/gamefic/plot.rb', line 40

def playbook
  @playbook ||= Gamefic::Plot::Playbook.new
end

#post_initializeObject



55
56
57
# File 'lib/gamefic/plot.rb', line 55

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.



68
69
70
71
72
73
74
# File 'lib/gamefic/plot.rb', line 68

def ready
  playbook.freeze
  @running = true
  call_ready
  call_player_ready
  p_subplots.each { |s| s.ready }
end

#running?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/gamefic/plot.rb', line 44

def running?
  @running
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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gamefic/plot.rb', line 101

def script path
  imported_script = source.export(path)
  if imported_script.nil?
    raise LoadError.new("cannot load script -- #{path}")
  end
  if !@working_scripts.include?(imported_script) and !imported_scripts.include?(imported_script)
    @working_scripts.push imported_script
    # @hack Arguments need to be in different order if source returns proc
    if imported_script.read.kind_of?(Proc)
      stage &imported_script.read
    else
      stage imported_script.read, imported_script.absolute_path
    end
    @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:



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

def syntaxes
  playbook.syntaxes
end

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



89
90
91
92
93
# File 'lib/gamefic/plot.rb', line 89

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.



78
79
80
81
82
83
84
85
86
87
# File 'lib/gamefic/plot.rb', line 78

def update
  entities.each { |e| e.flush }
  call_before_player_update
  p_players.each { |p| p.scene.update }
  p_entities.each { |e| e.update }
  call_player_update
  call_update
  p_subplots.each { |s| s.update unless s.concluded? }
  p_subplots.delete_if { |s| s.concluded? }
end