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: Darkroom, 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

#initial_state, #restore, #save

Methods included from Host

#branch, #in_subplot?, #subplots, #subplots_featuring

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, #scene_classes, #yes_or_no

Methods included from Commands

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

Methods included from Entities

#cast, #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.



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

def commands
  @commands
end

#imported_scriptsArray<Script> (readonly)

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

Returns:



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

def imported_scripts
  @imported_scripts
end

#metadataObject

TODO: Metadata could use better protection



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

def 
  
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#playbookGamefic::Plot::Playbook



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

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

#player_class(cls = nil) ⇒ Object



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

def player_class cls = nil
  @player_class = cls unless cls.nil?
  @player_class
end

#post_initializeObject



60
61
62
# File 'lib/gamefic/plot.rb', line 60

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.



73
74
75
76
77
78
79
80
81
# File 'lib/gamefic/plot.rb', line 73

def ready
  playbook.freeze
  @running = true
  # Call the initial state to make sure it's set

  initial_state
  call_ready
  call_player_ready
  p_subplots.each { |s| s.ready }
end

#running?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/gamefic/plot.rb', line 49

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.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gamefic/plot.rb', line 111

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:



67
68
69
# File 'lib/gamefic/plot.rb', line 67

def syntaxes
  playbook.syntaxes
end

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



99
100
101
102
103
# File 'lib/gamefic/plot.rb', line 99

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gamefic/plot.rb', line 85

def update
  entities.each { |e| e.flush }
  call_before_player_update
  p_players.each { |p|
    p.performed nil
    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