Debounce wield selection sounds.

This commit is contained in:
Aaron Suen 2019-04-14 00:44:18 -04:00
parent 24bbeb135b
commit e09ee314fc

View File

@ -15,7 +15,7 @@ minetest.register_item(":", {
})
})
local function wieldsound(player, idx, gain, delay)
local function wieldsound(player, idx, gain)
local n = player:get_inventory():get_stack("main", idx):get_name()
local def = minetest.registered_items[n]
if def and def.sounds then
@ -23,27 +23,38 @@ local function wieldsound(player, idx, gain, delay)
for k, v in pairs(def.sounds.dig) do t[k] = v end
t.pos = player:get_pos()
t.gain = gain
if delay then
return minetest.after(delay, function()
return minetest.sound_play(t.name, t)
end)
else
return minetest.sound_play(t.name, t)
end
return function() minetest.sound_play(t.name, t) end
end
return function() end
end
local wields = {}
local pending = {}
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
local pend = pending[pname]
if pend then
pend.t = pend.t - dtime
if pend.t <= 0 then
pend.f()
pending[pname] = nil
pend = nil
end
end
local idx = player:get_wield_index()
local old = wields[pname]
if idx ~= old then
if old then wieldsound(player, old, 0.07) end
wieldsound(player, idx, 0.15, 0.1)
wields[pname] = idx
if old and not pend then
wieldsound(player, old, 0.07)()
end
pending[pname] = {
t = 0.15,
f = wieldsound(player, idx, 0.15)
}
end
end
end)