-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
156 lines (141 loc) · 4.63 KB
/
main.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
78
79
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
timers = {}
local config = modlib.mod.configuration()
modlib.table.add_all(getfenv(1), config)
minetest.register_on_joinplayer(
function(player)
timers[player:get_player_name()] = {}
end
)
minetest.register_on_leaveplayer(
function(player)
timers[player:get_player_name()] = {}
end
)
local timer = 0
minetest.register_globalstep(
function(dtime)
timer = timer + dtime
if timer >= globalstep then
for playername, timers in pairs(timers) do
maintain_timers(timers, timer, minetest.get_player_by_name(playername))
end
timer = 0
end
end
)
function trigger_event(playername, event_name)
for _, timer in ipairs(timers[playername]) do
if timer.on_event and timer.on_event[event_name] then
timer.on_event[event_name](playername, timer)
end
end
end
function add_timer(playername, timer_definition)
local player = minetest.get_player_by_name(playername)
local offset = 0
offset = #(timers[playername])
if (offset == hud_timers_max) then
return false
end
offset = offset * -20
local bg_id =
player:hud_add(
{
hud_elem_type = "statbar",
position = hud_pos,
size = nil, -- intentionally set to nil
text = "hudbars_bar_background.png",
number = 2,
alignment = {x = 1, y = 1},
offset = {x = 0, y = offset}
}
)
local bar_id =
player:hud_add(
{
hud_elem_type = "statbar",
position = hud_pos,
size = nil, -- intentionally set to nil
text = "hud_timers_bar_timeout.png^[colorize:#" .. (timer_definition.color or "000000"),
number = 160,
alignment = {x = 1, y = 1},
offset = {x = 1, y = offset + 1}
}
)
local text_id =
player:hud_add(
{
hud_elem_type = "text",
position = hud_pos,
size = nil,
text = string.format(
format,
timer_definition.name,
modlib.number.round(timer_definition.duration, timer_definition.rounding_steps)
),
number = 0xFFFFFF,
alignment = {x = 1, y = 1},
offset = {x = 2, y = offset}
}
)
local timer = {
name = timer_definition.name or "Unnamed Timer",
time_left = timer_definition.duration,
duration = timer_definition.duration,
on_complete = timer_definition.on_complete or function() end,
on_remove = timer_definition.on_remove or function() end,
on_event = timer_definition.on_event,
rounding_steps = timer_definition.rounding_steps or 10,
ids = {bg = bg_id, bar = bar_id, label = text_id}
}
table.insert(
timers[playername],
timer
)
return timer
end
function remove_timer(playername, timer_index)
timers[playername][timer_index].time_left = -1
maintain_timers(timers[playername], 0, minetest.get_player_by_name(playername))
end
function remove_timer_by_reference(playername, timer)
for index, other_timer in pairs(timers[playername]) do
if timer == other_timer then
return remove_timer(playername, index)
end
end
end
--Updates hud
function maintain_timers(timers, dtime, player)
for i, timer in modlib.table.rpairs(timers) do
local time_left = timer.time_left - dtime
if time_left <= 0 then
player:hud_remove(timer.ids.bg)
player:hud_remove(timer.ids.bar)
player:hud_remove(timer.ids.label)
table.remove(timers, i)
if timer.time_left > 0 then
timer.on_complete(player:get_player_name(), timer)
else
timer.on_remove(player:get_player_name(), timer)
end
else
timers[i].time_left = time_left
end
end
for i, timer in ipairs(timers) do
player:hud_change(
timer.ids.label,
"text",
string.format(format, timer.name, modlib.number.round(timer.time_left, timer.rounding_steps))
)
player:hud_change(timer.ids.bar, "number", timer.time_left / timer.duration * 160)
local y_offset = - (i-1) * 20
player:hud_change(timer.ids.bg, "offset", {x = 0, y = y_offset})
player:hud_change(timer.ids.bar, "offset", {x = 1, y = y_offset + 1})
player:hud_change(timer.ids.label, "offset", {x = 2, y = y_offset})
end
end
function update_timers(playername)
maintain_timers(timers[playername], 0, minetest.get_player_by_name(playername))
end