testclock: Rework whole thing.

- Does not flicker anymore.
- Style change.
master
Diego Martinez 2015-07-09 09:20:32 -03:00
parent df496f59ac
commit 7dda4881ca
1 changed files with 34 additions and 28 deletions

View File

@ -1,36 +1,42 @@
local player_hud = { };
local player_hud = { }
local timer = 0;
local timer = 0
local function floormod ( x, y )
return (math.floor(x) % y);
local function floormod(x, y)
return math.floor(x) % y
end
local function get_time ( )
local secs = (60*60*24*minetest.env:get_timeofday());
local s = floormod(secs, 60);
local m = floormod(secs/60, 60);
local h = floormod(secs/3600, 60);
return ("%02d:%02d:%02d"):format(h, m, s);
local secs = 60*60*24*minetest.get_timeofday()
local s = floormod(secs, 60)
local m = floormod(secs/60, 60)
local h = floormod(secs/3600, 60)
return ("%02d:%02d:%02d"):format(h, m, s)
end
minetest.register_globalstep(function ( dtime )
timer = timer + dtime;
if (timer >= 1.0) then
timer = 0;
for _,p in ipairs(minetest.get_connected_players()) do
local name = p:get_player_name();
local h = p:hud_add({
hud_elem_type = "text";
position = {x=0.35, y=0.900};
text = get_time();
number = 0xFFFF00;
});
if (player_hud[name]) then
p:hud_remove(player_hud[name]);
end
player_hud[name] = h;
end
end
end);
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer >= 1.0 then
timer = 0
for _, p in ipairs(minetest.get_connected_players()) do
local name = p:get_player_name()
local h = player_hud[name]
if h then
p:hud_change(h, "text", get_time())
else
h = p:hud_add({
hud_elem_type = "text",
position = { x=0.35, y=0.900 },
text = get_time(),
number = 0xFFFF00,
})
end
player_hud[name] = h
end
end
end)
minetest.register_on_leaveplayer(function(player)
player_hud[player:get_player_name()] = nil
end)