Module: Zif

Defined in:
app/lib/zif/zif.rb,
app/lib/zif/game.rb,
app/lib/zif/scene.rb,
app/lib/zif/sprite.rb,
app/lib/zif/ui/label.rb,
app/lib/zif/clickable.rb,
app/lib/zif/traceable.rb,
app/lib/zif/assignable.rb,
app/lib/zif/serializable.rb,
app/lib/zif/layers/camera.rb,
app/lib/zif/render_target.rb,
app/lib/zif/ui/nine_panel.rb,
app/lib/zif/actions/action.rb,
app/lib/zif/compound_sprite.rb,
app/lib/zif/layers/tileable.rb,
app/lib/zif/actions/sequence.rb,
app/lib/zif/layers/layerable.rb,
app/lib/zif/actions/actionable.rb,
app/lib/zif/actions/animatable.rb,
app/lib/zif/layers/bitmaskable.rb,
app/lib/zif/layers/layer_group.rb,
app/lib/zif/layers/tiled_layer.rb,
app/lib/zif/ui/nine_panel_edge.rb,
app/lib/zif/layers/active_layer.rb,
app/lib/zif/layers/simple_layer.rb,
app/lib/zif/ui/two_stage_button.rb,
app/lib/zif/services/input_service.rb,
app/lib/zif/services/service_group.rb,
app/lib/zif/services/action_service.rb,
app/lib/zif/services/sprite_registry.rb,
app/lib/zif/layers/active_tiled_layer.rb,
app/lib/zif/services/tick_trace_service.rb,
app/lib/zif/layers/bitmasked_tiled_layer.rb,
app/lib/zif/layers/active_bitmasked_tiled_layer.rb

Overview

This is the namespace for the Zif library, and in the app/lib/zif/zif.rb file are some miscellaneous helper methods

Defined Under Namespace

Modules: Actions, Assignable, Clickable, Layers, Serializable, Services, Traceable, UI Classes: CompoundSprite, Game, RenderTarget, Scene, Sprite

Constant Summary collapse

GTK_COMPATIBLE_VERSION =
'2.10'.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 'app/lib/zif/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 'app/lib/zif/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


177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/lib/zif/zif.rb', line 177

def self.check_compatibility
  return if $gtk.version == Zif::GTK_COMPATIBLE_VERSION

  puts '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+'
  puts "|      This version of the Zif framework was tested against DRGTK '#{Zif::GTK_COMPATIBLE_VERSION}'       |"
  puts "|                You are running DragonRuby GTK Version '#{$gtk.version}'                 |"
  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 'app/lib/zif/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


171
172
173
# File 'app/lib/zif/zif.rb', line 171

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 'app/lib/zif/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 'app/lib/zif/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 'app/lib/zif/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 'app/lib/zif/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 'app/lib/zif/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

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

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.


148
149
150
# File 'app/lib/zif/zif.rb', line 148

def self.roll(dice: 4, sides: 16, modifier: 2)
  roll_raw(dice: dice, sides: sides) + modifier
end

.roll_raw(dice: 4, sides: 16) ⇒ Numeric

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

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.


159
160
161
# File 'app/lib/zif/zif.rb', line 159

def self.roll_raw(dice: 4, sides: 16)
  dice.times.map { rand(sides) + 1 }.inject(0) { |z, memo| memo + z }
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 'app/lib/zif/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.


165
166
167
168
# File 'app/lib/zif/zif.rb', line 165

def self.unique_name(type='unknown')
  @names_used ||= 0
  "#{type}_#{Kernel.tick_count}_#{@names_used += 1}"
end