62 lines
1.8 KiB
Lua
62 lines
1.8 KiB
Lua
lzr_ambience = {}
|
|
|
|
local S = minetest.get_translator("lzr_ambience")
|
|
|
|
local ambiences = {}
|
|
|
|
local current_singleplayer_ambience = nil
|
|
local current_singleplayer_ambience_id = nil
|
|
|
|
lzr_ambience.add_ambience = function(id, soundname, gain)
|
|
ambiences[id] = { soundname = soundname, gain = gain }
|
|
end
|
|
|
|
function lzr_ambience.stop_ambience()
|
|
if current_singleplayer_ambience then
|
|
minetest.sound_stop(current_singleplayer_ambience)
|
|
current_singleplayer_ambience = nil
|
|
end
|
|
end
|
|
|
|
function lzr_ambience.set_ambience(id)
|
|
local ambience = ambiences[id]
|
|
if not ambience then
|
|
minetest.log("error", "[lzr_ambience] set_ambience called with invalid ambience ID ("..tostring(id)..")")
|
|
return false
|
|
end
|
|
minetest.log("info", "[lzr_ambience] Ambience set to: "..tostring(id))
|
|
-- No-op if we already have this ambience active
|
|
if current_singleplayer_ambience_id == id then
|
|
return true
|
|
end
|
|
lzr_ambience.stop_ambience()
|
|
current_singleplayer_ambience = minetest.sound_play({name=ambience.soundname}, {gain = ambience.gain, to_player="singleplayer", loop=true})
|
|
current_singleplayer_ambience_id = id
|
|
return true
|
|
end
|
|
|
|
lzr_ambience.add_ambience("ocean", "lzr_ambience_ocean", 0.08)
|
|
lzr_ambience.add_ambience("temple", "lzr_ambience_temple", 1.0)
|
|
|
|
local setting = minetest.settings:get_bool("lzr_ambience_start_with_ambience")
|
|
if setting == nil then setting = true end
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
if setting then
|
|
lzr_ambience.set_ambience("ocean")
|
|
end
|
|
end)
|
|
|
|
minetest.register_chatcommand("ambience", {
|
|
description = S("Toggle ambience sounds"),
|
|
func = function()
|
|
if current_singleplayer_ambience then
|
|
lzr_ambience.stop_ambience()
|
|
return true, S("Ambience sounds disabled.")
|
|
else
|
|
lzr_ambience.set_ambience(current_singleplayer_ambience_id)
|
|
return true, S("Ambience sounds enabled.")
|
|
end
|
|
end,
|
|
})
|