dungeon/mods/mobf/sound.lua

103 lines
2.9 KiB
Lua

-------------------------------------------------------------------------------
-- Mob Framework Mod by Sapier
--
-- You may copy, use, modify or do nearly anything except removing this
-- copyright notice.
-- And of course you are NOT allow to pretend you have written it.
--
--! @file sound.lua
--! @brief component containing sound related functions
--! @copyright Sapier
--! @author Sapier
--! @date 2012-08-09
--
--! @defgroup grp_sound Sound subcomponent
--! @brief Component handling all sound related actions
--! @ingroup framework_int
--! @{
-- Contact sapier a t gmx net
-------------------------------------------------------------------------------
--! @class sound
sound = {}
--!@}
-------------------------------------------------------------------------------
-- name: play(entity)
--
--! @brief play a sound at a specified position
--! @memberof sound
--
--! @param pos position to play sound at
--! @param soundspec sound to play
-------------------------------------------------------------------------------
function sound.play(pos,soundspec)
if (soundspec ~= nil) then
local toplay = {
gain = soundspec.gain,
pos = pos,
max_hear_distance = soundspec.max_hear_distance,
loop = false,
}
minetest.sound_play(soundspec.name,toplay)
else
dbg_mobf.sound_lvl2("MOBF: no soundspec")
--todo add log entry
end
end
-------------------------------------------------------------------------------
-- name: play_random(entity,now)
--
--! @brief play a random sound for mob
--! @memberof sound
--
--! @param entity mob to do action
--! @param now current time
-------------------------------------------------------------------------------
function sound.play_random(entity,now)
if entity.dynamic_data == nil or
entity.dynamic_data.sound == nil then
mobf_bug_warning(LOGLEVEL_ERROR,"MOBF BUG!!!: >" ..entity.data.name .. "< removed=" .. dump(entity.removed) .. " entity=" .. tostring(entity) .. " sound callback without dynamic data")
return
end
if entity.data.sound ~= nil and
entity.data.sound.random ~= nil then
if (entity.dynamic_data.sound.random_last + entity.data.sound.random.min_delta < now) then
if math.random() < entity.data.sound.random.chance then
sound.play(entity.object:getpos(),entity.data.sound.random)
entity.dynamic_data.sound.random_last = now
dbg_mobf.sound_lvl1("MOBF: playing sound")
else
dbg_mobf.sound_lvl1("MOBF: not playing sound")
end
end
end
end
-------------------------------------------------------------------------------
-- name: sound.init_dynamic_data(entity)
--
--! @brief initialize all dynamic data for sound on activate
--! @memberof sound
--
--! @param entity mob to do action
--! @param now current time
-------------------------------------------------------------------------------
function sound.init_dynamic_data(entity,now)
local data = {
random_last = now,
}
entity.dynamic_data.sound = data
end