summaryrefslogtreecommitdiffhomepage
path: root/not/MusicPlayer.lua
blob: 1e65e3b62df75adb275edd9353a39a7504f9f2bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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