Hello everybody, is as follows, using the otclient and want to put sounds in the citys my server, and I was wondering how I could put 2 kinds of sounds (Music) at the same coordinates,I will leave my script below.
SoundsConfig = {
soundChannel = SoundChannels.Music,
checkInterval = 500,
folder = 'music/',
noSoundMessage = 'No sound file for this area.',
}
SoundsConfig.sounds = {
-- Boss
{ fromPos = { x = 32302, y = 32171, z = 7 }, toPos = { x = 32429, y= 32269, z = 7 }, name = "Thais Cancion", file = "thais.ogg" },
{ fromPos = { x = 32355, y = 32193, z = 7 }, toPos = { x = 32365, y= 32200, z = 7 }, name = "Forja", file = "martelada.ogg" },
-- Bridge
{ fromPos = { x = 32429, y = 32224, z = 7 }, toPos = { x = 32433, y= 32227, z = 7 }, priority = 1, name = "Thais Bridge", file = "dragons.ogg" },
}
-- Sound
local rcSoundChannel
local playingSound
-- Design
soundWindow = nil
soundButton = nil
function toggle()
if soundButton:isOn() then
soundWindow:close()
soundButton:setOn(false)
else
soundWindowpen()
soundButton:setOn(true)
end
end
function onMiniWindowClose()
soundButton:setOn(false)
end
function init()
connect(g_game, {
onGameStart = onGameStart,
onGameEnd = onGameEnd
})
rcSoundChannel = g_sounds.getChannel(SoundsConfig.soundChannel)
soundButton = modules.client_topmenu.addRightGameToggleButton('soundButton', tr('Sound Info') .. '', 'images/audio', toggle)
soundButton:setOn(true)
soundWindow = g_ui.loadUI('rcsound', modules.game_interface.getRightPanel())
soundWindow:disableResize()
soundWindow:setup()
if (g_game.isOnline()) then
onGameStart()
end
end
function terminate()
disconnect(g_game, {
onGameStart = onGameStart,
onGameEnd = onGameEnd
})
onGameEnd()
soundWindow:destroy()
soundButton:destroy()
end
function onGameStart()
g_sounds.enableAudio()
rcSoundChannel:setEnabled(true)
stopSound()
toggleSoundEvent = addEvent(toggleSound, SoundsConfig.checkInterval)
end
function onGameEnd()
stopSound()
removeEvent(toggleSoundEvent)
end
function isInPos(pos, fromPos, toPos)
local fromZ = math.min(fromPos.z, toPos.z)
local toZ = math.max(toPos.z, toPos.z)
return (pos.x >= fromPos.x and pos.x <= toPos.x) and (pos.y >= fromPos.y and pos.y <= toPos.y) and (pos.z >= fromZ and pos.z <= toZ)
end
function toggleSound()
toggleSoundEvent = scheduleEvent(toggleSound, SoundsConfig.checkInterval)
local player = g_game.getLocalPlayer()
if not player then
return
end
local pos = player:getPosition()
local toPlay = nil
for _, sound in pairs(SoundsConfig.sounds) do
if isInPos(pos, sound.fromPos, sound.toPos) then
local priority = sound.priority or 0
if not toPlay or priority > toPlay.priority then
toPlay = sound
toPlay.priority = priority
end
end
end
if not toPlay then
if playingSound then
g_logger.info("RC Sounds: Nothing to play. Player left sound area.")
stopSound()
end
return
end
local playingFile = playingSound and playingSound.file or ''
if playingFile == toPlay.file then
setLabel(getSoundTitle(toPlay))
return
end
g_logger.info("RC Sounds: New sound area detected:")
g_logger.info(" Position: {x=" .. pos.x .. ", y=" .. pos.y .. ", z=" .. pos.z .. "}")
g_logger.info(" Music: " .. toPlay.file)
stopSound()
playSound(toPlay)
playingSound = toPlay
end
function playSound(sound)
local path = SoundsConfig.folder .. sound.file
rcSoundChannel:enqueue(path, 0)
setLabel(getSoundTitle(sound))
end
function getSoundTitle(sound)
if sound.name then
return sound.name
end
local name, _ext = unpack(string.explode(sound.file, ".ogg"))
return name
end
function stopSound()
setLabel(SoundsConfig.noSoundMessage)
rcSoundChannel:stop()
playingSound = nil
end
function setLabel(str)
soundWindow:recursiveGetChildById('currentSound'):getChildById('value'):setText(str)
end