mobf_core/mobf/sound.lua

111 lines
3.3 KiB
Lua
Raw Normal View History

2012-09-19 23:30:14 +02:00
-------------------------------------------------------------------------------
-- Mob Framework Mod by Sapier
--
2012-09-19 23:30:14 +02:00
-- You may copy, use, modify or do nearly anything except removing this
-- copyright notice.
2012-09-19 23:30:14 +02:00
-- 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
-------------------------------------------------------------------------------
mobf_assert_backtrace(sound == nil)
2012-09-19 23:30:14 +02:00
--! @class sound
sound = {}
--!@}
-------------------------------------------------------------------------------
-- @function [parent=#sound] play(entity)
2012-09-19 23:30:14 +02:00
--
--! @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,
}
2012-09-19 23:30:14 +02:00
minetest.sound_play(soundspec.name,toplay)
else
dbg_mobf.sound_lvl2("MOBF: no soundspec")
--todo add log entry
end
end
-------------------------------------------------------------------------------
-- @function [parent=#sound] play_random(entity,now)
2012-09-19 23:30:14 +02:00
--
--! @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
2012-09-19 23:30:14 +02:00
if entity.data.sound ~= nil and
entity.data.sound.random ~= nil then
2012-09-19 23:30:14 +02:00
if (entity.dynamic_data.sound.random_last + entity.data.sound.random.min_delta < now) then
if math.random() < entity.data.sound.random.chance then
2013-06-19 22:17:38 +02:00
local toplay = nil
2013-07-30 19:03:24 +02:00
if type(entity.data.sound.random) == "table" and
#entity.data.sound.random > 0 then
2013-06-19 22:17:38 +02:00
local current_random_sound = math.floor(math.random(1,#entity.data.sound.random) + 0.5)
toplay = entity.data.sound.random[current_random_sound]
else
toplay = entity.data.sound.random
end
sound.play(entity.object:getpos(),toplay)
2012-09-19 23:30:14 +02:00
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
-------------------------------------------------------------------------------
-- @function [parent=#sound] sound.init_dynamic_data(entity)
2012-09-19 23:30:14 +02:00
--
--! @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,
}
2012-09-19 23:30:14 +02:00
entity.dynamic_data.sound = data
end