-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMusic.lua
More file actions
148 lines (131 loc) · 4.59 KB
/
Music.lua
File metadata and controls
148 lines (131 loc) · 4.59 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Music = class()
Music.loadProgress = 0
function ABCMusic:percentageCached()
return cachedIdx / #gPreCacheSoundTable
end
-- THESE FUNCTIONS ARE THE PUBLIC API --
-- called when we are switching to a new screen
-- return true if we should show the loading message because the transition will take a while
function Music.startLoading(type,name)
--print("music",type,name)
Music.isLoading = true
local retVal = false
if type == "StartScreen" then retVal = Music.startScreen()
elseif type == "PackSelect" then retVal = Music.packSelect()
elseif type == "LevelSelect" then retVal = Music.levelSelect(name) -- name is the name of the pack
elseif type == "Level" then retVal = Music.level(name) -- name is the name of the level
elseif type == "WinScreen" then retVal = Music.winScreen(name) -- name is the name of the level
else
assert(false,"invalid music type: "..type)
end
--Music.soundTablePointer = 1
return retVal
end
-- called every draw() cycle while we're loading. When done loading it should trigger a
-- musicLoaded event once
--
-- tick will be called at least once after each startLoading call, and will only stop once
-- a musicLoaded event is triggered - with one exception: when the cut scene is skipped (eg
-- left or right arrow in the level select screens) then tick is called only once.
function Music.tick()
sounds:cache()
if Music.map and Table.size(Music.map) ~= Table.size(Music.tuneMap) then
for k,v in pairs(Music.tuneMap) do
if Music.map[k] == nil then
--if k == "Easy" then Music.map[k] = Music.map["Tutorial"] return nil end
--if k == "Hard" then Music.map[k] = Music.map["Medium"] return nil end
-- if k == "Impossble" then Music.map[k] = Music.map["Crazy"] return nil end
--print("loading "..k)
Music.map[k] = ABCMusic(v,1)
-- print("loading "..k)
Music.loadProgress = Music.loadProgress + .3 / Table.size(Music.tuneMap)
return nil
end
end
end
local doneCaching = true
if not NO_MUSIC then
doneCaching = ABCMusic:preCachePlay()
-- 30% of the time is spent parsing the tunes
Music.loadProgress = .3 + ABCMusic:percentageCached()*.7
end
if doneCaching and Music.isLoading then
Events.trigger("musicLoaded")
currentMusic = Music.map[Music.next]
--print("music change",Music.next,currentMusic==nil)
Music.isLoading = false
end
end
function Music.switch(type)
if Music.map then currentMusic = Music.map[type] end
if currentMusic then currentMusic.soundTablePointer = 1 end
end
-- THESE ARE INTERNAL FUNCTIONS --
function Music.startScreen()
Music.loadProgress = 0
sampleMusic()
if not Music.map then
Music.map = {}
Music.tuneMap = {}
if not NO_MUSIC then
Music.tuneMap = {
Start = startTune,
Win = winTune,
Tutorial = tutorialTune,
--Easy = tutorialTune
}
end
Sounds:init()
Music.next = "Start"
return true
end
return false
end
function Music.packSelect()
Music.next = "Start"
return false
end
function Music.levelSelect(name) -- name is the name of the level pack
Music.next = "Start"
return false
end
function Music.level(name)
Music.next = "Tutorial"
return false
--[[
-- find which pack this level belongs to
local pack = "Impossible"
for _,p in ipairs(packs) do
if Table.contains(p.levels,name) then
pack = p.name
end
end
local val = Music.isPackCached(pack)
if not val then
Music.loadProgress = 0
currentMusic = nil
end
Music.next = pack
return not val
--]]
end
function Music.winScreen(name)
ABCMusic:fade(0,1)
local t = Tweener(1,nil,function() ABCMusic:fade(0.5,0.1) currentMusic = Music.map["Win"] currentMusic.soundTablePointer = 1 end)
Tweener.add(t)
-- Music.next = "Win"
return false
end
function Music.isPackCached(name)
if Music.map[name] ~= nil then return true end
local tune
if name == "Tutorial" then tune = tutorialTune
-- elseif name == "Easy" then tune = tutorialTune
--elseif name == "Medium" then tune = mediumTune
-- elseif name == "Hard" then tune = mediumTune
--elseif name == "Crazy" then tune = crazyTune
--elseif name == "Impossible" then tune = crazyTune
else assert(false,"Invalid pack name: "..name) end
Music.tuneMap[name] = tune
return false
end