made make_sound more concise, based on Termos' code
parent
9cd5358da2
commit
a7dbe6c0cb
79
init.lua
79
init.lua
|
@ -408,74 +408,25 @@ function mobkit.animate(self,anim)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--returns a random number inbetween l and r
|
|
||||||
local function random_within_range(l, r)
|
|
||||||
local middle = (l + r) / 2
|
|
||||||
local offset = middle - l
|
|
||||||
|
|
||||||
return middle + (random() - 0.5) * 2 * offset
|
|
||||||
end
|
|
||||||
|
|
||||||
--returns val if val is a number, if it's a table, it treats the fields
|
|
||||||
--x and y as an interval and returns a random number within it.
|
|
||||||
--otherwise returns nil
|
|
||||||
local function get_num_or_random_in_range(val)
|
|
||||||
if type(val) == "number" then
|
|
||||||
return val
|
|
||||||
elseif type(val) == "table" then
|
|
||||||
return random_within_range(val.x, val.y)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function mobkit.make_sound(self, sound)
|
function mobkit.make_sound(self, sound)
|
||||||
if not (self.sounds and self.sounds[sound]) then
|
local spec = self.sounds[sound]
|
||||||
return
|
local param_table = {object=self.object}
|
||||||
|
|
||||||
|
if type(spec) == 'table' then
|
||||||
|
--pick random sound if it's a spec for random sounds
|
||||||
|
if #spec > 0 then spec = spec[random(#spec)] end
|
||||||
|
|
||||||
|
--returns value or a random value within the range [value[1], value[2])
|
||||||
|
local function in_range(value)
|
||||||
|
return type(value) == 'table' and value[1]+random()*(value[2]-value[1]) or value
|
||||||
end
|
end
|
||||||
|
|
||||||
local soundtype = type(self.sounds[sound])
|
--pick random values within a range if they're a table
|
||||||
|
param_table.gain = in_range(spec.gain)
|
||||||
local name
|
param_table.fade = in_range(spec.fade)
|
||||||
local gain
|
param_table.pitch = in_range(spec.pitch)
|
||||||
local fade
|
|
||||||
local pitch
|
|
||||||
local max_hear_distance
|
|
||||||
local loop
|
|
||||||
|
|
||||||
if soundtype == "string" then
|
|
||||||
name = self.sounds[sound]
|
|
||||||
else
|
|
||||||
assert(soundtype == "table",
|
|
||||||
"Invalid sound definition: type was '"
|
|
||||||
.. soundtype ..
|
|
||||||
"', must be 'string' or 'table'")
|
|
||||||
sound = self.sounds[sound]
|
|
||||||
|
|
||||||
|
|
||||||
--if multiple sounds are in the table, choose one randomly
|
|
||||||
if #sound > 0 then
|
|
||||||
sound = sound[random(#sound)]
|
|
||||||
end
|
end
|
||||||
|
return minetest.sound_play(spec, param_table)
|
||||||
name = sound.name
|
|
||||||
|
|
||||||
--if they are not set in the sound table, these are nil and
|
|
||||||
--minetest fills in the default values
|
|
||||||
gain = get_num_or_random_in_range(sound.gain)
|
|
||||||
fade = get_num_or_random_in_range(sound.fade)
|
|
||||||
pitch = get_num_or_random_in_range(sound.pitch)
|
|
||||||
max_hear_distance = get_num_or_random_in_range(sound.max_distance)
|
|
||||||
loop = sound.loop
|
|
||||||
end
|
|
||||||
|
|
||||||
return minetest.sound_play(name,
|
|
||||||
{
|
|
||||||
object = self.object,
|
|
||||||
gain = gain,
|
|
||||||
fade = fade,
|
|
||||||
pitch = pitch,
|
|
||||||
max_hear_distance = max_hear_distance,
|
|
||||||
loop = loop,
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue