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

Class Method Details

.add_positions(a, b) ⇒ Array<Numeric>

Returns The addition of the two 2-element array inputs.

Examples:

# [4, 8] + [1, 1] = [5, 9]
Zif.add_positions([4, 8], [1, 1]) # => [5, 9]

Parameters:

  • a (Array<Numeric>)
  • b (Array<Numeric>)

Returns:

  • (Array<Numeric>)

    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.

Examples:

The boomerang goes to 5 and then back to 0

Zif.boomerang(2, 5) # => 2
Zif.boomerang(5, 5) # => 5
Zif.boomerang(6, 5) # => 4
Zif.boomerang(7, 5) # => 3
Zif.boomerang(10, 5) # => 0
Zif.boomerang(15, 5) # => 0

Parameters:

  • i (Numeric)
  • max (Numeric)

Returns:

  • (Numeric)

    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_compatibilityObject

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

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)

Returns:

  • (Float)

    The distance between these two points



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

Deprecated.

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>

Parameters:

  • h (Integer)

    Hue, valid range 0-359

  • s (Integer)

    Saturation, valid range 0-100

  • v (Integer)

    Value, valid range 0-100

Returns:

  • (Array<Numeric>)

    A three-element array of integers from 0-255 representing the RBG value of the given hsv.



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.

Examples:

Dividing one array from another

# This works because #fdiv is a method you can call on Numeric
Zif.position_math(:fdiv, [5, 6], [2, 2]) # => [2.5, 3.0]

Multiplying two arrays together

# #mult is another method.  You can use any method the elements of +a+ respond to.
Zif.position_math(:mult, [5, 6], [2, 2]) # => [10, 12]

Parameters:

  • op (Symbol) (defaults to: :plus)

    A method to call against the elements of a given the argument the matching element of b

  • a (Array<Numeric>) (defaults to: [1, 1])
  • b (Array<Numeric>) (defaults to: [2, 3])

Returns:

  • (Array<Numeric>)

    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.

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)

Returns:

  • (Float)

    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.

Parameters:

  • base (Numeric)

    The base value of the color, it cannot go lower than this

  • max (Numeric)

    The maximum value of the color, it cannot go higher than this

Returns:

  • (Array<Numeric>)

    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.

Examples:

Gimme a number between -5 and +5

Zif.relative_rand(10) # => -3

Parameters:

  • x (Numeric)

    A number representing the extent of the range of values (Remember that 0 is included)

Returns:

  • (Integer)

    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

Deprecated.

Default arguments will be changed to match roll_v2 in 3.0.0+

Examples:

Roll some dice with a modifier

Zif.roll(dice: 4, sides: 16, modifier: 2)
# We roll 8, 2, 3, 16.  Added together this is 29.  At the end, the modifier is added.
# => 31

Parameters:

  • dice (Integer) (defaults to: 4)
  • sides (Integer) (defaults to: 16)
  • modifier (Numeric) (defaults to: 2)

Returns:

  • (Numeric)

    Chooses a number between 1 and sides, dice times, and then adds them together with modifier.



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

Deprecated.

Default arguments will be changed to match roll_raw_v2 in 3.0.0+

Examples:

Roll some dice

Zif.roll_raw(dice: 4, sides: 16)
# We roll 8, 2, 3, 16.  Added together this is 29.
# => 29

Parameters:

  • dice (Integer) (defaults to: 4)
  • sides (Integer) (defaults to: 16)

Returns:

  • (Numeric)

    Chooses a number between 1 and sides, dice times, and adds them together.



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

Examples:

Roll some dice

Zif.roll_raw_v2(dice: 4, sides: 16)
# We roll 8, 2, 3, 16.  Added together this is 29.
# => 29

Parameters:

  • dice (Integer) (defaults to: 1)
  • sides (Integer) (defaults to: 6)

Returns:

  • (Numeric)

    Chooses a number between 1 and sides, dice times, and adds them together.



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

Examples:

Roll some dice with a modifier

Zif.roll_v2(dice: 4, sides: 16, modifier: 2)
# We roll 8, 2, 3, 16.  Added together this is 29.  At the end, the modifier is added.
# => 31

Parameters:

  • dice (Integer) (defaults to: 1)
  • sides (Integer) (defaults to: 6)
  • modifier (Numeric) (defaults to: 0)

Returns:

  • (Numeric)

    Chooses a number between 1 and sides, dice times, and then adds them together with modifier.



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.

Examples:

# [4, 8] - [1, 1] = [3, 7]
 Zif.sub_positions([4, 8], [1, 1]) # => [3, 7]

Parameters:

  • a (Array<Numeric>)
  • b (Array<Numeric>)

Returns:

  • (Array<Numeric>)

    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.

Parameters:

  • type (Symbol, String) (defaults to: 'unknown')

    The prefix for the random string, typically this is called with a class name.

Returns:

  • (String)

    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