-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusicHandler.lua
More file actions
114 lines (98 loc) · 2.56 KB
/
musicHandler.lua
File metadata and controls
114 lines (98 loc) · 2.56 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
local IterableMap = require("include/IterableMap")
local util = require("include/util")
local SoundHandler = require("soundHandler")
local soundFiles = util.LoadDefDirectory("sounds/defs")
local self = {}
local api = {}
local world
local font = love.graphics.newFont(70)
-- First eligible tracks are used as start music
local trackList = {
--'bgm_b',
}
local fallbackTrack = {
--'bgm_b',
}
local currentTrack = {}
local trackRunning = false
local initialDelay = true
local currentTrackRemaining = 1
local trackParity = 1
local function GetTracks()
local foundTrack = {}
for i = 1, #trackList do
local track = soundFiles[trackList[i]]
if track.handler and not foundTrack[track.handler] then
foundTrack[track.handler] = {sound = trackList[i]}
end
end
for i = 1, 3 do
if not foundTrack[i] then
foundTrack[i] = {sound = fallbackTrack[i]}
end
end
util.Permute(trackList)
return {{sound = "bgm_b"}}
end
function api.StopCurrentTrack(delay)
currentTrackRemaining = delay or 0
end
function api.SetCurrentTrackFadeTime(fadeTime)
if trackRunning then
for i = 1, #currentTrack do
SoundHandler.SetSoundFade(currentTrack[i].sound, false, 1/fadeTime)
end
end
end
function api.Update(dt)
if self.needDtReset then
Global.ResetMissingDt()
self.needDtReset = false
end
if initialDelay then
initialDelay = initialDelay - dt
if initialDelay < 0 then
initialDelay = false
self.needDtReset = true
else
return
end
end
currentTrackRemaining = (currentTrackRemaining or 0) - dt
if currentTrackRemaining < 0 then
if world.MusicEnabled() then
if trackRunning then
for i = 1, #currentTrack do
SoundHandler.StopSound(currentTrack[i].sound, trackParity)
end
end
trackParity = 3 - trackParity
currentTrack = GetTracks()
currentTrackRemaining = 0
for i = 1, 1 do
currentTrackRemaining = math.max(currentTrackRemaining, soundFiles[currentTrack[i].sound].duration or Global.DEFAULT_MUSIC_DURATION)
end
currentTrackRemaining = currentTrackRemaining - Global.CROSSFADE_TIME
trackRunning = true
for i = 1, #currentTrack do
SoundHandler.PlaySound(currentTrack[i].sound, trackParity, false, 1 / Global.CROSSFADE_TIME)
end
elseif trackRunning then
for i = 1, #currentTrack do
SoundHandler.StopSound(currentTrack[i].sound, trackParity)
end
trackRunning = false
end
end
end
function api.Initialize(newWorld)
self = {}
world = newWorld
api.StopCurrentTrack()
initialDelay = 0
for i = 1, #trackList do
SoundHandler.LoadSound(trackList[i], 1)
SoundHandler.LoadSound(trackList[i], 2)
end
end
return api