diff --git a/examples.lua b/examples.lua index df438ee..d7736a0 100644 --- a/examples.lua +++ b/examples.lua @@ -1,6 +1,6 @@ ----- EXAMPLE EFFECT TYPES ----- -playereffects.register_effect_type("high_speed", "high speed", {"speed"}, +playereffects.register_effect_type("high_speed", "High speed", nil, {"speed"}, function(player) player:set_physics_override(4,nil,nil) end, @@ -10,7 +10,7 @@ playereffects.register_effect_type("high_speed", "high speed", {"speed"}, player:set_physics_override(1,nil,nil) end ) -playereffects.register_effect_type("low_speed", "low speed", {"speed"}, +playereffects.register_effect_type("low_speed", "Low speed", nil, {"speed"}, function(player) player:set_physics_override(0.25,nil,nil) end, @@ -20,7 +20,7 @@ playereffects.register_effect_type("low_speed", "low speed", {"speed"}, player:set_physics_override(1,nil,nil) end ) -playereffects.register_effect_type("highjump", "greater jump height", {"jump"}, +playereffects.register_effect_type("highjump", "Greater jump height", "playereffects_example_highjump.png", {"jump"}, function(player) player:set_physics_override(nil,2,nil) end, @@ -29,7 +29,7 @@ playereffects.register_effect_type("highjump", "greater jump height", {"jump"}, player:set_physics_override(nil,1,nil) end ) -playereffects.register_effect_type("fly", "fly mode available", {"fly"}, +playereffects.register_effect_type("fly", "Fly mode available", "playereffects_example_fly.png", {"fly"}, function(player) local playername = player:get_player_name() local privs = minetest.get_player_privs(playername) diff --git a/init.lua b/init.lua index 69c2f42..a8159c5 100644 --- a/init.lua +++ b/init.lua @@ -45,11 +45,12 @@ function playereffects.next_effect_id() end --[=[ API functions ]=] -function playereffects.register_effect_type(name, description, groups, apply, cancel) +function playereffects.register_effect_type(name, description, icon, groups, apply, cancel) effect_type = {} effect_type.description = description effect_type.apply = apply effect_type.groups = groups + effect_type.icon = icon if cancel ~= nil then effect_type.cancel = cancel else @@ -89,7 +90,7 @@ function playereffects.apply_effect_type(effect_type_id, duration, player) else free_hudpos = biggest_hudpos + 1 end - local hudid = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration) + local hudids = playereffects.hud_effect(effect_type_id, player, free_hudpos, duration) local effect = { playername = playername, @@ -97,7 +98,7 @@ function playereffects.apply_effect_type(effect_type_id, duration, player) effect_type_id = effect_type_id, start_time = start_time, time_left = duration, - hudid = hudid, + hudids = hudids, hudpos = free_hudpos, } @@ -133,7 +134,10 @@ function playereffects.cancel_effect(effect_id) local effect = playereffects.effects[effect_id] if(effect ~= nil) then local player = minetest.get_player_by_name(effect.playername) - player:hud_remove(effect.hudid) + player:hud_remove(effect.hudids.text_id) + if(effect.hudids.icon_id~=nil) then + player:hud_remove(effect.hudids.icon_id) + end playereffects.effect_types[effect.effect_type_id].cancel(effect) playereffects.effects[effect_id] = nil minetest.log("action", "Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!") @@ -248,7 +252,7 @@ function playereffects.hud_update(player) local effect = effects[e] 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(effect.hudid, "text", description .. " ("..tostring(time_left).." s)") + player:hud_change(effect.hudids.text_id, "text", description .. " ("..tostring(time_left).." s)") end end @@ -257,26 +261,40 @@ function playereffects.hud_clear(player) local effects = playereffects.get_player_effects(playername) if(effects ~= nil) then for e=1,#effects do - player:hud_remove(effects[e].hudid) + player:hud_remove(effects[e].hudids.text_id) + if(effects[e].hudids.icon_id ~= nil) then + player:hud_remove(effects[e].hudids.icon_id) + end end end end function playereffects.hud_effect(effect_type_id, player, pos, time_left) - local id - id = player:hud_add({ + local text_id, icon_id + text_id = player:hud_add({ hud_elem_type = "text", position = { x = 1, y = 0.3 }, name = "effect_"..effect_type_id, text = playereffects.effect_types[effect_type_id].description .. " ("..tostring(time_left).." s)", - scale = { x = 120, y = 20}, - alignment = 1, + scale = { x = 170, y = 20}, + alignment = { x = -1, y = 0 }, direction = 1, number = 0xFFFFFF, - offset = { x = -120, y = pos*20 } + offset = { x = -5, y = pos*20 } }) - local playername = player:get_player_name() - return id + if(playereffects.effect_types[effect_type_id].icon ~= nil) then + icon_id = player:hud_add({ + hud_elem_type = "image", + scale = { x = 1, y = 1 }, + position = { x = 1, y = 0.3 }, + name = "effect_icon_"..effect_type_id, + text = playereffects.effect_types[effect_type_id].icon, + alignment = { x = -1, y=0 }, + direction = 0, + offset = { x = -186, y = pos*20 }, + }) + end + return { text_id = text_id, icon_id = icon_id } end diff --git a/textures/playereffects_example_fly.png b/textures/playereffects_example_fly.png new file mode 100644 index 0000000..9aad9cc Binary files /dev/null and b/textures/playereffects_example_fly.png differ diff --git a/textures/playereffects_example_highjump.png b/textures/playereffects_example_highjump.png new file mode 100644 index 0000000..27dc5aa Binary files /dev/null and b/textures/playereffects_example_highjump.png differ