Class: Gamefic::Narrative

Inherits:
Object
  • Object
show all
Extended by:
Scriptable
Includes:
Logging, Scriptable::Actions, Scriptable::Entities, Scriptable::Events, Scriptable::Proxies, Scriptable::Queries, Scriptable::Scenes
Defined in:
lib/gamefic/narrative.rb

Overview

A base class for building and managing the resources that compose a story. The Plot and Subplot classes inherit from Narrative and provide additional functionality.

Direct Known Subclasses

Chapter, Plot, Subplot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scriptable

attr_seed, blocks, lazy_attr, lazy_ivar, make_seed, method_missing, no_scripts, pick, pick!, proxy, respond_to_missing?

Methods included from Scriptable::Scenes

#block, #conclusion, #introduction, #multiple_choice, #pause, #preface, #scene, #yes_or_no

Methods included from Scriptable::Events

#on_conclude, #on_player_conclude, #on_player_output, #on_player_ready, #on_player_update, #on_ready, #on_update

Methods included from Scriptable::Actions

#after_action, #before_action, #interpret, #meta, #respond, #synonyms, #syntaxes, #verbs

Methods included from Scriptable::Queries

#abstract, #anywhere, #available, #children, #descendants, #myself, #parent, #plaintext, #siblings

Methods included from Scriptable::Proxies

#unproxy

Methods included from Scriptable::Entities

#destroy, #entities, #entity_vault, #find, #make, #pick, #pick!, #player_vault, #players

Methods included from Logging

logger

Constructor Details

#initialize(hydrate: true) ⇒ Narrative

Returns a new instance of Narrative.



23
24
25
26
27
28
29
# File 'lib/gamefic/narrative.rb', line 23

def initialize(hydrate: true)
  return unless hydrate

  seed
  script
  post_script
end

Instance Attribute Details

#rulebookObject (readonly)

Returns the value of attribute rulebook.



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

def rulebook
  @rulebook
end

Class Method Details

.inherited(klass) ⇒ Object



121
122
123
124
# File 'lib/gamefic/narrative.rb', line 121

def self.inherited klass
  super
  klass.blocks.concat blocks
end

Instance Method Details

#attach(cache) ⇒ Object



112
113
114
# File 'lib/gamefic/narrative.rb', line 112

def attach cache
  @rulebook = cache
end

#cast(active) ⇒ Gamefic::Active

Add an active entity to the narrative.

Parameters:

Returns:



79
80
81
82
83
84
# File 'lib/gamefic/narrative.rb', line 79

def cast active
  active.epic.add self
  player_vault.add active
  entity_vault.add active
  active
end

#concluding?Boolean

A narrative is considered to be concluding when all of its players are in a conclusion scene. Engines can use this method to determine whether the game is ready to end.

Returns:

  • (Boolean)


71
72
73
# File 'lib/gamefic/narrative.rb', line 71

def concluding?
  players.empty? || players.all?(&:concluding?)
end

#detachObject

Returns:

  • (Object)


106
107
108
109
110
# File 'lib/gamefic/narrative.rb', line 106

def detach
  cache = @rulebook
  @rulebook = nil
  cache
end

#hydrateObject



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

def hydrate
  script
  post_script
end

#included_blocksArray<Module>

Returns:



41
42
43
# File 'lib/gamefic/narrative.rb', line 41

def included_blocks
  self.class.included_blocks
end

#introduce(player = Gamefic::Actor.new) ⇒ Gamefic::Actor

Introduce an actor to the story.

Parameters:

Returns:



59
60
61
62
63
64
65
# File 'lib/gamefic/narrative.rb', line 59

def introduce(player = Gamefic::Actor.new)
  cast player
  rulebook.scenes.introductions.each do |scene|
    scene.run_start_blocks player, nil
  end
  player
end

#post_scriptObject



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

def post_script
  entity_vault.lock
  rulebook.freeze
end

#readyObject



97
98
99
# File 'lib/gamefic/narrative.rb', line 97

def ready
  rulebook.run_ready_blocks
end

#scenesArray<Symbol>

Returns:



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

def scenes
  rulebook.scenes.names
end

#scriptObject



35
36
37
38
# File 'lib/gamefic/narrative.rb', line 35

def script
  @rulebook = Rulebook.new
  included_blocks.select(&:script?).each { |blk| Stage.run self, &blk.code }
end

#seedObject



31
32
33
# File 'lib/gamefic/narrative.rb', line 31

def seed
  included_blocks.select(&:seed?).each { |blk| Stage.run self, &blk.code }
end

#uncast(active) ⇒ Gamefic::Active

Remove an active entity from the narrative.

Parameters:

Returns:



90
91
92
93
94
95
# File 'lib/gamefic/narrative.rb', line 90

def uncast active
  active.epic.delete self
  player_vault.delete active
  entity_vault.delete active
  active
end

#updateObject



101
102
103
# File 'lib/gamefic/narrative.rb', line 101

def update
  rulebook.run_update_blocks
end