Class: Gamefic::Rulebook

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/rulebook.rb,
lib/gamefic/rulebook/calls.rb,
lib/gamefic/rulebook/hooks.rb,
lib/gamefic/rulebook/events.rb,
lib/gamefic/rulebook/scenes.rb

Overview

A collection of rules that define the behavior of a narrative.

Rulebooks provide a way to separate narrative data from code. This separation is necessary to ensure that the game state can be serialized in snapshots.

Defined Under Namespace

Classes: Calls, Events, Hooks, Scenes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(narrative) ⇒ Rulebook

Returns a new instance of Rulebook.

Parameters:



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

def initialize(narrative)
  @narrative = narrative
  @calls = Calls.new
  @events = Events.new
  @hooks = Hooks.new
  @scenes = Scenes.new
end

Instance Attribute Details

#callsCalls (readonly)

Returns:



17
18
19
# File 'lib/gamefic/rulebook.rb', line 17

def calls
  @calls
end

#eventsEvents (readonly)

Returns:



20
21
22
# File 'lib/gamefic/rulebook.rb', line 20

def events
  @events
end

#hooksHooks (readonly)

Returns:



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

def hooks
  @hooks
end

#narrativeNarrative (readonly)

Returns:



29
30
31
# File 'lib/gamefic/rulebook.rb', line 29

def narrative
  @narrative
end

#scenesScenes (readonly)

Returns:



26
27
28
# File 'lib/gamefic/rulebook.rb', line 26

def scenes
  @scenes
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/gamefic/rulebook.rb', line 126

def empty?
  calls.empty? && hooks.empty? && scenes.empty? && events.empty?
end

#freezeObject



40
41
42
43
44
# File 'lib/gamefic/rulebook.rb', line 40

def freeze
  super
  [@calls, @events, @hooks, @scenes].each(&:freeze)
  self
end

#responsesArray<Response>

Returns:



47
48
49
# File 'lib/gamefic/rulebook.rb', line 47

def responses
  @calls.responses
end

#responses_for(*verbs) ⇒ Array<Response>

Get an array of all the responses that match a list of verbs.

Parameters:

Returns:



86
87
88
# File 'lib/gamefic/rulebook.rb', line 86

def responses_for *verbs
  @calls.responses_for *verbs
end

#run_after_actions(action) ⇒ Object



110
111
112
# File 'lib/gamefic/rulebook.rb', line 110

def run_after_actions action
  hooks.run_after action, narrative
end

#run_before_actions(action) ⇒ Object



106
107
108
# File 'lib/gamefic/rulebook.rb', line 106

def run_before_actions action
  hooks.run_before action, narrative
end

#run_conclude_blocksObject



114
115
116
# File 'lib/gamefic/rulebook.rb', line 114

def run_conclude_blocks
  events.conclude_blocks.each { |blk| Stage.run narrative, &blk }
end

#run_player_conclude_blocks(player) ⇒ Object



118
119
120
# File 'lib/gamefic/rulebook.rb', line 118

def run_player_conclude_blocks player
  events.player_conclude_blocks.each { |blk| Stage.run(narrative) { blk.call(player) } }
end

#run_player_output_blocks(player, output) ⇒ Object



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

def run_player_output_blocks player, output
  events.player_output_blocks.each { |blk| Stage.run(narrative) { blk.call(player, output) } }
end

#run_ready_blocksObject



98
99
100
# File 'lib/gamefic/rulebook.rb', line 98

def run_ready_blocks
  events.ready_blocks.each { |blk| Stage.run narrative, &blk }
end

#run_update_blocksObject



102
103
104
# File 'lib/gamefic/rulebook.rb', line 102

def run_update_blocks
  events.update_blocks.each { |blk| Stage.run narrative, &blk }
end

#scriptObject



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

def script
  narrative.class.included_blocks.select(&:script?).each { |blk| Stage.run(narrative, &blk.code) }
end

#script_with_defaultsObject



134
135
136
137
# File 'lib/gamefic/rulebook.rb', line 134

def script_with_defaults
  script
  scenes.with_defaults narrative
end

#synonymsObject

An array of all the verbs defined in responses and any synonyms defined in syntaxes.

Examples:

rulebook.respond :verb { |_| nil }
rulebook.interpret 'synonym', 'verb'
rulebook.synonyms #=> [:synonym, :verb]


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

def synonyms
  @calls.synonyms
end

#syntaxesArray<Syntax>

Returns:



52
53
54
# File 'lib/gamefic/rulebook.rb', line 52

def syntaxes
  @calls.syntaxes
end

#syntaxes_for(*synonyms) ⇒ Array<Syntax>

Get an array of all the syntaxes that match a lit of verbs.

Parameters:

Returns:



94
95
96
# File 'lib/gamefic/rulebook.rb', line 94

def syntaxes_for *synonyms
  @calls.syntaxes_for *synonyms
end

#verbsArray<Symbol>

An array of all the verbs available in the rulebook. This list only includes verbs that are explicitly defined in reponses. It excludes synonyms that might be defined in syntaxes (see #synonyms).

Examples:

rulebook.respond :verb { |_| nil }
rulebook.interpret 'synonym', 'verb'
rulebook.verbs #=> [:verb]

Returns:



66
67
68
# File 'lib/gamefic/rulebook.rb', line 66

def verbs
  @calls.verbs
end