Add different music for different dimensions

This commit is contained in:
kay27 2022-01-21 19:32:27 +04:00 committed by Gitea
parent 1894d8c5f0
commit 7ceb953a56
3 changed files with 39 additions and 17 deletions

View File

@ -1,7 +1,15 @@
local modname = minetest.get_current_modname() local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
local pianowtune = "diminixed-pianowtune01" local pianowtune = "diminixed-pianowtune01"
local end_tune = "diminixed-ambientwip"
local nether_tune = "horizonchris96-traitor"
local dimension_to_base_track = {
["overworld"] = pianowtune,
["nether"] = nether_tune,
["end"] = end_tune,
}
local listeners = {} local listeners = {}
@ -25,54 +33,68 @@ local function stop()
end end
local function play() local function play()
local spec = {
name = pianowtune,
gain = 0.3,
pitch = 1.0,
}
local new_weather_state = mcl_weather.get_weather() local new_weather_state = mcl_weather.get_weather()
local was_good_weather = weather_state == "none" or weather_state == "clear" local was_good_weather = weather_state == "none" or weather_state == "clear"
weather_state = new_weather_state weather_state = new_weather_state
local is_good_weather = weather_state == "none" or weather_state == "clear" local is_good_weather = weather_state == "none" or weather_state == "clear"
local is_weather_changed = weather_state ~= new_weather_state local is_weather_changed = weather_state ~= new_weather_state
if is_weather_changed or not is_good_weather then
stop()
minetest.after(20, play)
return
end
local time = minetest.get_timeofday() local time = minetest.get_timeofday()
if time < 0.25 or time >= 0.75 then if time < 0.25 or time >= 0.75 then
stop() stop()
minetest.after(10, play) minetest.after(10, play)
return return
end end
local day_count = minetest.get_day_count()
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(minetest.get_connected_players()) do
local player_name = player:get_player_name() local player_name = player:get_player_name()
local hp = player:get_hp() local hp = player:get_hp()
local pos = player:get_pos() local pos = player:get_pos()
local listener = listeners[player_name]
local old_hp = listener and listener.hp
local dimension = mcl_worlds.pos_to_dimension(pos) local dimension = mcl_worlds.pos_to_dimension(pos)
local is_hp_changed = old_hp and math.abs(old_hp - hp) > 0.00001
local listener = listeners[player_name]
local old_hp = listener and listener.hp
local old_dimension = listener and listener.dimension
local is_dimension_changed = old_dimension and (old_dimension ~= dimension) or false
local is_hp_changed = old_hp and (math.abs(old_hp - hp) > 0.00001) or false
local handle = listener and listener.handle local handle = listener and listener.handle
if is_hp_changed then
local track = dimension_to_base_track[dimension]
if is_hp_changed
or is_dimension_changed
or (dimension == "overworld" and (is_weather_changed or not is_good_weather))
or not track
or (listener and (listener.day_count == day_count))
then
minetest.chat_send_all("here! dc = "..tostring(is_dimension_changed))
stop_music_for_listener_name(player_name) stop_music_for_listener_name(player_name)
if not listeners[player_name] then
listeners[player_name] = {}
end
listeners[player_name].hp = hp listeners[player_name].hp = hp
elseif dimension ~= "overworld" then listeners[player_name].dimension = dimension
stop_music_for_listener_name(player_name)
elseif not handle then elseif not handle then
local spec = {
name = track,
gain = 0.3,
pitch = 1.0,
}
local parameters = { local parameters = {
to_player = player_name, to_player = player_name,
gain = 1.0, gain = 1.0,
fade = 0.0, fade = 0.0,
pitch = 1.0, pitch = 1.0,
} }
handle = minetest.sound_play(spec, parameters, false) handle = minetest.sound_play(spec, parameters, false)
listeners[player_name] = { listeners[player_name] = {
spec = spec, spec = spec,
parameters = parameters, parameters = parameters,
handle = handle, handle = handle,
hp = hp, hp = hp,
dimension = dimension,
day_count = day_count,
} }
end end
end end

Binary file not shown.