Implement sounds, enable start and stop events

This commit is contained in:
Till Affeldt
2020-04-14 11:25:00 +02:00
parent 2090aff6bd
commit 299d520af6
9 changed files with 146 additions and 22 deletions

View File

@ -4,7 +4,7 @@ if not minetest.get_modpath("skylayer") then return end
local SKYBOX_NAME = "climate_api:clouds"
local function set_clouds(player, clouds)
sky = { name = SKYBOX_NAME, cloud_data = clouds }
local sky = { name = SKYBOX_NAME, cloud_data = clouds }
skylayer.add_layer(player:get_player_name(), sky)
end
@ -44,5 +44,5 @@ local function remove_effect(player_data)
end
climate_api.register_effect("climate_api:clouds", handle_effect, "tick")
climate_api.register_effect("climate_api:clouds", remove_effect, "end")
climate_api.register_effect("climate_api:clouds", remove_effect, "stop")
climate_api.set_effect_cycle("climate_api:clouds", climate_api.LONG_CYCLE)

View File

@ -31,5 +31,5 @@ local function remove_effect(player_data)
end
climate_api.register_effect("climate_api:skybox", handle_effect, "tick")
climate_api.register_effect("climate_api:skybox", remove_effect, "end")
climate_api.register_effect("climate_api:skybox", remove_effect, "stop")
climate_api.set_effect_cycle("climate_api:skybox", climate_api.LONG_CYCLE)

View File

@ -1,14 +1,59 @@
if not climate_mod.settings.sound then return end
return
local handles = {}
local function start_sound(pname, weather, sound)
if handles[pname] == nil then handles[pname] = {} end
if handles[pname][weather] ~= nil then return end
local handle = minetest.sound_play(sound, {
to_player = pname,
loop = true
})
handles[pname][weather] = handle
end
local function update_effect(player_data)
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_stop(handle)
handles[pname][weather] = nil
end
local function start_effect(player_data)
for playername, data in pairs(player_data) do
for weather, value in pairs(data) do
climate_mod.effects.play_sound(player, value)
start_sound(playername, weather, value)
end
end
end
climate_api.register_effect("climate_api:sound", update_effect, "change")
climate_api.set_effect_cycle("climate_api:skybox", climate_api.MEDIUM_CYCLE)
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, sound)
end
end
end
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, sound)
end
end
end
end
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)
end
end
end
climate_api.register_effect("climate_api:sound", start_effect, "start")
climate_api.register_effect("climate_api:sound", handle_effect, "tick")
climate_api.register_effect("climate_api:sound", stop_effect, "stop")
climate_api.set_effect_cycle("climate_api:sound", 0)