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