mtimer/system/update_timer.lua

120 lines
4.1 KiB
Lua
Raw Normal View History

2019-02-16 16:45:32 +01:00
local m = mtimer
local deserialize = minetest.deserialize
2019-02-22 15:29:08 +01:00
2019-02-22 20:33:42 +01:00
-- Calculate HUD positions and offsets
--
-- Based on the given named position a table of positional tables is returned
-- by this helper function. When an invalid named position is provided all
-- tables only contain 0 values. Valid positions shown in the diagram below.
--
-- +--------------------------------+
-- | tl tc tr |
-- | |
-- | |
-- | ml mc mr |
-- | |
-- | |
-- | bl bc br |
-- +--------------------------------+
--
-- For orientation: `mc` is the center of the screen (where the crosshair is).
-- `bc` is the location of the hotbar and health bars, etc.
-- Both are valid positions but should not be used.
--
-- Provided offsets will be added or substracted according to the position. In
-- result the pffset behaves like a border
--
2019-02-22 20:33:42 +01:00
-- @param pos A positional string as described
-- @param poffset An offset table like { x=1, y=1 }
2019-02-22 20:33:42 +01:00
-- @return table a Table containing the positional tables based on the string
local get_hud_positions = function (pos, offset)
2019-02-22 15:29:08 +01:00
local p = { x = 0, y = 0 }
local a = { x = 0, y = 0 }
local o = { x = 0, y = 0 }
if pos == 'tl' then
p = { x = 0, y = 0 }
a = { x = 1, y = 1 }
o = { x = 5 + offset.x, y = 3 + offset.y }
end
if pos == 'tc' then
p = { x = 0.5, y = 0 }
a = { x = 0, y = 1 }
o = { x = 0 - offset.x, y = 3 + offset.y }
end
if pos == 'tr' then
p = { x = 1, y = 0 }
a = { x=-1, y = 1 }
o = { x = -6 - offset.x, y = 3 + offset.y }
end
if pos == 'ml' then
p = { x = 0, y = 0.5 }
a = { x = 1, y = 0 }
o = { x = 5 + offset.x, y = 0 + offset.y }
end
if pos == 'mc' then
p = { x = 0.5,y = 0.5 }
a = { x = 0, y = 0 }
o = { x = 0 + offset.x, y = 0 + offset.y }
end
if pos == 'mr' then
p = { x = 1, y = 0.5 }
a = { x = -1,y = 0 }
o = { x = -6 - offset.x, y = 0 + offset.y }
end
if pos == 'bl' then
p = { x = 0, y = 1 }
a = { x = 1, y = -1 }
o = { x = 5 + offset.x, y = 0 - offset.y }
end
if pos == 'bc' then
p = { x = 0.5, y = 1 }
a = { x = 0, y = -1 }
o = { x = 0 + offset.x, y = 0 - offset.y }
end
if pos == 'br' then
p = { x = 1, y = 1 }
a = { x = -1, y = -1 }
o = { x = -6 - offset.x, y = 0 - offset.y }
end
2019-02-22 15:29:08 +01:00
return { position = p, alignment = a, offset = o }
end
2019-02-22 20:33:42 +01:00
-- Update the timer
--
-- This function updates the timer for the given player referenced by the
-- players name. The function is called when a formspec update (fields) is
-- sent to the server and is automatically called by the registered globalstep.
--
-- The function sets the needed values based on the player meta data and uses
-- the `mtimer.get_timer_data` function for the actual data to be shown.
--
-- @param player_name Name of the player to update the timer for
-- @return void
mtimer.update_timer = function (player_name)
2019-02-22 15:29:08 +01:00
local player = minetest.get_player_by_name(player_name)
local meta = player:get_meta()
local m = m.meta
local hud_id = meta:get_string('mtimer:hud_id')
local text = mtimer.get_timer_data(player_name).formatted
local number = meta:get_string(m.color.key):gsub('#', '0x')
local size = meta:get_string(m.hud_element_size.key)
2019-02-22 15:29:08 +01:00
local position = meta:get_string(m.position.key)
local offset = deserialize(meta:get_string(m.hud_element_offset.key))
local orientation = get_hud_positions(position, offset)
2019-02-22 18:55:39 +01:00
if meta:get_string(m.visible.key) == 'false' then text = '' end
2019-02-22 15:29:08 +01:00
player:hud_change(hud_id, 'text', text)
player:hud_change(hud_id, 'number', number)
player:hud_change(hud_id, 'position', orientation.position)
player:hud_change(hud_id, 'alignment', orientation.alignment)
player:hud_change(hud_id, 'size', {x=size, y=size})
2019-02-22 15:29:08 +01:00
player:hud_change(hud_id, 'offset', orientation.offset)
end