ordering / stop sound
parent
a3019ea488
commit
63770caaf2
8
api.lua
8
api.lua
|
@ -11,5 +11,11 @@ def = {
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
soundblock.register = function(def)
|
soundblock.register = function(def)
|
||||||
soundblock.sounds[def.key] = def
|
table.insert(soundblock.sounds, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
minetest.register_on_mods_loaded(function()
|
||||||
|
table.sort(soundblock.sounds, function(a, b)
|
||||||
|
return a.key < b.key
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
32
form.lua
32
form.lua
|
@ -26,18 +26,16 @@ soundblock.showform = function(pos, _, player)
|
||||||
|
|
||||||
local selected_sound_key = meta:get_string("selected_sound_key")
|
local selected_sound_key = meta:get_string("selected_sound_key")
|
||||||
local selected_id = 1
|
local selected_id = 1
|
||||||
local i = 1
|
|
||||||
|
|
||||||
-- sound list
|
-- sound list
|
||||||
local sound_list = "textlist[0,1;4,6;sounds;"
|
local sound_list = "textlist[0,1;4,6;sounds;"
|
||||||
|
|
||||||
for key, sound in pairs(soundblock.sounds) do
|
for i, sounddef in ipairs(soundblock.sounds) do
|
||||||
if selected_sound_key == key then
|
if selected_sound_key == sounddef.key then
|
||||||
selected_id = i
|
selected_id = i
|
||||||
end
|
end
|
||||||
|
|
||||||
i = i + 1
|
sound_list = sound_list .. minetest.formspec_escape(sounddef.name) .. ","
|
||||||
sound_list = sound_list .. minetest.formspec_escape(sound.name) .. ","
|
|
||||||
end
|
end
|
||||||
sound_list = sound_list:sub(1, #sound_list-1)
|
sound_list = sound_list:sub(1, #sound_list-1)
|
||||||
sound_list = sound_list .. ";" .. selected_id .. "]";
|
sound_list = sound_list .. ";" .. selected_id .. "]";
|
||||||
|
@ -89,8 +87,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
local timer = minetest.get_node_timer(pos)
|
local timer = minetest.get_node_timer(pos)
|
||||||
|
local state = meta:get_string("state")
|
||||||
|
|
||||||
if fields.toggle_state then
|
if fields.toggle_state then
|
||||||
local state = meta:get_string("state")
|
|
||||||
if state == "on" then
|
if state == "on" then
|
||||||
state = "mesecons"
|
state = "mesecons"
|
||||||
timer:stop()
|
timer:stop()
|
||||||
|
@ -101,7 +100,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
|
||||||
else -- off
|
else -- off
|
||||||
state = "on"
|
state = "on"
|
||||||
timer:start(0)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -117,22 +115,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
if fields.sounds then
|
if fields.sounds then
|
||||||
|
minetest.chat_send_all("XXX:" .. fields.sounds)
|
||||||
parts = fields.sounds:split(":")
|
parts = fields.sounds:split(":")
|
||||||
if parts[1] == "CHG" then
|
if parts[1] == "CHG" then
|
||||||
local selected_id = tonumber(parts[2])
|
local selected_id = tonumber(parts[2])
|
||||||
local i = 1
|
local sounddef = soundblock.sounds[selected_id]
|
||||||
local selected_sound_key
|
|
||||||
|
|
||||||
for key in pairs(soundblock.sounds) do
|
meta:set_string("selected_sound_key", sounddef.key)
|
||||||
if selected_id == i then
|
|
||||||
selected_sound_key = key
|
|
||||||
end
|
|
||||||
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
meta:set_string("selected_sound_key", selected_sound_key)
|
|
||||||
local sounddef = soundblock.sounds[selected_sound_key]
|
|
||||||
if sounddef and sounddef.length then
|
if sounddef and sounddef.length then
|
||||||
meta:set_int("interval_min", sounddef.length)
|
meta:set_int("interval_min", sounddef.length)
|
||||||
meta:set_int("interval_max", sounddef.length)
|
meta:set_int("interval_max", sounddef.length)
|
||||||
|
@ -141,4 +130,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if state == "on" then
|
||||||
|
timer:start(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -1,9 +1,27 @@
|
||||||
|
|
||||||
|
local played_sound_cache = {}
|
||||||
|
|
||||||
|
local stop_sound = function(pos)
|
||||||
|
local hash = minetest.hash_node_position(pos)
|
||||||
|
if played_sound_cache[hash] then
|
||||||
|
minetest.sound_stop(played_sound_cache[hash])
|
||||||
|
played_sound_cache[hash] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local execute = function(pos)
|
local execute = function(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
|
|
||||||
|
stop_sound(pos)
|
||||||
|
|
||||||
local selected_sound_key = meta:get_string("selected_sound_key")
|
local selected_sound_key = meta:get_string("selected_sound_key")
|
||||||
local def = soundblock.sounds[selected_sound_key]
|
local def
|
||||||
|
|
||||||
|
for _, sounddef in ipairs(soundblock.sounds) do
|
||||||
|
if sounddef.key == selected_sound_key then
|
||||||
|
def = sounddef
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if def == nil then
|
if def == nil then
|
||||||
return
|
return
|
||||||
|
@ -28,12 +46,15 @@ local execute = function(pos)
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.sound_play(filename, {
|
local handle = minetest.sound_play(filename, {
|
||||||
pos = play_pos,
|
pos = play_pos,
|
||||||
gain = gain,
|
gain = gain,
|
||||||
max_hear_distance = hear_distance
|
max_hear_distance = hear_distance
|
||||||
})
|
})
|
||||||
|
|
||||||
|
local hash = minetest.hash_node_position(pos)
|
||||||
|
played_sound_cache[hash] = handle
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_node("soundblock:block", {
|
minetest.register_node("soundblock:block", {
|
||||||
|
@ -67,6 +88,7 @@ minetest.register_node("soundblock:block", {
|
||||||
local state = meta:get_string("state")
|
local state = meta:get_string("state")
|
||||||
|
|
||||||
if state ~= "on" then
|
if state ~= "on" then
|
||||||
|
stop_sound(pos)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -85,7 +107,10 @@ minetest.register_node("soundblock:block", {
|
||||||
effector = {
|
effector = {
|
||||||
action_on = function (pos)
|
action_on = function (pos)
|
||||||
execute(pos)
|
execute(pos)
|
||||||
end
|
end,
|
||||||
|
action_off = function(pos)
|
||||||
|
stop_sound(pos)
|
||||||
|
end
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue