Update invisibility potions and poisons to utilize globalstep. Seems smoother.

This commit is contained in:
Brandon 2020-06-20 13:30:35 -04:00
parent a8beeb9f9b
commit 3dbaac7549

View file

@ -1,32 +1,68 @@
local invisibility = {} local is_invisible = {}
local poisoned = {} local is_poisoned = {}
local regenerating = {} local is_regenerating = {}
local strong = {} local is_strong = {}
local weak = {} local is_weak = {}
-- reset player invisibility/poison if they go offline local timer = 0
minetest.register_globalstep(function(dtime)
-- Check for invisible players
for player, bool in pairs(is_invisible) do
if is_invisible[player] then
mcl_potions._add_spawner(player, "#B0B0B0")
end
end
-- Check for poisoned players
for player, bool in pairs(is_poisoned) do
if is_poisoned[player] then
player = player or player:get_luaentity()
is_poisoned[player].timer = is_poisoned[player].timer + dtime
is_poisoned[player].hit_timer = (is_poisoned[player].hit_timer or 0) + dtime
mcl_potions._add_spawner(player, "#225533")
if is_poisoned[player].hit_timer >= is_poisoned[player].step then
player:set_hp( math.max(player:get_hp() - 1, 1) )
is_poisoned[player].hit_timer = 0
end
if is_poisoned[player].timer >= is_poisoned[player].dur then
is_poisoned[player] = nil
end
end
end
end )
-- reset player is_invisible/poison if they go offline
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
if invisibility[name] then if is_invisible[name] then
invisibility[name] = nil is_invisible[name] = nil
end end
if poisoned[name] then if is_poisoned[name] then
poisoned[name] = nil is_poisoned[name] = nil
end end
if regenerating[name] then if is_regenerating[name] then
regenerating[name] = nil is_regenerating[name] = nil
end end
if strong[name] then if is_strong[name] then
strong[name] = nil is_strong[name] = nil
end end
if weak[name] then if is_weak[name] then
weak[name] = nil is_weak[name] = nil
end end
end) end)
@ -35,7 +71,7 @@ function mcl_potions.invisible(player, toggle)
if not player then return false end if not player then return false end
invisibility[player:get_player_name()] = toggle is_invisible[player:get_player_name()] = toggle
if toggle then -- hide player if toggle then -- hide player
player:set_properties({visual_size = {x = 0, y = 0}}) player:set_properties({visual_size = {x = 0, y = 0}})
@ -50,14 +86,14 @@ end
function mcl_potions.poison(player, toggle) function mcl_potions.poison(player, toggle)
if not player then return false end if not player then return false end
poisoned[player:get_player_name()] = toggle is_poisoned[player:get_player_name()] = toggle
end end
function mcl_potions.regenerate(player, toggle) function mcl_potions.regenerate(player, toggle)
if not player then return false end if not player then return false end
regenerating[player:get_player_name()] = toggle is_regenerating[player:get_player_name()] = toggle
end end
@ -90,7 +126,7 @@ function mcl_potions._add_spawner(obj, color)
local d = 0.2 local d = 0.2
local pos = obj:get_pos() local pos = obj:get_pos()
minetest.add_particlespawner({ minetest.add_particlespawner({
amount = 5, amount = 2,
time = 1, time = 1,
minpos = {x=pos.x-d, y=pos.y+1, z=pos.z-d}, minpos = {x=pos.x-d, y=pos.y+1, z=pos.z-d},
maxpos = {x=pos.x+d, y=pos.y+2, z=pos.z+d}, maxpos = {x=pos.x+d, y=pos.y+2, z=pos.z+d},
@ -157,24 +193,23 @@ end
function mcl_potions.poison_func(player, factor, duration) function mcl_potions.poison_func(player, factor, duration)
if not poisoned[player:get_player_name()] then if not is_poisoned[player] then
mcl_potions.poison(player, true)
for i=1,math.floor(duration/factor) do is_poisoned[player] = {step = factor, dur = duration, timer = 0}
minetest.after(i*factor, function()
if poisoned[player:get_player_name()] then else
player:set_hp(math.max(player:get_hp() - 1,1))
end local victim = is_poisoned[player]
end)
end victim.step = math.min(victim.step, factor)
for i=1,math.floor(duration) do victim.dur = math.max(duration, victim.dur - victim.timer)
minetest.after(i, function() mcl_potions._add_spawner(player, "#225533") end) victim.timer = 0
end
minetest.after(duration, function() mcl_potions.poison(player, false) end)
end end
end end
function mcl_potions.regeneration_func(player, factor, duration) function mcl_potions.regeneration_func(player, factor, duration)
if not regenerating[player:get_player_name()] then if not is_regenerating[player:get_player_name()] then
mcl_potions.regenerate(player, true) mcl_potions.regenerate(player, true)
for i=1,math.floor(duration/factor) do for i=1,math.floor(duration/factor) do
minetest.after(i*factor, function() minetest.after(i*factor, function()
@ -194,11 +229,6 @@ end
function mcl_potions.invisiblility_func(player, duration) function mcl_potions.invisiblility_func(player, duration)
mcl_potions.invisible(player, true) mcl_potions.invisible(player, true)
minetest.after(duration, function() mcl_potions.invisible(player, false) end ) minetest.after(duration, function() mcl_potions.invisible(player, false) end )
for i=1,math.floor(duration) do
minetest.after(i, function() mcl_potions._add_spawner(player, "#B0B0B0") end)
end
end end
function mcl_potions.water_breathing_func(player, duration) function mcl_potions.water_breathing_func(player, duration)