diff options
author | Aki <nthirtyone@gmail.com> | 2017-08-13 02:26:55 +0200 |
---|---|---|
committer | Aki <nthirtyone@gmail.com> | 2017-08-13 02:26:55 +0200 |
commit | b1cf14d64a2d3e28683db87190c4b2c7799c259d (patch) | |
tree | e45dac5cc6ce5265e3e3e2c914eb7cc92a820b8b /not/MusicPlayer.lua | |
parent | 0dd01913fe0eefc7ba4bc0797877f40fdedf9315 (diff) | |
parent | ed62b573417bdc85bec616f6016846b02de4c906 (diff) | |
download | roflnauts-b1cf14d64a2d3e28683db87190c4b2c7799c259d.zip roflnauts-b1cf14d64a2d3e28683db87190c4b2c7799c259d.tar.gz roflnauts-b1cf14d64a2d3e28683db87190c4b2c7799c259d.tar.bz2 |
Merge branch 'multi'maps
Diffstat (limited to 'not/MusicPlayer.lua')
-rw-r--r-- | not/MusicPlayer.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua new file mode 100644 index 0000000..4634ed9 --- /dev/null +++ b/not/MusicPlayer.lua @@ -0,0 +1,51 @@ +require "not.Object" + +--- `MusicPlayer` +-- Simple music player object that plays and loops selected track. +MusicPlayer = Object:extends() + +function MusicPlayer:new (trackName) + self.tracks = {} + if trackName then + self:setTrack(trackName) + self:play() + end +end + +function MusicPlayer:delete () + self.tracks = nil + self:stop() +end + +function MusicPlayer:setTrack (trackName) + if self.source then + self.source:stop() + end + if self.tracks[trackName] then + self.source = self.tracks[trackName] + else + local source = love.audio.newSource("assets/music/" .. trackName) + source:setLooping(true) + source:setVolume(.7) + self.source = source + self.tracks[trackName] = source + end +end + +function MusicPlayer:getCurrentTrack () + for key,track in pairs(self.tracks) do + if self.tracks[key] == self.source then + return key + end + end +end + +function MusicPlayer:play () + self.source:play() +end + +function MusicPlayer:stop () + self.source:stop() +end + +return MusicPlayer |