Module: Utils

Defined in:
lib/misc/utils.rb

Constant Summary collapse

HEARING_DISTANCE =
1000.0
DEBUG_COLORS =
[
  Gosu::Color::RED,
  Gosu::Color::BLUE,
  Gosu::Color::YELLOW,
  Gosu::Color::WHITE
]

Class Method Summary collapse

Class Method Details

.adjust_speed(speed) ⇒ Object



32
33
34
# File 'lib/misc/utils.rb', line 32

def self.adjust_speed(speed)
  speed * update_interval / 33.33
end

.angle_between(x, y, target_x, target_y) ⇒ Object



95
96
97
98
99
# File 'lib/misc/utils.rb', line 95

def self.angle_between(x, y, target_x, target_y)
  dx = target_x - x
  dy = target_y - y
  (180 - Math.atan2(dx, dy) * 180 / Math::PI) + 360 % 360
end

.button_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/misc/utils.rb', line 36

def self.button_down?(button)
  @buttons ||= {}
  now = Gosu.milliseconds
  now = now - (now % 150)
  if $window.button_down?(button)
    @buttons[button] = now
    true
  elsif @buttons[button]
    if now == @buttons[button]
      true
    else
      @buttons.delete(button)
      false
    end
  end
end

.distance_between(x1, y1, x2, y2) ⇒ Object



89
90
91
92
93
# File 'lib/misc/utils.rb', line 89

def self.distance_between(x1, y1, x2, y2)
  dx = x1 - x2
  dy = y1 - y2
  Math.sqrt(dx * dx + dy * dy)
end

.main_fontObject



24
25
26
# File 'lib/misc/utils.rb', line 24

def self.main_font
  media_path('armalite_rifle.ttf')
end

.mark_corners(box) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/misc/utils.rb', line 108

def self.mark_corners(box)
  i = 0
  box.each_slice(2) do |x, y|
    color = DEBUG_COLORS[i]
    $window.draw_triangle(
      x - 3, y - 3, color,
      x,     y,     color,
      x + 3, y - 3, color,
      100)
    i = (i + 1) % 4
  end
end

.media_path(file) ⇒ Object



9
10
11
12
# File 'lib/misc/utils.rb', line 9

def self.media_path(file)
  File.join(File.dirname(File.dirname(File.dirname(
    __FILE__))), 'media', file)
end

.pan(object, camera) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/misc/utils.rb', line 130

def self.pan(object, camera)
  return 0 if object == camera.target
  pan = object.x - camera.target.x
  sig = pan > 0 ? 1 : -1
  pan = (pan % HEARING_DISTANCE) / HEARING_DISTANCE
  if sig > 0
    pan
  else
    -1 + pan
  end
end

.point_at_distance(source_x, source_y, angle, distance) ⇒ Object



101
102
103
104
105
106
# File 'lib/misc/utils.rb', line 101

def self.point_at_distance(source_x, source_y, angle, distance)
  angle = (90 - angle) * Math::PI / 180
  x = source_x + Math.cos(angle) * distance
  y = source_y - Math.sin(angle) * distance
  [x, y]
end

.point_in_poly(testx, testy, *poly) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/misc/utils.rb', line 68

def self.point_in_poly(testx, testy, *poly)
  nvert = poly.size / 2 # Number of vertices in poly
  vertx = []
  verty = []
  poly.each_slice(2) do |x, y|
    vertx << x
    verty << y
  end
  inside = false
  j = nvert - 1
  (0..nvert - 1).each do |i|
    if (((verty[i] > testy) != (verty[j] > testy)) &&
       (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) /
       (verty[j] - verty[i]) + vertx[i]))
      inside = !inside
    end
    j = i
  end
  inside
end

.rotate(angle, around_x, around_y, *points) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/misc/utils.rb', line 53

def self.rotate(angle, around_x, around_y, *points)
  result = []
  angle = angle * Math::PI / 180.0
  points.each_slice(2) do |x, y|
    r_x = Math.cos(angle) * (around_x - x) -
      Math.sin(angle) * (around_y - y) + around_x
    r_y = Math.sin(angle) * (around_x - x) +
      Math.cos(angle) * (around_y - y) + around_y
    result << r_x
    result << r_y
  end
  result
end

.title_fontObject



20
21
22
# File 'lib/misc/utils.rb', line 20

def self.title_font
  media_path('top_secret.ttf')
end

.track_update_intervalObject



14
15
16
17
18
# File 'lib/misc/utils.rb', line 14

def self.track_update_interval
  now = Gosu.milliseconds
  @update_interval = (now - (@last_update ||= 0)).to_f
  @last_update = now
end

.update_intervalObject



28
29
30
# File 'lib/misc/utils.rb', line 28

def self.update_interval
  @update_interval ||= $window.update_interval
end

.volume(object, camera) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/misc/utils.rb', line 121

def self.volume(object, camera)
  return 1 if object == camera.target
  distance = Utils.distance_between(
    camera.target.x, camera.target.y,
    object.x, object.y)
  distance = [(HEARING_DISTANCE - distance), 0].max
  distance / HEARING_DISTANCE
end

.volume_and_pan(object, camera) ⇒ Object



142
143
144
# File 'lib/misc/utils.rb', line 142

def self.volume_and_pan(object, camera)
  [volume(object, camera), pan(object, camera)]
end