Fix loading of Level.

This commit is contained in:
Ting-Kuan 2014-04-17 19:23:35 -04:00
parent b7439ed1ca
commit 1c1cf2f189
5 changed files with 32 additions and 23 deletions

View file

@ -59,15 +59,17 @@ module.exports = class LevelLoader extends CocoClass
# Unless you specify cache:false, sometimes the browser will use a cached session
# and players will 'lose' code
@listenToOnce(@session, 'sync', @onSessionLoaded)
sessionRes = @supermodel.addModelResource(@session, 'level_session', {cache:false})
@listenToOnce(sessionRes, 'resource:loaded', @onSessionLoaded)
sessionRes.load()
if @opponentSessionID
@opponentSession = new LevelSession()
@opponentSession.url = "/db/level_session/#{@opponentSessionID}"
@listenToOnce(@opponentSession, 'sync', @onSessionLoaded)
opponentSessionRes = @supermodel.addModelResource(@opponentSession, 'opponent_session')
@listenToOnce(opponentSessionRes, 'resource:loaded', @onSessionLoaded)
opponentSessionRes.load()
sessionsLoaded: ->
@ -84,7 +86,7 @@ module.exports = class LevelLoader extends CocoClass
# Supermodel (Level) Loading
loadLevelModels: ->
@listenTo(@supermodel, 'loaded-one', @onSupermodelLoadedOne)
@listenTo(@supermodel, 'superModel:updateProgress', @onSupermodelLoadedOne) # Some models are not added via addModelResource()
@listenToOnce(@supermodel, 'error', @onSupermodelError)
@level = @supermodel.getModel(Level, @levelID) or new Level _id: @levelID
levelID = @levelID
@ -188,6 +190,7 @@ module.exports = class LevelLoader extends CocoClass
# World init
initWorld: ->
console.debug('gintau', 'init-world')
return if @initialized
@initialized = true
@world = new World @level.get('name')
@ -222,21 +225,25 @@ module.exports = class LevelLoader extends CocoClass
progress: ->
return 0 unless @level.loaded
overallProgress = 0
supermodelProgress = @supermodel.getProgress()
overallProgress += supermodelProgress * 0.7
overallProgress = supermodelProgress * 0.7
overallProgress += 0.1 if @sessionsLoaded()
if @headless
spriteMapProgress = 0.2
else
spriteMapProgress = if supermodelProgress is 1 then 0.2 else 0
spriteMapProgress *= @spriteSheetsBuilt / @spriteSheetsToBuild if @spriteSheetsToBuild
overallProgress += spriteMapProgress
spriteMapProgress = 1
unless @headless
spriteMapProgress = @spriteSheetsBuilt / @spriteSheetsToBuild if @spriteSheetsToBuild
spriteMapProgress *= 0.2
overallProgress += spriteMapProgress
return overallProgress
notifyProgress: ->
Backbone.Mediator.publish 'level-loader:progress-changed', progress: @progress()
progress = @progress()
Backbone.Mediator.publish 'level-loader:progress-changed', progress: progress
@initWorld() if @allDone()
@trigger 'progress'
@trigger 'loaded-all' if @progress() is 1
console.debug 'gintau', 'notify-notifyProgress', progress
if progress is 1
console.debug 'gintau', 'notify-loaded-all'
@trigger 'loaded-all'

View file

@ -164,7 +164,7 @@ module.exports = class SuperModel extends Backbone.Model
@num += r.value
@progress = @num / @denom
console.debug 'gintau', 'updateProgress', @progress, @num, @denom
console.debug 'gintau', 'supermodel-updateProgress', @progress, @num, @denom
@trigger('superModel:updateProgress', @progress)
@trigger('loaded-all') if @finished()

View file

@ -82,7 +82,7 @@ module.exports = class CocoView extends Backbone.View
# View Rendering
render: ->
@showLoading()
console.debug 'gintau' , 'CocoView-Render', @
return @ unless me
super()
return @template if _.isString(@template)
@ -119,7 +119,6 @@ module.exports = class CocoView extends Backbone.View
@$el?.find('.loading-screen .progress-bar').css('width', prog)
onLoaded: ->
@render?()
# Error handling for loading
onResourceLoadFailed: (source) ->
@ -181,14 +180,14 @@ module.exports = class CocoView extends Backbone.View
# Loading RootViews
showLoading: ($el=@$el) ->
console.debug 'gintau', 'showLoading', $el
#console.debug 'gintau', 'showLoading', $el
$el.find('>').addClass('hidden')
$el.append loadingScreenTemplate()
@_lastLoading = $el
hideLoading: ->
return unless @_lastLoading?
console.debug 'gintau', 'hideLoading', @$el
#console.debug 'gintau', 'hideLoading', @$el
@_lastLoading.find('.loading-screen').remove()
@_lastLoading.find('>').removeClass('hidden')
@_lastLoading = null

View file

@ -9,21 +9,22 @@ module.exports = class LevelLoadingView extends View
subscriptions:
'level-loader:progress-changed': 'onLevelLoaderProgressChanged'
onLoaded: ->
afterRender: ->
@$el.find('.tip.rare').remove() if _.random(1, 10) < 9
tips = @$el.find('.tip').addClass('to-remove')
tip = _.sample(tips)
$(tip).removeClass('to-remove')
@$el.find('.to-remove').remove()
@hideLoading()
onLevelLoaderProgressChanged: (e) ->
@$el.find('.progress-bar').css('width', (100 * e.progress) + '%')
@progress = e.progress
@progress = 0.01 if @progress < 0.01
@updateProgressBar()
updateProgressBar: ->
@$el?.find('.progress-bar').css('width', (100 * @progress) + '%')
@$el.find('.progress-bar').css('width', (100 * @progress) + '%')
showReady: ->
ready = $.i18n.t('play_level.loading_ready', defaultValue: 'Ready!')

View file

@ -86,7 +86,6 @@ module.exports = class PlayLevelView extends View
@listenToOnce(@supermodel, 'error', @onLevelLoadError)
@saveScreenshot = _.throttle @saveScreenshot, 30000
@insertSubView @loadingView = new LoadingView {}
if @isEditorPreview
f = =>
@supermodel.shouldSaveBackups = (model) ->
@ -124,11 +123,12 @@ module.exports = class PlayLevelView extends View
c.explainHourOfCode = elapsed < 86400 * 1000
c
onLoaded: ->
afterRender: ->
super()
window.onPlayLevelViewLoaded? @ # still a hack
@insertSubView @loadingView = new LoadingView {}
@$el.find('#level-done-button').hide()
super()
$('body').addClass('is-playing')
onLevelLoaderProgressChanged: ->
@ -152,6 +152,8 @@ module.exports = class PlayLevelView extends View
return true
onLevelLoaderLoaded: ->
console.debug 'level_view', 'onLevelLoaderLoaded', @levelLoader.progress()
return unless @levelLoader.progress() is 1 # double check, since closing the guide may trigger this early
@loadingView.showReady()
if window.currentModal and not window.currentModal.destroyed