summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZefram <zefram@fysh.org>2014-08-14 15:04:42 +0100
committerZefram <zefram@fysh.org>2014-08-15 12:25:46 +0100
commit1d0687556a52891aeadc0e8d9a58e44c53cb826b (patch)
tree73bc6d002dc90a5a6dab751df636aa28781b6b22
parent049129d9b8010f2199f19c5cf3c2cded3262767d (diff)
Fix music player's sound management
If an operating music player was disconnected from the electrical network or destroyed, it used to leave the music playing forever. There was also a glitch upon starting playing, as the music was started by the form handler but then stopped when the run function realised it wasn't receiving any power, because it hadn't demanded any yet.
-rw-r--r--technic/machines/LV/music_player.lua29
1 files changed, 14 insertions, 15 deletions
diff --git a/technic/machines/LV/music_player.lua b/technic/machines/LV/music_player.lua
index f51d29d..ba5daab 100644
--- a/technic/machines/LV/music_player.lua
+++ b/technic/machines/LV/music_player.lua
@@ -76,6 +76,15 @@ local run = function(pos, node)
meta:set_int("LV_EU_demand", demand)
end
+local function stop_player(pos, node)
+ local pos_hash = minetest.hash_node_position(pos)
+ local music_handle = music_handles[pos_hash]
+ if music_handle then
+ minetest.sound_stop(music_handle)
+ music_handles[pos_hash] = nil
+ end
+end
+
minetest.register_node("technic:music_player", {
description = S("%s Music Player"):format("LV"),
tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
@@ -91,8 +100,6 @@ minetest.register_node("technic:music_player", {
end,
on_receive_fields = function(pos, formanme, fields, sender)
local meta = minetest.get_meta(pos)
- local pos_hash = minetest.hash_node_position(pos)
- local music_handle = music_handles[pos_hash]
local current_track = meta:get_int("current_track")
if fields.track1 then current_track = 1 end
if fields.track2 then current_track = 2 end
@@ -120,22 +127,14 @@ minetest.register_node("technic:music_player", {
"button[6,4;1,2;stop;Stop]"..
"label[4,0;"..S("Current track %s")
:format(current_track).."]")
- if fields.play then
- if music_handle then
- minetest.sound_stop(music_handle)
- end
- music_handle = play_track(pos, current_track)
- meta:set_int("active", 1)
- end
- if fields.stop then
- meta:set_int("active", 0)
- if music_handle then
- minetest.sound_stop(music_handle)
- end
+ if fields.play or fields.stop then
+ stop_player(pos)
+ meta:set_int("active", fields.play and 1 or 0)
end
- music_handles[pos_hash] = music_handle
end,
+ on_destruct = stop_player,
technic_run = run,
+ technic_on_disable = stop_player,
})
technic.register_machine("LV", "technic:music_player", technic.receiver)