Update stop/start sound

master
Le Sanglier des Ardennes 2016-12-04 15:43:54 +01:00
parent 3b283b4856
commit 0766bfee36
1 changed files with 138 additions and 16 deletions

154
init.lua
View File

@ -1,3 +1,6 @@
vinylplayer_player = {}
-- /giveme vinylplayer:player 99
minetest.register_node("vinylplayer:player", {
description = "Vinyl Player",
@ -9,23 +12,142 @@ minetest.register_node("vinylplayer:player", {
"vinylplayer_player_back.png",
"vinylplayer_player_front.png"
},
is_ground_content = true,
groups = {cracky = 3},
groups = {
cracky = 3
},
drop = "vinylplayer:player_fragments",
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local pos = pointed_thing
if not pos then
return itemstack
end
local playerName = player:get_player_name()
minetest.chat_send_player(playerName, "on_rightclick: play sound")
handle = minetest.sound_play("vinylplayer_keepyourweeds", {
gain = 10.0,
max_hear_distance = 32,
})
return itemstack
end,
})
function vinylplayer_get_handle(pos)
print("vinylplayer_get_handle: begin")
local i = 0
while vinylplayer_player[i] ~= nil do
local mypos = minetest.pos_to_string(vinylplayer_player[i].pos)
print( "[ " .. tostring(i) .." ] at " .. mypos)
print(" " .. tostring(vinylplayer_player[i].pos.x) .. " " .. tostring(pos.x))
print(" " .. tostring(vinylplayer_player[i].pos.y) .. " " .. tostring(pos.y))
print(" " .. tostring(vinylplayer_player[i].pos.z) .. " " .. tostring(pos.z))
if vinylplayer_player[i].pos.x == pos.x and
vinylplayer_player[i].pos.y == pos.y and
vinylplayer_player[i].pos.z == pos.z then
print(" return an exact and existing handle")
return vinylplayer_player[i].handle
end
i = i + 1
end
print("vinylplayer_get_handle: end")
return 0
end
function vinylplayer_set_handle(pos, handle)
print("vinylplayer_set_handle: begin")
local i = 0
while vinylplayer_player[i] ~= nil do
local mypos = minetest.pos_to_string(vinylplayer_player[i].pos)
print( "[ " .. tostring(i) .." ] at " .. mypos)
print(" " .. tostring(vinylplayer_player[i].pos.x) .. " " .. tostring(pos.x))
print(" " .. tostring(vinylplayer_player[i].pos.y) .. " " .. tostring(pos.y))
print(" " .. tostring(vinylplayer_player[i].pos.z) .. " " .. tostring(pos.z))
if vinylplayer_player[i].pos.x == pos.x and
vinylplayer_player[i].pos.y == pos.y and
vinylplayer_player[i].pos.z == pos.z then
print(" pos is equal => set handle")
vinylplayer_player[i].handle = handle
return
end
i = i + 1
end
vinylplayer_player[i] = {}
vinylplayer_player[i].pos = pos
print("handle: " .. dump(handle))
vinylplayer_player[i].handle = handle
print(" set handle")
print("vinylplayer_set_handle: end")
end
-- Stop sound when node is dig
minetest.register_on_dignode(function(pos, oldnode, digger)
if oldnode.name == "vinylplayer:player" then
local mypos = minetest.pos_to_string(pos)
print( "Destruct Node/block at ".. mypos)
print("vinylplayer_get_handle: begin")
local i = 0
while vinylplayer_player[i] ~= nil do
local mypos = minetest.pos_to_string(vinylplayer_player[i].pos)
print( "[ " .. tostring(i) .." ] at " .. mypos)
print(" " .. tostring(vinylplayer_player[i].pos.x) .. " " .. tostring(pos.x))
print(" " .. tostring(vinylplayer_player[i].pos.y) .. " " .. tostring(pos.y))
print(" " .. tostring(vinylplayer_player[i].pos.z) .. " " .. tostring(pos.z))
if vinylplayer_player[i].pos.x == pos.x and
vinylplayer_player[i].pos.y == pos.y and
vinylplayer_player[i].pos.z == pos.z then
print(" return an exact and existing handle")
minetest.sound_stop(vinylplayer_player[i].handle)
return vinylplayer_player[i].handle
end
i = i + 1
end
print("vinylplayer_get_handle: end")
end
return 0
end)
minetest.register_on_punchnode(function(pos, node, player, pointed_thing)
if node.name == "vinylplayer:player" then
local player_name = player:get_player_name()
local mypos = minetest.pos_to_string(pos)
if vinylplayer_get_handle(pos) == 0 then
minetest.sound_stop(0) -- Fix: Force to stop the 1st sound node
local handle = 0
print( "Play sound Node/block at ".. mypos)
handle = minetest.sound_play("vinylplayer_keepyourweeds", {
pos = pos,
gain = 1.0,
max_hear_distance = 32,
})
vinylplayer_set_handle(pos, handle)
else
print( "Stop sound Node/block at ".. mypos)
minetest.sound_stop(vinylplayer_get_handle(pos))
vinylplayer_set_handle(pos, 0)
end
end
end)