Class: DiceBag::RollPart
- Inherits:
-
SimplePart
- Object
- SimplePart
- DiceBag::RollPart
- Includes:
- RollPartString
- Defined in:
- lib/dicebag/roll_part.rb
Overview
This represents the xDx part of the dice string.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#sides ⇒ Object
readonly
Returns the value of attribute sides.
-
#tally ⇒ Object
readonly
Returns the value of attribute tally.
Attributes inherited from SimplePart
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(part) ⇒ RollPart
constructor
A new instance of RollPart.
- #notes ⇒ Object
- #roll ⇒ Object
-
#roll_die ⇒ Object
Rolls a single die from the xDx string.
-
#rolled? ⇒ Boolean
Checks to see if this instance has rolled yet or not.
-
#total ⇒ Object
Gets the total of the last roll; if there is no last roll, it calls roll() first.
Methods included from RollPartString
Methods inherited from SimplePart
Constructor Details
#initialize(part) ⇒ RollPart
Returns a new instance of RollPart.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dicebag/roll_part.rb', line 14 def initialize(part) @total = nil @tally = [] @value = part @count = part[:count] @sides = part[:sides] @notes = part[:notes] || [] # Our Default Options = { explode: 0, drop: 0, keep: 0, reroll: 0, target: 0 } .update(part[:options]) if part.key?(:options) end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
8 9 10 |
# File 'lib/dicebag/roll_part.rb', line 8 def count @count end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/dicebag/roll_part.rb', line 11 def end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
10 11 12 |
# File 'lib/dicebag/roll_part.rb', line 10 def parts @parts end |
#sides ⇒ Object (readonly)
Returns the value of attribute sides.
9 10 11 |
# File 'lib/dicebag/roll_part.rb', line 9 def sides @sides end |
#tally ⇒ Object (readonly)
Returns the value of attribute tally.
12 13 14 |
# File 'lib/dicebag/roll_part.rb', line 12 def tally @tally end |
Instance Method Details
#<=>(other) ⇒ Object
75 76 77 |
# File 'lib/dicebag/roll_part.rb', line 75 def <=>(other) total <=> other.total end |
#notes ⇒ Object
28 29 30 |
# File 'lib/dicebag/roll_part.rb', line 28 def notes @notes.empty? ? '' : @notes.join("\n") end |
#roll ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dicebag/roll_part.rb', line 46 def roll generate_results @results.sort! @results.reverse! # Save the tally in case it's requested later. @tally = @results.dup # Drop the low end numbers if :drop is not zero. handle_drop # Keep the high end numbers if :keep is greater than zero. handle_keep # Set the total. handle_total self end |
#roll_die ⇒ Object
Rolls a single die from the xDx string.
39 40 41 42 43 44 |
# File 'lib/dicebag/roll_part.rb', line 39 def roll_die num = 0 num = rand(sides) + 1 while num <= [:reroll] num end |
#rolled? ⇒ Boolean
Checks to see if this instance has rolled yet or not.
34 35 36 |
# File 'lib/dicebag/roll_part.rb', line 34 def rolled? @total.nil? ? false : true end |
#total ⇒ Object
Gets the total of the last roll; if there is no last roll, it calls roll() first.
69 70 71 72 73 |
# File 'lib/dicebag/roll_part.rb', line 69 def total roll if @total.nil? @total end |