sound.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. --[[
  2. # Sound Loop Effect
  3. Use this effect to loop an ambient sound effect
  4. Expects a table as the parameter containing the following values:
  5. - ``name <string>``: Name of the played sound effect (without .ogg file ending)
  6. - ``gain <number>`` (optional): Volume of the sound (defaults to 1.0)
  7. - ``pitch <number>`` (optional): Pitch of the sound (defaults to 1.0)
  8. ]]
  9. if not climate_mod.settings.sound then return end
  10. local EFFECT_NAME = "climate_api:sound"
  11. local FADE_DURATION = climate_api.LONG_CYCLE
  12. local modpath = minetest.get_modpath(minetest.get_current_modname())
  13. local soundloop = dofile(modpath .. "/lib/soundloop.lua")
  14. local function start_sound(pname, sound)
  15. return soundloop.play(pname, sound, FADE_DURATION)
  16. end
  17. local function stop_sound(pname, sound)
  18. return soundloop.stop(pname, sound, FADE_DURATION)
  19. end
  20. local function start_effect(player_data)
  21. for playername, data in pairs(player_data) do
  22. for weather, value in pairs(data) do
  23. start_sound(playername, value)
  24. end
  25. end
  26. end
  27. local function handle_effect(player_data, prev_data)
  28. for playername, data in pairs(player_data) do
  29. for weather, value in pairs(data) do
  30. if prev_data[playername][weather] == nil then
  31. start_sound(playername, value)
  32. end
  33. end
  34. end
  35. for playername, data in pairs(prev_data) do
  36. for weather, value in pairs(data) do
  37. if player_data[playername][weather] == nil then
  38. stop_sound(playername, value)
  39. end
  40. end
  41. end
  42. end
  43. local function stop_effect(prev_data)
  44. for playername, data in pairs(prev_data) do
  45. for weather, value in pairs(data) do
  46. stop_sound(playername, value)
  47. end
  48. end
  49. end
  50. climate_api.register_effect(EFFECT_NAME, start_effect, "start")
  51. climate_api.register_effect(EFFECT_NAME, handle_effect, "tick")
  52. climate_api.register_effect(EFFECT_NAME, stop_effect, "stop")