Class: D20

Inherits:
Object
  • Object
show all
Defined in:
lib/dicebag/systems/dnd.rb

Overview

This models a D20 roll used in various D&D versions for rolling a d20 +/-mod to equal or exceed a DC value.

This is a very simple version, but could easily be expanded on.

Usage:

die = D20.new

die.roll 5, 15  => [:success, 17]
die.roll -3, 12 => [:fail, 9]

Direct Known Subclasses

D20Advantage, D20Disadvantage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod = 0, difficulty = 10) ⇒ D20

Returns a new instance of D20.



20
21
22
23
24
25
# File 'lib/dicebag/systems/dnd.rb', line 20

def initialize(mod = 0, difficulty = 10)
  @mod   = mod.to_i
  @dc    = difficulty.to_i
  @dstr  = "1d20 #{stringify_mod}"
  @roll  = DiceBag::Roll.new @dstr
end

Instance Attribute Details

#dcObject (readonly)

Returns the value of attribute dc.



14
15
16
# File 'lib/dicebag/systems/dnd.rb', line 14

def dc
  @dc
end

#modObject (readonly)

Returns the value of attribute mod.



13
14
15
# File 'lib/dicebag/systems/dnd.rb', line 13

def mod
  @mod
end

Class Method Details

.roll(mod = 0, difficulty = 10) ⇒ Object



16
17
18
# File 'lib/dicebag/systems/dnd.rb', line 16

def self.roll(mod = 0, difficulty = 10)
  new(mod, difficulty).roll
end

Instance Method Details

#rollObject



27
28
29
30
31
32
# File 'lib/dicebag/systems/dnd.rb', line 27

def roll
  total = roll_for_result.total
  sym   = total >= dc ? :success : :failure

  [sym, total]
end

#roll_for_resultObject



42
43
44
# File 'lib/dicebag/systems/dnd.rb', line 42

def roll_for_result
  @roll.roll
end

#stringify_modObject



34
35
36
37
38
39
40
# File 'lib/dicebag/systems/dnd.rb', line 34

def stringify_mod
  return "+#{@mod}" if @mod.positive?

  return @mod.to_s  if @mod.negative?

  ''
end