Module: Zif
- Defined in:
- lib/zif.rb,
lib/zif/game.rb,
lib/zif/scene.rb,
lib/zif/sprite.rb,
lib/zif/ui/input.rb,
lib/zif/ui/label.rb,
lib/zif/clickable.rb,
lib/zif/traceable.rb,
lib/zif/assignable.rb,
lib/zif/serializable.rb,
lib/zif/key_pressable.rb,
lib/zif/layers/camera.rb,
lib/zif/render_target.rb,
lib/zif/ui/nine_panel.rb,
lib/zif/actions/action.rb,
lib/zif/compound_sprite.rb,
lib/zif/layers/tileable.rb,
lib/zif/actions/sequence.rb,
lib/zif/layers/layerable.rb,
lib/zif/actions/actionable.rb,
lib/zif/actions/animatable.rb,
lib/zif/layers/bitmaskable.rb,
lib/zif/layers/layer_group.rb,
lib/zif/layers/tiled_layer.rb,
lib/zif/ui/nine_panel_edge.rb,
lib/zif/layers/active_layer.rb,
lib/zif/layers/simple_layer.rb,
lib/zif/ui/two_stage_button.rb,
lib/zif/services/input_service.rb,
lib/zif/services/service_group.rb,
lib/zif/services/action_service.rb,
lib/zif/services/sprite_registry.rb,
lib/zif/layers/active_tiled_layer.rb,
lib/zif/services/tick_trace_service.rb,
lib/zif/layers/bitmasked_tiled_layer.rb,
lib/zif/layers/active_bitmasked_tiled_layer.rb
Overview
This is the namespace for the Zif library, and in the lib/zif.rb
file are some miscellaneous helper methods
Defined Under Namespace
Modules: Actions, Assignable, Clickable, KeyPressable, Layers, Serializable, Services, Traceable, UI Classes: CompoundSprite, Game, RenderTarget, Scene, Sprite
Constant Summary collapse
- GTK_COMPATIBLE_VERSION =
'5.5'.freeze
Class Method Summary collapse
-
.add_positions(a, b) ⇒ Array<Numeric>
The addition of the two 2-element array inputs.
-
.boomerang(i, max) ⇒ Numeric
i
uptomax
, otherwise the greater of the reflection offmax
, or0
. -
.check_compatibility ⇒ Object
Checks the running DragonRuby GTK version against GTK_COMPATIBLE_VERSION If different, it prints a little warning.
-
.distance(x1, y1, x2, y2) ⇒ Float
Implements the distance formula.
-
.ease(t, total) ⇒ Object
deprecated
Deprecated.
Usually you want to use Actions::Action instead
- .hsv_to_rgb(h, s, v) ⇒ Array<Numeric>
-
.position_math(op = :plus, a = [1, 1], b = [2, 3]) ⇒ Array<Numeric>
The result of the operation.
-
.radian_angle_between_points(x1, y1, x2, y2) ⇒ Float
The angle between these two points, in radians.
-
.rand_rgb(base, max) ⇒ Array<Numeric>
A three-element array of random numbers between base and max.
-
.relative_rand(x) ⇒ Integer
A random number between negative 1/2 of
x
and positive 1/2 ofx
. - .roll(dice: 4, sides: 16, modifier: 2) ⇒ Numeric deprecated Deprecated.
- .roll_raw(dice: 4, sides: 16) ⇒ Numeric deprecated Deprecated.
-
.roll_raw_v2(dice: 1, sides: 6) ⇒ Numeric
v2 default arguments are to 1d6.
-
.roll_v2(dice: 1, sides: 6, modifier: 0) ⇒ Numeric
v2 default arguments are to 1d6+0.
-
.sub_positions(a, b) ⇒ Array<Numeric>
The subtraction of the two 2-element array inputs.
-
.unique_name(type = 'unknown') ⇒ String
Generates a unique string out of
type
, the current tick count, and an incrementing number.
Class Method Details
.add_positions(a, b) ⇒ Array<Numeric>
Returns The addition of the two 2-element array inputs.
27 28 29 |
# File 'lib/zif.rb', line 27 def self.add_positions(a, b) position_math(:plus, a, b) end |
.boomerang(i, max) ⇒ Numeric
Returns i
upto max
, otherwise the greater of the reflection off max
, or 0
.
15 16 17 18 19 |
# File 'lib/zif.rb', line 15 def self.boomerang(i, max) return i if i <= max return [max - (i - max), 0].max end |
.check_compatibility ⇒ Object
Checks the running DragonRuby GTK version against GTK_COMPATIBLE_VERSION If different, it prints a little warning. This is invoked automatically by Zif::Game#initialize
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/zif.rb', line 206 def self.check_compatibility return if $gtk.version == Zif::GTK_COMPATIBLE_VERSION puts '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+' against_str = "This version of the Zif framework was tested against DRGTK '#{Zif::GTK_COMPATIBLE_VERSION}'" against_pad = [(77 - against_str.length).idiv(2), 0].max running_str = "You are running DragonRuby GTK Version '#{$gtk.version}'" running_pad = [(77 - running_str.length).idiv(2), 0].max puts "|#{' ' * against_pad}#{against_str}#{' ' * (against_pad.even? ? against_pad : against_pad + 1)}|" puts "|#{' ' * running_pad}#{running_str}#{' ' * (running_pad.even? ? running_pad : running_pad + 1)}|" puts '| |' puts '| Please ensure you are using the latest versions of DRGTK and Zif: |' puts '| DRGTK: http://dragonruby.herokuapp.com/toolkit/game |' puts '| Zif: https://github.com/danhealy/dragonruby-zif/releases |' puts '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+' end |
.distance(x1, y1, x2, y2) ⇒ Float
Implements the distance formula
127 128 129 |
# File 'lib/zif.rb', line 127 def self.distance(x1, y1, x2, y2) Math.sqrt((x2 - x1)**2 + (y2 - y1)**2) end |
.ease(t, total) ⇒ Object
Usually you want to use Zif::Actions::Action instead
200 201 202 |
# File 'lib/zif.rb', line 200 def self.ease(t, total) Math.sin(((t / total.to_f) * Math::PI) / 2.0) end |
.hsv_to_rgb(h, s, v) ⇒ Array<Numeric>
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/zif.rb', line 80 def self.hsv_to_rgb(h, s, v) h = [359, [h, 0].max].min.fdiv(360) s = s.fdiv(100) v = v.fdiv(100) h_i = (h * 6).to_i f = h * 6 - h_i p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) case h_i when 0 r = v g = t b = p when 1 r = q g = v b = p when 2 r = p g = v b = t when 3 r = p g = q b = v when 4 r = t g = p b = v when 5 r = v g = p b = q end [(r * 255).to_i, (g * 255).to_i, (b * 255).to_i] end |
.position_math(op = :plus, a = [1, 1], b = [2, 3]) ⇒ Array<Numeric>
Returns The result of the operation.
53 54 55 56 57 58 |
# File 'lib/zif.rb', line 53 def self.position_math(op=:plus, a=[1, 1], b=[2, 3]) [ a[0].send(op, b[0]), a[1].send(op, b[1]) ] end |
.radian_angle_between_points(x1, y1, x2, y2) ⇒ Float
Returns The angle between these two points, in radians.
136 137 138 |
# File 'lib/zif.rb', line 136 def self.radian_angle_between_points(x1, y1, x2, y2) Math.atan2(x2 - x1, y2 - y1) end |
.rand_rgb(base, max) ⇒ Array<Numeric>
Returns A three-element array of random numbers between base and max.
71 72 73 |
# File 'lib/zif.rb', line 71 def self.rand_rgb(base, max) 3.times.map { base + rand(max - base) } end |
.relative_rand(x) ⇒ Integer
Returns A random number between negative 1/2 of x
and positive 1/2 of x
.
64 65 66 |
# File 'lib/zif.rb', line 64 def self.relative_rand(x) (rand(x + 1) - x.fdiv(2)).round end |
.roll(dice: 4, sides: 16, modifier: 2) ⇒ Numeric
Default arguments will be changed to match roll_v2 in 3.0.0+
175 176 177 |
# File 'lib/zif.rb', line 175 def self.roll(dice: 4, sides: 16, modifier: 2) roll_raw(dice: dice, sides: sides) + modifier end |
.roll_raw(dice: 4, sides: 16) ⇒ Numeric
Default arguments will be changed to match roll_raw_v2 in 3.0.0+
188 189 190 |
# File 'lib/zif.rb', line 188 def self.roll_raw(dice: 4, sides: 16) dice.times.map { rand(sides) + 1 }.inject(0) { |z, memo| memo + z } end |
.roll_raw_v2(dice: 1, sides: 6) ⇒ Numeric
v2 default arguments are to 1d6
161 162 163 |
# File 'lib/zif.rb', line 161 def self.roll_raw_v2(dice: 1, sides: 6) roll_raw_v2(dice, sides) end |
.roll_v2(dice: 1, sides: 6, modifier: 0) ⇒ Numeric
v2 default arguments are to 1d6+0
149 150 151 |
# File 'lib/zif.rb', line 149 def self.roll_v2(dice: 1, sides: 6, modifier: 0) roll(dice, sides, modifier) end |
.sub_positions(a, b) ⇒ Array<Numeric>
Returns The subtraction of the two 2-element array inputs.
37 38 39 |
# File 'lib/zif.rb', line 37 def self.sub_positions(a, b) position_math(:minus, a, b) end |
.unique_name(type = 'unknown') ⇒ String
Returns Generates a unique string out of type
, the current tick count, and an incrementing number.
194 195 196 197 |
# File 'lib/zif.rb', line 194 def self.unique_name(type='unknown') @names_used ||= 0 "#{type}_#{Kernel.tick_count}_#{@names_used += 1}" end |