Class: Gamefic::Dispatcher

Inherits:
Object show all
Defined in:
lib/gamefic/dispatcher.rb

Overview

The action selector for character commands.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor, commands = [], actions = []) ⇒ Dispatcher

Returns a new instance of Dispatcher.

Parameters:



8
9
10
11
12
13
# File 'lib/gamefic/dispatcher.rb', line 8

def initialize actor, commands = [], actions = []
  @actor = actor
  @commands = commands
  @actions = actions
  @started = false
end

Class Method Details

.dispatch(actor, command) ⇒ Dispatcher

Parameters:

Returns:



38
39
40
41
42
43
# File 'lib/gamefic/dispatcher.rb', line 38

def self.dispatch actor, command
  group = actor.playbooks.reverse.map { |p| p.dispatch(actor, command) }
  dispatcher = Dispatcher.new(actor)
  group.each { |d| dispatcher.merge d }
  dispatcher
end

.dispatch_from_params(actor, verb, params) ⇒ Dispatcher

Parameters:

Returns:



49
50
51
52
53
54
# File 'lib/gamefic/dispatcher.rb', line 49

def self.dispatch_from_params actor, verb, params
  group = actor.playbooks.reverse.map { |p| p.dispatch_from_params(actor, verb, params) }
  dispatcher = Dispatcher.new(actor)
  group.each { |d| dispatcher.merge d }
  dispatcher
end

Instance Method Details

#merge(dispatcher) ⇒ Object



15
16
17
18
# File 'lib/gamefic/dispatcher.rb', line 15

def merge dispatcher
  commands.concat dispatcher.commands
  actions.concat dispatcher.actions
end

#nextObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gamefic/dispatcher.rb', line 20

def next
  instance = nil
  while instance.nil? && !@actions.empty?
    action = actions.shift
    commands.each do |cmd|
      instance = action.attempt(actor, cmd, !@started)
      if instance
        @started = true
        break
      end
    end
  end
  instance
end