From 2c458f0c756ebe577083a7cc972a07e870cb0ba9 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Jun 2017 17:21:02 +0200 Subject: Music renamed to MusicPlayer changed behaviour to use static table for sources need to change dependencies now --- not/Music.lua | 25 ------------------------- not/MusicPlayer.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 25 deletions(-) delete mode 100644 not/Music.lua create mode 100644 not/MusicPlayer.lua (limited to 'not') diff --git a/not/Music.lua b/not/Music.lua deleted file mode 100644 index ee930f4..0000000 --- a/not/Music.lua +++ /dev/null @@ -1,25 +0,0 @@ ---- `Music` --- Simple music player object that plays and loops selected track in single Scene. -Music = { - source = --[[love.audio.newSource]]nil -} - -Music.__index = Music - -function Music:new (trackName) - local o = setmetatable({}, self) - o:init(trackName) - return o -end - --- TODO: trackName should be passed without file extension. -function Music:init (trackName) - self.source = love.audio.newSource("assets/music/" .. trackName) - self.source:setLooping(true) - self.source:setVolume(.7) - self.source:play() -end - -function Music:delete () - self.source:stop() -end \ No newline at end of file diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua new file mode 100644 index 0000000..1e65e3b --- /dev/null +++ b/not/MusicPlayer.lua @@ -0,0 +1,34 @@ +require "not.object" + +--- `MusicPlayer` +-- Simple music player object that plays and loops selected track. +MusicPlayer = Object:extends() + +MusicPlayer.TRACKS = {} + +-- TODO: trackName should be passed without file extension. +function MusicPlayer:new (trackName) + self:setTrack(trackName) +end + +function MusicPlayer:delete () + self.source:stop() +end + +function MusicPlayer:setTrack (trackName) + if self.source then + self.source:stop() + end + if MusicPlayer.TRACKS[trackName] then + self.source = MusicPlayer.TRACKS[trackName] + else + local source = love.audio.newSource("assets/music" .. trackName) + source:setLooping(true) + source:setVolume(.7) + self.source = source + MusicPlayer.TRACKS[trackName] = source + end + self.source:play() +end + +return MusicPlayer -- cgit v1.1