Skip to content

Commit

Permalink
Make SoundInstance a separate class, add position
Browse files Browse the repository at this point in the history
Changes to Sound:play()
- New parameter: pos - optional second argument. Does nothing right now.
- Now returns an instance which will be played upon execution, or nothing if all sources are busy.
Both carry over to Game:playSound().
  • Loading branch information
jakubg1 committed Jul 11, 2021
1 parent ff9aa3b commit 210eb51
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/Essentials/Sound.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
local class = require "com/class"
local Sound = class:derive("Sound")

local SoundInstance = require("src/Essentials/SoundInstance")

function Sound:new(path, looping)
print("Loading sound data from " .. path .. "...")
self.INSTANCE_COUNT = 8
-- Each sound has 8 instances of it so it can play up to 8 instances at the same time.
self.instances = {}
for i = 1, self.INSTANCE_COUNT do
self.instances[i] = loadSound(path, "static")
if not self.instances[i] then error("Failed to load sound: " .. path) end
if looping then self.instances[i]:setLooping(looping) end
self.instances[i] = SoundInstance(path, looping)
end
end

function Sound:update(dt)
self:setVolume(game.runtimeManager.options:getEffectiveSoundVolume())
end

function Sound:play(pitch)
pitch = pitch or 1
function Sound:getFreeInstance()
for i, instance in ipairs(self.instances) do
if not instance:isPlaying() then
instance:setPitch(pitch)
instance:play()
return
return instance
end
end
end

function Sound:play(pitch, pos)
pitch = pitch or 1
local instance = self:getFreeInstance()
if instance then
instance:setPitch(pitch)
instance:setPos(pos)
instance:play()
return instance
end
-- might add an algorithm that will nuke one of the sounds and play it again on that instance if no free instances
end

Expand All @@ -41,4 +49,4 @@ function Sound:setVolume(volume)
end
end

return Sound
return Sound
54 changes: 54 additions & 0 deletions src/Essentials/SoundInstance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local class = require "com/class"
local SoundInstance = class:derive("SoundInstance")

local Vec2 = require("src/Essentials/Vector2")

function SoundInstance:new(path, looping)
self.sound = loadSound(path, "static")
if not self.sound then
error("Failed to load sound: " .. path)
end
if looping then
self.sound:setLooping(looping)
end

self.pos = Vec2()
end

function SoundInstance:update(dt)
self:setVolume(game.runtimeManager.options:getEffectiveSoundVolume())
end

function SoundInstance:play(pitch)
pitch = pitch or 1
for i, instance in ipairs(self.instances) do
if not instance:isPlaying() then
instance:setPitch(pitch)
instance:play()
return
end
end
-- might add an algorithm that will nuke one of the sounds and play it again on that instance if no free instances
end

function SoundInstance:stop()
self.sound:stop()
end

function SoundInstance:setVolume(volume)
self.sound:setVolume(volume)
end

function SoundInstance:setPitch(pitch)
self.sound:setPitch(pitch)
end

function SoundInstance:setPos(pos)
self.pos = pos
end

function SoundInstance:isPlaying()
return self.sound:isPlaying()
end

return SoundInstance
4 changes: 2 additions & 2 deletions src/Game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ end



function Game:playSound(name, pitch)
self.resourceManager:getSound(self.configManager.config.general.soundEvents[name]):play(pitch)
function Game:playSound(name, pitch, pos)
return self.resourceManager:getSound(self.configManager.config.general.soundEvents[name]):play(pitch, pos)
end

function Game:stopSound(name)
Expand Down

0 comments on commit 210eb51

Please sign in to comment.