forked from VoxeLibre/VoxeLibre
Add player invulnerability & fix not continuously damaging players when holding the attack key
Player invulnerability is the same as Minecraft's Damage Immunity https://minecraft.wiki/w/Damage#Immunity The old code for some reason only allows a few damage by holding and does not continuously damage other players after a few hits
This commit is contained in:
parent
ebd733be82
commit
a8806fe04e
|
@ -663,6 +663,8 @@ minetest.register_on_joinplayer(function(player)
|
||||||
lastPos = nil,
|
lastPos = nil,
|
||||||
swimDistance = 0,
|
swimDistance = 0,
|
||||||
jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly
|
jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly
|
||||||
|
last_damage = 0,
|
||||||
|
invul_timestamp = 0,
|
||||||
}
|
}
|
||||||
mcl_playerplus.elytra[player] = {active = false, rocketing = 0, speed = 0}
|
mcl_playerplus.elytra[player] = {active = false, rocketing = 0, speed = 0}
|
||||||
|
|
||||||
|
@ -727,19 +729,35 @@ mcl_damage.register_modifier(function(obj, damage, reason)
|
||||||
end
|
end
|
||||||
end, -200)
|
end, -200)
|
||||||
|
|
||||||
-- damage invulnerability
|
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
|
||||||
mcl_damage.register_modifier(function(obj, damage, reason)
|
-- damage invulnerability
|
||||||
local invul = obj:get_meta():get_int("mcl_damage:invulnerable")
|
if hitter then
|
||||||
if invul > 0 then
|
local name = player:get_player_name()
|
||||||
return 0
|
local time_now = minetest.get_us_time()
|
||||||
|
local invul_timestamp = mcl_playerplus_internal[name].invul_timestamp
|
||||||
|
local time_diff = time_now - invul_timestamp
|
||||||
|
-- check for invulnerability time in microseconds (0.5 second)
|
||||||
|
if time_diff <= 500000 and time_diff >= 0 then
|
||||||
|
damage = damage - mcl_playerplus_internal[name].last_damage
|
||||||
|
if damage < 0 then
|
||||||
|
damage = 0
|
||||||
|
end
|
||||||
|
return damage
|
||||||
else
|
else
|
||||||
obj:get_meta():set_int("mcl_damage:invulnerable", 1)
|
mcl_playerplus_internal[name].last_damage = damage
|
||||||
minetest.after(0.5, function()
|
mcl_playerplus_internal[name].invul_timestamp = time_now
|
||||||
obj:get_meta():set_int("mcl_damage:invulnerable", 0)
|
end
|
||||||
end)
|
end
|
||||||
|
-- attack reach limit
|
||||||
|
if hitter and hitter:is_player() then
|
||||||
|
local player_pos = player:get_pos()
|
||||||
|
local hitter_pos = hitter:get_pos()
|
||||||
|
if vector.distance(player_pos, hitter_pos) > 3 then
|
||||||
|
damage = 0
|
||||||
return damage
|
return damage
|
||||||
end
|
end
|
||||||
end, -1000)
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_respawnplayer(function(player)
|
minetest.register_on_respawnplayer(function(player)
|
||||||
local pos = player:get_pos()
|
local pos = player:get_pos()
|
||||||
|
|
Loading…
Reference in New Issue