Class: FifthedSim::RollNode
- Inherits:
-
DiceExpression
- Object
- DiceExpression
- FifthedSim::RollNode
- Defined in:
- lib/fifthed_sim/nodes/roll_node.rb
Overview
Model a single roll of the dice. Users of the library will rarely interact with this class, and will instead manpiulate values based on the DiceResult type.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.average(type) ⇒ Object
Obtain a DieRoll filled with the average result of this die type This will round down.
-
.average_value(type) ⇒ Object
Obtain an average value for this die type, as a float We’re extremely lazy here.
-
.roll(type) ⇒ Object
Create a diceresult by rolling a certain type.
Instance Method Summary collapse
-
#average ⇒ Object
The average roll for a die of this type.
-
#crit? ⇒ Boolean
Is this roll a critical? (AKA, is it the max value of the dice?).
-
#critfail? ⇒ Boolean
Is this roll a critical failure? (AKA, is it a 1?).
-
#difference_from_average ⇒ Object
How far away this roll is from the average roll.
- #distribution ⇒ Object
- #expression_equation ⇒ Object
-
#initialize(val, type) ⇒ RollNode
constructor
A new instance of RollNode.
- #reroll ⇒ Object
- #value_equation(terminal: false) ⇒ Object
Methods inherited from DiceExpression
#*, #+, #-, #/, #max, #min, #or_greater, #or_least, #percentile, #range, #test_then, #to_dice_expression, #to_f, #to_i
Constructor Details
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
43 44 45 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 43 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
43 44 45 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 43 def value @value end |
Class Method Details
.average(type) ⇒ Object
Obtain a DieRoll filled with the average result of this die type This will round down.
20 21 22 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 20 def self.average(type) self.new((type + 1) / 2, type) end |
.average_value(type) ⇒ Object
Obtain an average value for this die type, as a float We’re extremely lazy here.
27 28 29 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 27 def self.average_value(type) self.new(1, type).average end |
.roll(type) ⇒ Object
Create a diceresult by rolling a certain type.
12 13 14 15 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 12 def self.roll(type) raise ArgumentError, "Must be an Integer" unless type.is_a? Fixnum self.new(SecureRandom.random_number(type) + 1, type) end |
Instance Method Details
#average ⇒ Object
The average roll for a die of this type
47 48 49 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 47 def average (@type + 1) / 2.0 end |
#crit? ⇒ Boolean
Is this roll a critical? (AKA, is it the max value of the dice?)
65 66 67 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 65 def crit? @value == @type end |
#critfail? ⇒ Boolean
Is this roll a critical failure? (AKA, is it a 1?)
59 60 61 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 59 def critfail? @value == 1 end |
#difference_from_average ⇒ Object
How far away this roll is from the average roll
53 54 55 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 53 def difference_from_average @value - average end |
#distribution ⇒ Object
69 70 71 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 69 def distribution Distribution.for((1..@type)) end |
#expression_equation ⇒ Object
84 85 86 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 84 def expression_equation "d#{@type}" end |
#reroll ⇒ Object
39 40 41 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 39 def reroll self.class.roll(@type) end |
#value_equation(terminal: false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fifthed_sim/nodes/roll_node.rb', line 73 def value_equation(terminal: false) return value.to_s unless terminal if critfail? Rainbow(value.to_s).color(:red).bright.to_s elsif crit? Rainbow(value.to_s).color(:yellow).bright.to_s else value.to_s end end |