Sound improvements

This commit is contained in:
Till Affeldt 2020-04-18 17:24:59 +02:00
parent a6cfca7745
commit 0c5c9f0d15
4 changed files with 77 additions and 38 deletions

View File

@ -18,6 +18,7 @@
- Make switches between effects more smooth
- Adjust size of particle boxes based on player speed
- Create conditions for time of day, annual progression, biome filters
- Generate wind based on speed and yaw instead of x and z values
## Future Plans & Ideas
- Complete season system

View File

@ -3,48 +3,21 @@ if not climate_mod.settings.sound then return end
local EFFECT_NAME = "climate_api:sound"
local FADE_DURATION = climate_api.LONG_CYCLE
local handles = {}
local removables = {}
local function end_sound(pname, weather, sound)
if removables[pname] == nil
or removables[pname][weather] == nil then return end
local handle = removables[pname][weather]
minetest.sound_stop(handle)
removables[pname][weather] = nil
local modpath = minetest.get_modpath(minetest.get_current_modname())
local soundloop = dofile(modpath .. "/lib/soundloop.lua")
local function start_sound(pname, sound)
return soundloop.play(pname, sound, FADE_DURATION)
end
local function start_sound(pname, weather, sound)
local handle
if handles[pname] == nil then handles[pname] = {} end
if handles[pname][weather] ~= nil then return end
if removables[pname] == nil or removables[pname][weather] == nil then
handle = minetest.sound_play(sound.name, {
to_player = pname,
loop = true,
gain = 0
})
else
handle = removables[pname][weather]
removables[pname][weather] = nil
end
minetest.sound_fade(handle, sound.gain / FADE_DURATION, sound.gain)
handles[pname][weather] = handle
end
local function stop_sound(pname, weather, sound)
if handles[pname] == nil or handles[pname][weather] == nil then return end
local handle = handles[pname][weather]
minetest.sound_fade(handle, -sound.gain / FADE_DURATION, 0)
if removables[pname] == nil then removables[pname] = {} end
removables[pname][weather] = handle
handles[pname][weather] = nil
minetest.after(FADE_DURATION, end_sound, pname, weather, sound)
local function stop_sound(pname, sound)
return soundloop.stop(pname, sound, FADE_DURATION)
end
local function start_effect(player_data)
for playername, data in pairs(player_data) do
for weather, value in pairs(data) do
start_sound(playername, weather, value)
start_sound(playername, value)
end
end
end
@ -53,7 +26,7 @@ local function handle_effect(player_data, prev_data)
for playername, data in pairs(player_data) do
for weather, value in pairs(data) do
if prev_data[playername][weather] == nil then
start_sound(playername, weather, value)
start_sound(playername, value)
end
end
end
@ -61,7 +34,7 @@ local function handle_effect(player_data, prev_data)
for playername, data in pairs(prev_data) do
for weather, value in pairs(data) do
if player_data[playername][weather] == nil then
stop_sound(playername, weather, value)
stop_sound(playername, value)
end
end
end
@ -71,7 +44,7 @@ local function stop_effect(prev_data)
minetest.log(dump2(prev_data, "stop_effect"))
for playername, data in pairs(prev_data) do
for weather, value in pairs(data) do
stop_sound(playername, weather, value)
stop_sound(playername, value)
end
end
end

View File

@ -2,10 +2,18 @@ climate_api.register_influence("heat", function(pos)
return climate_api.environment.get_heat(pos)
end)
climate_api.register_influence("base_heat", function(pos)
return minetest.get_heat(pos)
end)
climate_api.register_influence("humidity", function(pos)
return climate_api.environment.get_humidity(pos)
end)
climate_api.register_influence("base_humidity", function(pos)
return minetest.get_humidity(pos)
end)
climate_api.register_influence("biome", function(pos)
local data = minetest.get_biome_data(pos)
local biome = minetest.get_biome_name(data.biome)

57
lib/soundloop.lua Normal file
View File

@ -0,0 +1,57 @@
local soundloop = {}
local sounds = {}
local function parse_sound(sound)
if type(sound) == "string" then
return { name = sound, gain = 1, pitch = 1 }
end
if sound.gain == nil then sound.gain = 1 end
if sound.pitch == nil then sound.pitch = 1 end
return sound
end
soundloop.play = function(player, sound, fade)
sound = parse_sound(sound)
if fade == nil then fade = 1 end
local step
local handle
local start_gain
if sounds[player] == nil then sounds[player] = {} end
if sounds[player][sound.name] == nil then
step = sound.gain / fade
start_gain = 0
elseif sounds[player][sound.name] ~= sound.gain then
minetest.sound_stop(sounds[player][sound.name].handle)
start_gain = sounds[player][sound.name].gain
local change = sound.gain - start_gain
step = change / fade
else
return
end
handle = minetest.sound_play(sound.name, {
to_player = player,
loop = true,
gain = 0
})
sounds[player][sound.name] = {
gain = sound.gain,
handle = handle
}
minetest.sound_fade(handle, step, sound.gain)
return handle
end
soundloop.stop = function(player, sound, fade)
sound = parse_sound(sound)
if sounds[player] == nil or sounds[player][sound.name] == nil then
return
end
if fade == nil then fade = 1 end
local handle = sounds[player][sound.name].handle
local step = -sounds[player][sound.name].gain / fade
minetest.sound_fade(handle, step, 0)
sounds[player][sound.name].gain = 0
minetest.after(fade, minetest.sound_stop, handle)
end
return soundloop