-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthbar.rb
60 lines (51 loc) · 1.33 KB
/
healthbar.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'RMagick'
class HealthBar
attr_accessor :health, :x, :y, :z
def initialize (window, width, height, max_health, starting_health, x, y, z, text = "", foreground_colour = 'red', border_colour = 'white', empty_callback)
@window = window
@width = width
@height = height
@max_health = max_health
self.health = starting_health
self.x = x
self.y = y
self.z = z
@text = text
@foreground = foreground_colour
@border = border_colour
@empty_callback = empty_callback
@fixed_width = nil
end
def draw
update_bar
@bar.draw(self.x, self.y, self.z)
end
def update_bar
@gc = Magick::Draw.new
@gc.fill_opacity(0)
@gc.stroke(@border)
@gc.stroke_width(1)
@gc.rectangle(0,0, @width, @height)
@gc.fill_opacity(1)
@gc.fill(@foreground)
@gc.stroke(@foreground)
@gc.stroke_width(1)
@gc.rectangle(1,1, (@fixed_width == nil ? bar_width - 1 : @fixed_width), @height - 1)
if @text.length > 0 then
@gc.fill('black')
@gc.stroke('black')
@gc.text(25, 15, @text)
end
background = Magick::Image.new(@width + 1, @height + 1)
@gc.draw background
@bar = Gosu::Image.new(@window, background, true)
if @fixed_width == nil and bar_width == 1 then
@empty_callback.call()
@fixed_width = 1
end
end
def bar_width
w = Float(health) / Float(@max_health) * @width
return (w > 0 ? w : 1)
end
end