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

#initializeRulebook

Returns a new instance of Rulebook.



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

def initialize
  @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

#scenesScenes (readonly)

Returns:



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

def scenes
  @scenes
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

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

#freezeObject



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

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

#responsesArray<Response>

Returns:



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

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:



81
82
83
# File 'lib/gamefic/rulebook.rb', line 81

def responses_for *verbs
  @calls.responses_for *verbs
end

#run_after_actions(action) ⇒ Object



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

def run_after_actions action
  hooks.run_after action
end

#run_before_actions(action) ⇒ Object



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

def run_before_actions action
  hooks.run_before action
end

#run_conclude_blocksObject



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

def run_conclude_blocks
  events.conclude_blocks.each(&:run)
end

#run_player_conclude_blocks(player) ⇒ Object



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

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

#run_player_output_blocks(player, output) ⇒ Object



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

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

#run_ready_blocksObject



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

def run_ready_blocks
  events.ready_blocks.each(&:run)
end

#run_update_blocksObject



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

def run_update_blocks
  events.update_blocks.each(&:run)
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]


73
74
75
# File 'lib/gamefic/rulebook.rb', line 73

def synonyms
  @calls.synonyms
end

#syntaxesArray<Syntax>

Returns:



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

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:



89
90
91
# File 'lib/gamefic/rulebook.rb', line 89

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:



61
62
63
# File 'lib/gamefic/rulebook.rb', line 61

def verbs
  @calls.verbs
end