codecombat/app/lib/surface/MusicPlayer.coffee

113 lines
3.4 KiB
CoffeeScript
Raw Normal View History

CocoClass = require 'core/CocoClass'
2014-01-03 13:32:13 -05:00
AudioPlayer = require 'lib/AudioPlayer'
{me} = require 'core/auth'
2014-01-03 13:32:13 -05:00
CROSSFADE_LENGTH = 1500
2014-09-11 11:38:30 -04:00
MUSIC_VOLUME = 0.6
2014-01-03 13:32:13 -05:00
module.exports = class MusicPlayer extends CocoClass
currentMusic: null
standingBy: null
2014-02-12 15:41:41 -05:00
2014-01-03 13:32:13 -05:00
subscriptions:
'music-player:play-music': 'onPlayMusic'
2014-01-03 13:32:13 -05:00
'audio-player:loaded': 'onAudioLoaded'
2014-09-25 13:30:43 -04:00
'playback:real-time-playback-started': 'onRealTimePlaybackStarted'
'playback:real-time-playback-ended': 'onRealTimePlaybackEnded'
'music-player:enter-menu': 'onEnterMenu'
'music-player:exit-menu': 'onExitMenu'
'level:set-volume': 'onSetVolume'
2014-02-12 15:41:41 -05:00
2014-01-03 13:32:13 -05:00
constructor: ->
2014-02-12 15:41:41 -05:00
super arguments...
me.on 'change:music', @onMusicSettingChanged, @
2014-01-03 13:32:13 -05:00
onAudioLoaded: ->
@onPlayMusic(@standingBy) if @standingBy
onPlayMusic: (e) ->
return if application.isIPadApp # Hard to measure, but just guessing this will save memory.
unless me.get 'volume'
@lastMusicEventIgnoredWhileMuted = e
return
2014-01-03 13:32:13 -05:00
src = e.file
src = "/file#{src}#{AudioPlayer.ext}"
if (not e.file) or src is @currentMusic?.src
if e.play then @restartCurrentMusic() else @fadeOutCurrentMusic()
return
2014-06-30 22:16:26 -04:00
media = AudioPlayer.getStatus(src)
if not media?.loaded
AudioPlayer.preloadSound(src)
@standingBy = e
return
2014-02-12 15:41:41 -05:00
delay = e.delay ? 0
2014-01-03 13:32:13 -05:00
@standingBy = null
@fadeOutCurrentMusic()
@startNewMusic(src, delay) if e.play
2014-06-30 22:16:26 -04:00
restartCurrentMusic: ->
return unless @currentMusic
@currentMusic.play('none', 0, 0, -1, 0.3)
@updateMusicVolume()
2014-06-30 22:16:26 -04:00
fadeOutCurrentMusic: ->
return unless @currentMusic
createjs.Tween.removeTweens(@currentMusic)
f = -> @stop()
2014-06-30 22:16:26 -04:00
createjs.Tween.get(@currentMusic).to({volume: 0.0}, CROSSFADE_LENGTH).call(f)
startNewMusic: (src, delay) ->
@currentMusic = createjs.Sound.play(src, 'none', 0, 0, -1, 0.3) if src
2014-01-03 13:32:13 -05:00
return unless @currentMusic
@currentMusic.volume = 0.0
if me.get('music', true)
createjs.Tween.get(@currentMusic).wait(delay).to({volume: MUSIC_VOLUME}, CROSSFADE_LENGTH)
2014-01-03 13:32:13 -05:00
onMusicSettingChanged: ->
@updateMusicVolume()
updateMusicVolume: ->
return unless @currentMusic
createjs.Tween.removeTweens(@currentMusic)
@currentMusic.volume = if me.get('music', true) then MUSIC_VOLUME else 0.0
2014-02-12 15:41:41 -05:00
2014-09-25 13:30:43 -04:00
onRealTimePlaybackStarted: (e) ->
@previousMusic = @currentMusic
trackNumber = _.random 0, 2
Backbone.Mediator.publish 'music-player:play-music', file: "/music/music_real_time_#{trackNumber}", play: true
onRealTimePlaybackEnded: (e) ->
@fadeOutCurrentMusic()
if @previousMusic
@currentMusic = @previousMusic
@restartCurrentMusic()
if @currentMusic.volume
createjs.Tween.get(@currentMusic).wait(5000).to({volume: MUSIC_VOLUME}, CROSSFADE_LENGTH)
onEnterMenu: (e) ->
return if @inMenu
@inMenu = true
@previousMusic = @currentMusic
2014-11-30 17:57:34 -05:00
file = "/music/music-menu"
Backbone.Mediator.publish 'music-player:play-music', file: file, play: true, delay: 1000
onExitMenu: (e) ->
return unless @inMenu
@inMenu = false
@fadeOutCurrentMusic()
if @previousMusic
@currentMusic = @previousMusic
@restartCurrentMusic()
2014-09-25 13:30:43 -04:00
onSetVolume: (e) ->
return unless e.volume and @lastMusicEventIgnoredWhileMuted
@onPlayMusic @lastMusicEventIgnoredWhileMuted
@lastMusicEventIgnoredWhileMuted = null
2014-02-12 15:41:41 -05:00
destroy: ->
me.off 'change:music', @onMusicSettingChanged, @
@fadeOutCurrentMusic()
super()