sound.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local Sound = class()
  2. function Sound:init()
  3. self.mute = false
  4. self.reposition = {}
  5. ctx.event:on('sound.play', f.cur(self.play, self))
  6. ctx.event:on('sound.loop', f.cur(self.loop, self))
  7. love.audio.setDistanceModel('linear clamped')
  8. end
  9. function Sound:update()
  10. love.audio.setPosition(ctx.view.x + ctx.view.width / 2, ctx.view.y + ctx.view.height / 2, 200)
  11. for i = #self.reposition, 1, -1 do
  12. if self.reposition[i]:isStopped() then
  13. table.remove(self.reposition, i)
  14. else
  15. self.reposition[i]:setPosition(ctx.view.x + ctx.view.width / 2, ctx.view.y + ctx.view.height / 2, 200)
  16. end
  17. end
  18. end
  19. function Sound:play(_data)
  20. local name = _data.sound
  21. if self.mute then return end
  22. local sound = data.media.sounds[name]:play()
  23. if _data.gui then
  24. _data.relative = true
  25. _data.x, _data.y = 0, 0
  26. table.insert(self.reposition, sound)
  27. end
  28. sound:setRelative(_data.relative or false)
  29. sound:setRolloff(_data.rolloff or 1)
  30. sound:setPosition(_data.x or 0, _data.y or 0, _data.z or 0)
  31. sound:setAttenuationDistances(_data.minrange or 350, _data.maxrange or 1000)
  32. return sound
  33. end
  34. function Sound:loop(data)
  35. local sound = self:play(data)
  36. if sound then sound:setLooping(true) end
  37. return sound
  38. end
  39. function Sound:mute()
  40. self.mute = not self.mute
  41. love.audio.tag.all.stop()
  42. end
  43. return Sound