Class: D20
- Inherits:
-
Object
- Object
- D20
- 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
Instance Attribute Summary collapse
-
#dc ⇒ Object
readonly
Returns the value of attribute dc.
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(mod = 0, difficulty = 10) ⇒ D20
constructor
A new instance of D20.
- #roll ⇒ Object
- #roll_for_result ⇒ Object
- #stringify_mod ⇒ Object
Constructor Details
Instance Attribute Details
#dc ⇒ Object (readonly)
Returns the value of attribute dc.
14 15 16 |
# File 'lib/dicebag/systems/dnd.rb', line 14 def dc @dc end |
#mod ⇒ Object (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
#roll ⇒ Object
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_result ⇒ Object
42 43 44 |
# File 'lib/dicebag/systems/dnd.rb', line 42 def roll_for_result @roll.roll end |
#stringify_mod ⇒ Object
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 |