Class: BCDice::GameSystem::OneWayHeroics::RandomEventTable

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/game_system/one_way_heroics/random_event_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type, items) ⇒ RandomEventTable

Returns a new instance of RandomEventTable.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bcdice/game_system/one_way_heroics/random_event_table.rb', line 7

def initialize(name, type, items)
  @name = name
  @items = items.freeze

  m = /(\d+)D(\d+)/i.match(type)
  unless m
    raise ArgumentError, "Unexpected table type: #{type}"
  end

  @times = m[1].to_i
  @sides = m[2].to_i
end

Instance Method Details

#roll_with_day(day, randomizer) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bcdice/game_system/one_way_heroics/random_event_table.rb', line 20

def roll_with_day(day, randomizer)
  value = randomizer.roll_sum(@times, @sides)
  index = value - 1

  chosen = @items[index]
  chosen =
    if chosen.respond_to?(:roll)
      chosen.roll(randomizer)
    elsif chosen.respond_to?(:roll_with_day)
      chosen.roll_with_day(day, randomizer)
    else
      chosen
    end

  return "#{@name}(#{value}) > #{chosen}"
end