Rewrite the way HUD IDs are handled

This commit is contained in:
Wuzzy 2014-07-17 17:06:06 +02:00
parent 54b18385c8
commit dcc930c499
2 changed files with 54 additions and 29 deletions

View File

@ -83,8 +83,6 @@ Internally, Player Effects also uses these fields:
* `start_time`: The operating system time (from `os.time()`) of when the effect has been started. * `start_time`: The operating system time (from `os.time()`) of when the effect has been started.
* `time_left`: The number of seconds left before the effect runs out. This number is only set when the effect starts or the effect is unfrozen because i.e. a player re-joins. You cant use this field to blindly get the remaining time of the effect. * `time_left`: The number of seconds left before the effect runs out. This number is only set when the effect starts or the effect is unfrozen because i.e. a player re-joins. You cant use this field to blindly get the remaining time of the effect.
* `hudids`: A table of HUD IDs which belong to this effect. The fields are: `icon_id` for the HUD ID of the icon and `text_id` for the HUD ID of the description text.
* `hudpos`: The Y offset factor of the effect text and the icon.
You should normally not need to care about these internally used fields. You should normally not need to care about these internally used fields.

View File

@ -5,6 +5,16 @@ playereffects = {}
--[[ table containing the groups (experimental) ]] --[[ table containing the groups (experimental) ]]
playereffects.groups = {} playereffects.groups = {}
--[[ table containing all the HUD info tables, indexed by player names.
A single HUD info table is formatted like this: { text_id = 1, icon_id=2, pos = 0 }
Where: text_id: HUD ID of the textual effect description
icon_id: HUD ID of the effect icon (optional)
pos: Y offset factor (starts with 0)
Example of full table:
{ ["player1"] = {{ text_id = 1, icon_id=4, pos = 0 }}, ["player2] = { { text_id = 5, icon_id=6, pos = 0 }, { text_id = 7, icon_id=8, pos = 1 } } }
]]
playereffects.hudinfos = {}
--[[ table containing all the effect types ]] --[[ table containing all the effect types ]]
playereffects.effect_types = {} playereffects.effect_types = {}
@ -109,8 +119,12 @@ function playereffects.apply_effect_type(effect_type_id, duration, player)
local smallest_hudpos local smallest_hudpos
local biggest_hudpos = -1 local biggest_hudpos = -1
local free_hudpos local free_hudpos
for e=1,#effects do if(playereffects.hudinfos[playername] == nil) then
local hudpos = effects[e].hudpos playereffects.hudinfos[playername] = {}
end
local hudinfos = playereffects.hudinfos[playername]
for effect_id, hudinfo in pairs(hudinfos) do
local hudpos = hudinfo.pos
if(hudpos > biggest_hudpos) then if(hudpos > biggest_hudpos) then
biggest_hudpos = hudpos biggest_hudpos = hudpos
end end
@ -127,12 +141,18 @@ function playereffects.apply_effect_type(effect_type_id, duration, player)
else else
free_hudpos = biggest_hudpos + 1 free_hudpos = biggest_hudpos + 1
end end
local hudids
--[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]] --[[ show no more than 20 effects on the screen, so that hud_update does not need to be called so often ]]
local text_id, icon_id
if(free_hudpos <= 20) then if(free_hudpos <= 20) then
hudids = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration) text_id, icon_id = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration)
local hudinfo = {
text_id = text_id,
icon_id = icon_id,
pos = free_hudpos,
}
playereffects.hudinfos[playername][effect_id] = hudinfo
else else
hudids = {text_id=nil, icon_id=nil} text_id, icon_id = nil, nil
end end
local effect = { local effect = {
@ -141,8 +161,6 @@ function playereffects.apply_effect_type(effect_type_id, duration, player)
effect_type_id = effect_type_id, effect_type_id = effect_type_id,
start_time = start_time, start_time = start_time,
time_left = duration, time_left = duration,
hudids = hudids,
hudpos = free_hudpos,
metadata = metadata, metadata = metadata,
} }
@ -186,11 +204,15 @@ function playereffects.cancel_effect(effect_id)
local effect = playereffects.effects[effect_id] local effect = playereffects.effects[effect_id]
if(effect ~= nil) then if(effect ~= nil) then
local player = minetest.get_player_by_name(effect.playername) local player = minetest.get_player_by_name(effect.playername)
if(effect.hudids.text_id~=nil) then local hudinfo = playereffects.hudinfos[effect.playername][effect_id]
player:hud_remove(effect.hudids.text_id) if(hudinfo ~= nil) then
end if(hudinfo.text_id~=nil) then
if(effect.hudids.icon_id~=nil) then player:hud_remove(hudinfo.text_id)
player:hud_remove(effect.hudids.icon_id) end
if(hudinfo.icon_id~=nil) then
player:hud_remove(hudinfo.icon_id)
end
playereffects.hudinfos[effect.playername][effect_id] = nil
end end
playereffects.effect_types[effect.effect_type_id].cancel(effect, player) playereffects.effect_types[effect.effect_type_id].cancel(effect, player)
playereffects.effects[effect_id] = nil playereffects.effects[effect_id] = nil
@ -336,13 +358,16 @@ end)
function playereffects.hud_update(player) function playereffects.hud_update(player)
if(playereffects.use_hud == true) then if(playereffects.use_hud == true) then
local now = os.time() local now = os.time()
local effects = playereffects.get_player_effects(player:get_player_name()) local playername = player:get_player_name()
for e=1,#effects do local hudinfos = playereffects.hudinfos[playername]
local effect = effects[e] if(hudinfos ~= nil) then
if(effect.hudids.text_id ~= nil) then for effect_id, hudinfo in pairs(hudinfos) do
local description = playereffects.effect_types[effect.effect_type_id].description local effect = playereffects.effects[effect_id]
local time_left = os.difftime(effect.start_time + effect.time_left, now) if(effect ~= nil and hudinfo.text_id ~= nil) then
player:hud_change(effect.hudids.text_id, "text", description .. " ("..tostring(time_left).." s)") local description = playereffects.effect_types[effect.effect_type_id].description
local time_left = os.difftime(effect.start_time + effect.time_left, now)
player:hud_change(hudinfo.text_id, "text", description .. " ("..tostring(time_left).." s)")
end
end end
end end
end end
@ -351,15 +376,17 @@ end
function playereffects.hud_clear(player) function playereffects.hud_clear(player)
if(playereffects.use_hud == true) then if(playereffects.use_hud == true) then
local playername = player:get_player_name() local playername = player:get_player_name()
local effects = playereffects.get_player_effects(playername) local hudinfos = playereffects.hudinfos[playername]
if(effects ~= nil) then if(hudinfos ~= nil) then
for e=1,#effects do for effect_id, hudinfo in pairs(hudinfos) do
if(effects[e].hudids.text_id ~= nil) then local effect = playereffects.effects[effect_id]
player:hud_remove(effects[e].hudids.text_id) if(hudinfo.text_id ~= nil) then
player:hud_remove(hudinfo.text_id)
end end
if(effects[e].hudids.icon_id ~= nil) then if(hudinfo.icon_id ~= nil) then
player:hud_remove(effects[e].hudids.icon_id) player:hud_remove(hudinfo.icon_id)
end end
playereffects.hudinfos[playername][effect_id] = nil
end end
end end
end end
@ -402,7 +429,7 @@ function playereffects.hud_effect(effect_type_id, player, pos, time_left)
text_id = nil text_id = nil
icon_id = nil icon_id = nil
end end
return { text_id = text_id, icon_id = icon_id } return text_id, icon_id
end end