-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SoundInstance a separate class, add position
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
Showing
3 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters