From 06bc96d7d045fa84fe3164a252e7953f7cf5a835 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Fri, 2 May 2014 10:31:20 -0700 Subject: [PATCH] Set up the level loader to get thangtype names first, then load the thang types themselves so the world can be generated in parallel with thang type loading and rendering. --- app/collections/CocoCollection.coffee | 7 ++++- app/collections/ThangNamesCollection.coffee | 9 ++++--- app/lib/LevelLoader.coffee | 30 +++++++++++++-------- app/lib/surface/SpriteBoss.coffee | 4 +-- app/models/CocoModel.coffee | 6 +++-- app/models/SuperModel.coffee | 1 + 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app/collections/CocoCollection.coffee b/app/collections/CocoCollection.coffee index 64074a327..817b0700f 100644 --- a/app/collections/CocoCollection.coffee +++ b/app/collections/CocoCollection.coffee @@ -8,4 +8,9 @@ module.exports = class CocoCollection extends Backbone.Collection model.loaded = true for model in @models getURL: -> - return if _.isString @url then @url else @url() \ No newline at end of file + return if _.isString @url then @url else @url() + + fetch: -> + @jqxhr = super(arguments...) + @loading = true + @jqxhr diff --git a/app/collections/ThangNamesCollection.coffee b/app/collections/ThangNamesCollection.coffee index 1897d8b12..414628d84 100644 --- a/app/collections/ThangNamesCollection.coffee +++ b/app/collections/ThangNamesCollection.coffee @@ -6,6 +6,9 @@ module.exports = class ThangNamesCollection extends CocoCollection model: ThangType isCachable: false - constructor: (ids) -> - console.log 'data', {type:'POST', data:{ids:ids}} - super([], {type:'POST', data:{ids:ids}}) + constructor: (@ids) -> super() + + fetch: (options) -> + options ?= {} + _.extend options, {type:'POST', data:{ids:@ids}} + super(options) diff --git a/app/lib/LevelLoader.coffee b/app/lib/LevelLoader.coffee index 26c743925..074d8c76d 100644 --- a/app/lib/LevelLoader.coffee +++ b/app/lib/LevelLoader.coffee @@ -105,19 +105,18 @@ module.exports = class LevelLoader extends CocoClass objUniq = (array) -> _.uniq array, false, (arg) -> JSON.stringify(arg) - thangNames = new ThangNamesCollection(thangIDs) - @supermodel.loadCollection thangNames, 'thang_names' -# for thangID in _.uniq thangIDs -# url = "/db/thang.type/#{thangID}/version" -# url += "?project=true" if @headless and not @editorMode -# res = @maybeLoadURL url, ThangType, 'thang' -# @listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res + worldNecessities = [] + + @thangIDs = _.uniq thangIDs + thangNames = new ThangNamesCollection(@thangIDs) + worldNecessities.push @supermodel.loadCollection(thangNames, 'thang_names') + for obj in objUniq componentVersions url = "/db/level.component/#{obj.original}/version/#{obj.majorVersion}" - @maybeLoadURL url, LevelComponent, 'component' + worldNecessities.push @maybeLoadURL(url, LevelComponent, 'component') for obj in objUniq systemVersions url = "/db/level.system/#{obj.original}/version/#{obj.majorVersion}" - @maybeLoadURL url, LevelSystem, 'system' + worldNecessities.push @maybeLoadURL(url, LevelSystem, 'system') for obj in objUniq articleVersions url = "/db/article/#{obj.original}/version/#{obj.majorVersion}" @maybeLoadURL url, Article, 'article' @@ -128,6 +127,17 @@ module.exports = class LevelLoader extends CocoClass unless @headless and not @editorMode wizard = ThangType.loadUniversalWizard() @supermodel.loadModel wizard, 'thang' + + jqxhrs = (resource.jqxhr for resource in worldNecessities when resource?.jqxhr) + $.when(jqxhrs...).done(@onWorldNecessitiesLoaded) + + onWorldNecessitiesLoaded: => + @initWorld() + for thangID in @thangIDs + url = "/db/thang.type/#{thangID}/version" + url += "?project=true" if @headless and not @editorMode + res = @maybeLoadURL url, ThangType, 'thang' + @listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res maybeLoadURL: (url, Model, resourceName) -> return if @supermodel.getModel(url) @@ -138,7 +148,6 @@ module.exports = class LevelLoader extends CocoClass @loadLevelSounds() @denormalizeSession() app.tracker.updatePlayState(@level, @session) unless @headless - @initWorld() denormalizeSession: -> return if @headless or @sessionDenormalized or @spectateMode @@ -201,7 +210,6 @@ module.exports = class LevelLoader extends CocoClass @world = new World @level.get('name') serializedLevel = @level.serialize(@supermodel) @world.loadFromLevel serializedLevel, false - console.log 'INITIALIZED!', (new Date().getTime() - @t0) / 1000 # Initial Sound Loading diff --git a/app/lib/surface/SpriteBoss.coffee b/app/lib/surface/SpriteBoss.coffee index 03e05e0c3..1eb7aa8b7 100644 --- a/app/lib/surface/SpriteBoss.coffee +++ b/app/lib/surface/SpriteBoss.coffee @@ -48,7 +48,7 @@ module.exports = class SpriteBoss extends CocoClass toString: -> "" thangTypeFor: (type) -> - _.find @options.thangTypes, (m) -> m.get('original') is type or m.get('name') is type + _.find @options.thangTypes, (m) -> m.get('actions') and m.get('original') is type or m.get('name') is type createLayers: -> @spriteLayers = {} @@ -145,7 +145,7 @@ module.exports = class SpriteBoss extends CocoClass addThangToSprites: (thang, layer=null) -> return console.warn 'Tried to add Thang to the surface it already has:', thang.id if @sprites[thang.id] - thangType = _.find @options.thangTypes, (m) -> m.get('name') is thang.spriteName + thangType = _.find @options.thangTypes, (m) -> m.get('actions') and m.get('name') is thang.spriteName options = @createSpriteOptions thang: thang options.resolutionFactor = if thangType.get('kind') is 'Floor' then 2 else 4 sprite = new CocoSprite thangType, options diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index a7d408adf..ab2737ad4 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -36,6 +36,8 @@ class CocoModel extends Backbone.Model @loading = false @markToRevert() @loadFromBackup() + + getNormalizedURL: -> "#{@urlRoot}/#{@id}" set: -> res = super(arguments...) @@ -75,9 +77,9 @@ class CocoModel extends Backbone.Model return super attrs, options fetch: -> - res = super(arguments...) + @jqxhr = super(arguments...) @loading = true - res + @jqxhr markToRevert: -> if @type() is 'ThangType' diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index 14f08ef11..f2bc57de4 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -199,6 +199,7 @@ class ModelResource extends Resource super(name, value) @model = modelOrCollection @fetchOptions = fetchOptions + @jqxhr = @model.jqxhr load: -> @markLoading()