This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt_healthbar.lua
77 lines (63 loc) · 2.03 KB
/
dt_healthbar.lua
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
DT_HealthBar = Core.class(Sprite)
function DT_HealthBar:init(config)
self.size = 1
self.back_color = config.back_color
self.front_color = config.front_color
self.alpha = 1
self.fill_style = Shape.SOLID
self.max_value = config.max_value
self.min_value = 0
self.width = config.width
self.height = config.height
self.current = config.max_value
self.current_width = config.width
self:addChild(self:draw_bar(self.width, self.back_color))
self:addChild(self:draw_bar(self.width, self.front_color))
end
function DT_HealthBar:draw_bar(width, color)
local bar = Shape.new()
bar:setFillStyle(self.fill_style, color)
bar:setLineStyle(self.size, color, self.alpha)
bar:beginPath()
bar:moveTo(0,0)
bar:lineTo(width, 0)
bar:lineTo(width, self.height)
bar:lineTo(0, self.height)
bar:lineTo(0, 0)
bar:endPath()
return bar
end
function DT_HealthBar:update()
self:removeChildAt(2)
self:addChild(self:draw_bar(self.current_width, self.front_color))
end
function DT_HealthBar:add(amount)
self.current = math.min(self.max_value, math.max(self.min_value, amount))
local percent = self.current / self.max_value
local percentage_of_width = self.width * percent
self.current_width = self.current_width + percentage_of_width
if self.current_width > self.width then
self.current_width = self.width
end
self:update()
end
function DT_HealthBar:subtract(amount)
self.current = math.min(self.max_value, math.max(self.min_value, amount))
local percent = self.current / self.max_value
local percentage_of_width = self.width * percent
self.current_width = self.current_width - percentage_of_width
if self.current_width < 0 then
self.current_width = 0
end
self:update()
end
function DT_HealthBar:fill()
self.current = self.max_value
self.current_width = self.width
self:update()
end
function DT_HealthBar:empty()
self.current = self.min_value
self.current_width = self.min_value
self:update()
end