One last refactor to ambient sounds.

Make tree sounds managed by the mod that defines the nodes in
question using register_ambient, consistent with other things like
fluids.

nc_envsounds is now responsible for just the air moving and
cave dripping sounds made by air itself.

Common windiness logic moved into api layer.
This commit is contained in:
Aaron Suen 2019-09-02 11:34:14 -04:00
parent 25b2831826
commit 3a4bfdb258
7 changed files with 33 additions and 27 deletions

View File

@ -3,8 +3,8 @@ local ItemStack, ipairs, math, minetest, nodecore, pairs, type, unpack,
vector
= ItemStack, ipairs, math, minetest, nodecore, pairs, type, unpack,
vector
local math_exp, math_random
= math.exp, math.random
local math_exp, math_random, math_sin, math_sqrt
= math.exp, math.random, math.sin, math.sqrt
-- LUALOCALS > ---------------------------------------------------------
local oldplay = minetest.sound_play
@ -15,6 +15,12 @@ function minetest.sound_play(name, spec, ...)
return oldplay(name, spec, ...)
end
function nodecore.windiness(y)
if y < 0 then return 0 end
if y > 512 then y = 512 end
return math_sqrt(y) * (1 + 0.5 * math_sin(minetest.get_gametime() / 5))
end
function nodecore.stack_sounds(pos, kind, stack)
stack = stack or nodecore.stack_get(pos)
stack = ItemStack(stack)

View File

@ -1,32 +1,10 @@
-- LUALOCALS < ---------------------------------------------------------
local math, minetest, nodecore, pairs, vector
= math, minetest, nodecore, pairs, vector
local math_exp, math_random, math_sin, math_sqrt
= math.exp, math.random, math.sin, math.sqrt
local math_exp, math_random
= math.exp, math.random
-- LUALOCALS > ---------------------------------------------------------
local function windiness(y)
if y < 0 then return 0 end
if y > 512 then y = 512 end
return math_sqrt(y) * (1 + 0.5 * math_sin(minetest.get_gametime() / 5))
end
nodecore.register_ambiance({
label = "Tree Leaves Ambiance",
nodenames = {"nc_tree:leaves"},
neigbors = {"air"},
interval = 1,
chance = 100,
sound_name = "nc_envsound_tree",
check = function(pos)
pos.y = pos.y + 1
if pos.y <= 0 then return end
return minetest.get_node(pos).name == "air"
and minetest.get_node_light(pos, 0.5) == 15
and { gain = windiness(pos.y) / 20 }
end
})
local function check(pos, done)
local sp = {
x = pos.x + math_random() * 64 - 32,
@ -46,7 +24,7 @@ local function check(pos, done)
if sp.y <= 0 then return end
minetest.sound_play("nc_envsound_air", {
pos = sp,
gain = windiness(sp.y) / 100
gain = nodecore.windiness(sp.y) / 100
})
elseif light < 4 then
minetest.sound_play("nc_envsound_drip", {

20
mods/nc_tree/ambiance.lua Normal file
View File

@ -0,0 +1,20 @@
-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore
= minetest, nodecore
-- LUALOCALS > ---------------------------------------------------------
nodecore.register_ambiance({
label = "Tree Leaves Ambiance",
nodenames = {"nc_tree:leaves"},
neigbors = {"air"},
interval = 1,
chance = 100,
sound_name = "nc_tree_breeze",
check = function(pos)
pos.y = pos.y + 1
if pos.y <= 0 then return end
return minetest.get_node(pos).name == "air"
and minetest.get_node_light(pos, 0.5) == 15
and { gain = nodecore.windiness(pos.y) / 20 }
end
})

View File

@ -12,3 +12,5 @@ include("stick")
include("schematic")
include("decor")
include("cultivation")
include("ambiance")