Add simple HUD support

This commit is contained in:
Wuzzy 2015-02-02 04:20:38 +01:00
parent f79321e5e8
commit 205084ae1e

View File

@ -19,23 +19,25 @@ mana.playerlist = {}
]===] ]===]
-- Sets the maximum mana of the specified player -- Sets the maximum mana of the specified player
function mana.set(playername, mana) function mana.set(playername, value)
mana.playerlist[playerlist].mana = mana mana.playerlist[playername].mana = value
mana.hud_update(playername)
end end
-- Sets the maximum mana of the specified player -- Sets the maximum mana of the specified player
function mana.setmax(playername, maxmana) function mana.setmax(playername, value)
mana.playerlist[playerlist].maxmana = maxmana mana.playerlist[playername].maxmana = value
mana.hud_update(playername)
end end
-- Returns the current mana of the specified player -- Returns the current mana of the specified player
function mana.get(playername) function mana.get(playername)
return mana.playerlist[playerlist].mana return mana.playerlist[playername].mana
end end
-- Returns the maximum mana of the specified player -- Returns the maximum mana of the specified player
function mana.getmax(playername) function mana.getmax(playername)
return mana.playerlist[playerlist].maxmana return mana.playerlist[playername].maxmana
end end
--[[ --[[
@ -47,17 +49,18 @@ returns:
was no was no
- false on failure - false on failure
]] ]]
function mana.add(playername, mana) function mana.add(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
if(t ~= nil and mana >= 0) then if(t ~= nil and value >= 0) then
local excess local excess
if((t.mana + mana) > t.maxmana) then if((t.mana + value) > t.maxmana) then
excess = (t.mana + mana) - t.maxmana excess = (t.mana + value) - t.maxmana
t.mana = t.maxmana t.mana = t.maxmana
else else
excess = 0 excess = 0
t.mana = t.mana + mana t.mana = t.mana + value
end end
mana.hud_update(playername)
return true, excess return true, excess
else else
return false return false
@ -73,10 +76,11 @@ returns:
- true on success, mana has been subtracted - true on success, mana has been subtracted
- false on failure, no mana has been subtracted - false on failure, no mana has been subtracted
]] ]]
function mana.subtract(playername, mana) function mana.subtract(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
if(t ~= nil and t.mana >= mana and mana >= 0) then if(t ~= nil and t.mana >= value and value >= 0) then
t.mana = t.mana - mana t.mana = t.mana -value
mana.hud_update(playername)
return true return true
else else
return false return false
@ -126,11 +130,13 @@ end
minetest.register_on_dieplayer(function(player) minetest.register_on_dieplayer(function(player)
local playername = player:get_player_name() local playername = player:get_player_name()
mana.set(playername, 0) mana.set(playername, 0)
mana.hud_update(playername)
end) end)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local playername = player:get_player_name() local playername = player:get_player_name()
mana.hud_remove(playername)
mana.playerlist[playername] = nil mana.playerlist[playername] = nil
end) end)
@ -148,12 +154,42 @@ minetest.register_on_joinplayer(function(player)
mana.playerlist[playername].maxmana = 20 mana.playerlist[playername].maxmana = 20
end end
-- TODO: Update HUD mana.hud_add(playername)
end) end)
--[===[ --[===[
TODO: HUD functions HUD functions
]===] ]===]
function mana.manastring(playername)
return string.format("Mana: %d/%d", mana.get(playername), mana.getmax(playername))
end
function mana.hud_add(playername)
local player = minetest.get_player_by_name(playername)
id = player:hud_add({
hud_elem_type = "text",
position = { x = 1, y=0.3 },
name = "mana",
text = mana.manastring(playername),
scale = { x = 0, y = 0 },
alignment = { x = -1, y = 0},
direction = 1,
number = 0xFFFFFF,
offset = { x = 0, y = 0}
})
mana.playerlist[playername].hudid = id
return id
end
function mana.hud_update(playername)
local player = minetest.get_player_by_name(playername)
player:hud_change(mana.playerlist[playername].hudid, "text", mana.manastring(playername))
end
function mana.hud_remove(playername)
local player = minetest.get_player_by_name(playername)
player:hud_remove(mana.playerlist[playername].hudid)
end