-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
215 lines (181 loc) · 5.2 KB
/
engine.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import partialRight from 'lodash/partialRight'
import map from 'lodash/map'
import filter from 'lodash/filter'
import find from 'lodash/find'
import bind from 'lodash/bind'
import get from 'lodash/get'
import capitalize from 'lodash/capitalize'
export class AudioEngine {
constructor ($bus, tracks) {
this.$bus = $bus
this.tracks = tracks
}
init () {
return import('howler')
.then(bind(this.initHowler, this))
.then(bind(this.setupTracks, this))
.then(bind(this.setupPlayers, this))
.then(() => {
this.initSettings()
this.initEvents()
this.ambient.play()
this.music.play()
this.sfx.play()
return this
})
}
initHowler ({Howl, Howler}) {
this.Howl = Howl
this.Howler = Howler
return this
}
setupTracks () {
const processTracks = partialRight(map, this.processTrack)
this.tracks = processTracks(this.tracks)
return this.tracks
}
processTrack (track) {
// If no extension is defined, we assume that the extension is included in the name:
track.fetch = () => import(`@/assets/${track.name}${track.ext || ''}`)
return track
}
setupPlayers () {
return Promise.all([
this.initAmbient(),
this.initMusic(),
this.initSfx()
])
}
initAmbient () {
const initial = this.findTrack('tubestatic')
return this.createPlayer('ambient', initial)
.then(howl => {
this.ambient = howl
return this
})
}
initMusic () {
const initial = this.findTrack('VESSELACCESS')
return this.createPlayer('music', initial)
.then(howl => {
this.music = howl
return this
})
}
initSfx () {
const initial = this.findTrack('osstart')
return this.createPlayer('sfx', initial)
.then(howl => {
this.sfx = howl
return this
})
}
findTrack (trackname) {
return find(this.tracks, {name: trackname})
}
createPlayer (player, track) {
const initTrack = track.src
? bind(Promise.resolve, Promise, track.src)
: () => track.fetch().then(imported => imported.default)
const currentlyMuted = get(this, 'settings.muted', false)
return initTrack()
.then(src => {
return new this.Howl({
src: [src],
loop: player !== 'sfx',
preload: true,
mute: currentlyMuted,
rate: track.rate || 1,
volume: track.volume || this.getVolume(player),
onloaderror (id, err) {
return this.logError(player, `LoadError: ${id} -- ${err}`)
},
onplayerror (id, err) {
return this.logError(player, `PlayError: ${id} -- ${err}`)
}
})
})
}
initSettings () {
this.settings = {
muted: true,
musicVolume: 1,
sfxVolume: 1,
ambientVolume: 1
}
this.emitSettings()
}
handleSettingsChange (settings) {
if (!settings) return
Object.assign(this.settings, settings)
if (typeof settings.muted === 'boolean') {
this.eachPlayer(player => { player.mute = settings.muted })
if (settings.muted) {
this.stopAll()
} else {
this.eachPlayer(player => { player.mute = false })
this.playAll()
}
}
}
initEvents () {
this.$bus.$on('settings:init', () => this.emitSettings())
this.$bus.$on('audio:change', ({settings, cue, options}) => {
console.log('Audio Change:', {cue, options, settings})
if (settings) return this.handleSettingsChange(settings)
return this.handleAudioChange(cue, options)
})
this.$bus.$on('audio:play', (player, trackname) => this.playTrack(player, trackname))
}
handleAudioChange (cue, options) {
if (!cue) return this.logError('SYSTEM', 'Audio change sent with no cue!')
if (options.stop) {
if (cue === 'all') return this.stopAll()
return this.eachPlayer((player, type) => type === cue && player.stop())
}
const track = this.findTrack(cue)
if (!track) return this.logError('SYSTEM', `Track not found for cue: ${cue}.`)
const {type, name} = track
return this.playTrack(type, name)
}
stopAll () {
this.eachPlayer(player => player.stop())
}
playAll () {
this.eachPlayer((player, type) => type !== 'sfx' && player.play())
}
eachPlayer (cb) {
const existingPlayers = filter(['ambient', 'music', 'sfx'], (player) => this[player])
return map(existingPlayers, (player) => cb(this[player], player))
}
emitSettings () {
this.$bus.$emit('audio:init', {settings: this.settings})
}
playTrack (player, trackname) {
const track = this.findTrack(trackname)
if (!track) {
return this.logError(player, `TrackNotFoundError: ${trackname}}`)
}
if (!this[player]) {
return this.logError(player, `PlayerNotFoundError: ${player} uninitialized`)
}
this[player].stop()
return this.createPlayer(player, track)
.then((howl) => {
this[player] = howl
this[player].play()
return this
})
}
getVolume (player) {
return {
music: 0.75,
ambient: 0.5,
sfx: 1
}[player] || 1
}
logError (type, message) {
return console.warn(`[${capitalize(type)} Audio] ${message}`)
}
}
export default AudioEngine