diff options
| author | _Tradam <[email protected]> | 2021-12-16 19:22:26 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-16 19:22:26 -0500 |
| commit | 5954b9beb4d4a3b4f248d72d1851195f030558a8 (patch) | |
| tree | fecd8aa840a25afdb502915b0fdb4d03b7ed339a /dragon/numeric.rb | |
| parent | 2f845281f133849256b57bb08fd3e9ae57600784 (diff) | |
| parent | eaa29e72939f5edf61735ccbb73c36ee89369f65 (diff) | |
| download | dragonruby-game-toolkit-contrib-master.tar.gz dragonruby-game-toolkit-contrib-master.zip | |
Diffstat (limited to 'dragon/numeric.rb')
| -rw-r--r-- | dragon/numeric.rb | 189 |
1 files changed, 69 insertions, 120 deletions
diff --git a/dragon/numeric.rb b/dragon/numeric.rb index 93bacae..52ddf61 100644 --- a/dragon/numeric.rb +++ b/dragon/numeric.rb @@ -13,6 +13,35 @@ class Numeric alias_method :lte, :<= alias_method :__original_eq_eq__, :== unless Numeric.instance_methods.include? :__original_eq_eq__ + def to_layout_row opts = {} + $layout.rect(row: self, + col: opts.col || 0, + w: opts.w || 0, + h: opts.h || 0).y + end + + def to_layout_col opts = {} + $layout.rect(row: 0, + col: self, + w: opts.w || 0, + h: opts.h || 0).x + end + + def to_layout_w + $layout.rect(row: 0, col: 0, w: self, h: 1).w + end + + def to_layout_h + $layout.rect(row: 0, col: 0, w: 1, h: self).h + end + + def to_layout_row_from_bottom opts = {} + ($layout.row_max_index - self).to_layout_row opts + end + + def to_layout_col_from_right opts = {} + ($layout.col_max_index - self).to_layout_col opts + end # Converts a numeric value representing seconds into frames. # @@ -28,10 +57,26 @@ class Numeric self / 2.0 end + def third + self / 3.0 + end + + def quarter + self / 4.0 + end + def to_byte clamp(0, 255).to_i end + def clamp *opts + min = (opts.at 0) + max = (opts.at 1) + return min if min && self < min + return max if max && self > max + return self + end + def clamp_wrap min, max max, min = min, max if min > max return self if self >= min && self <= max @@ -256,7 +301,7 @@ S self * Math::PI.fdiv(180) end - # Converts a number representing an angle in radians to degress. + # Converts a number representing an angle in radians to degrees. # # @gtk def to_degrees @@ -273,21 +318,21 @@ S GTK::Geometry.to_square(self, x, y, anchor_x, anchor_y) end - # Returns a normal vector for a number that represents an angle in degress. + # Returns a normal vector for a number that represents an angle in degrees. # # @gtk def vector max_value = 1 [vector_x(max_value), vector_y(max_value)] end - # Returns the y component of a normal vector for a number that represents an angle in degress. + # Returns the y component of a normal vector for a number that represents an angle in degrees. # # @gtk def vector_y max_value = 1 max_value * Math.sin(self.to_radians) end - # Returns the x component of a normal vector for a number that represents an angle in degress. + # Returns the x component of a normal vector for a number that represents an angle in degrees. # # @gtk def vector_x max_value = 1 @@ -314,6 +359,18 @@ S (self % n) == 0 end + def multiply n + self * n + end + + def fmult n + self * n.to_f + end + + def imult n + (self * n).to_i + end + def mult n self * n end @@ -415,32 +472,6 @@ S (0..self).to_a end - def >= other - return false if !other - return gte other - end - - def > other - return false if !other - return gt other - end - - def <= other - return false if !other - return lte other - end - - def < other - return false if !other - return gt other - end - - def == other - return true if __original_eq_eq__ other - return __original_eq_eq__ other.entity_id if other.is_a? OpenEntity - return false - end - # @gtk def map unless block_given? @@ -526,34 +557,6 @@ The object above is not a Numeric. S end - def - other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :-, e - end - - def + other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :+, e - end - - def * other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :*, e - end - - def / other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :/, e - end - def serialize self end @@ -579,6 +582,10 @@ S def self.clamp n, min, max n.clamp min, max end + + def mid? l, r + (between? l, r) || (between? r, l) + end end class Fixnum @@ -604,40 +611,6 @@ class Fixnum return !even? end - def + other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :+, e - end - - def * other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :*, e - end - - def / other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :/, e - end - - def - other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :-, e - end - - def == other - return true if __original_eq_eq__ other - return __original_eq_eq__ other.entity_id if other.is_a? GTK::OpenEntity - return false - end - # Returns `-1` if the number is less than `0`. `+1` if the number # is greater than `0`. Returns `0` if the number is equal to `0`. # @@ -693,34 +666,6 @@ class Float alias_method :__original_multiply__, :* unless Float.instance_methods.include? :__original_multiply__ alias_method :__original_divide__, :- unless Float.instance_methods.include? :__original_divide__ - def - other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :-, e - end - - def + other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :+, e - end - - def * other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :*, e - end - - def / other - return nil unless other - super - rescue Exception => e - __raise_arithmetic_exception__ other, :/, e - end - def serialize self end @@ -762,4 +707,8 @@ class Integer def nan? false end + + def center other + (self - other).abs.fdiv(2) + end end |
