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.

This commit is contained in:
Scott Erickson 2014-05-02 10:31:20 -07:00
parent 2952d7702c
commit 06bc96d7d0
6 changed files with 38 additions and 19 deletions

View file

@ -8,4 +8,9 @@ module.exports = class CocoCollection extends Backbone.Collection
model.loaded = true for model in @models model.loaded = true for model in @models
getURL: -> getURL: ->
return if _.isString @url then @url else @url() return if _.isString @url then @url else @url()
fetch: ->
@jqxhr = super(arguments...)
@loading = true
@jqxhr

View file

@ -6,6 +6,9 @@ module.exports = class ThangNamesCollection extends CocoCollection
model: ThangType model: ThangType
isCachable: false isCachable: false
constructor: (ids) -> constructor: (@ids) -> super()
console.log 'data', {type:'POST', data:{ids:ids}}
super([], {type:'POST', data:{ids:ids}}) fetch: (options) ->
options ?= {}
_.extend options, {type:'POST', data:{ids:@ids}}
super(options)

View file

@ -105,19 +105,18 @@ module.exports = class LevelLoader extends CocoClass
objUniq = (array) -> _.uniq array, false, (arg) -> JSON.stringify(arg) objUniq = (array) -> _.uniq array, false, (arg) -> JSON.stringify(arg)
thangNames = new ThangNamesCollection(thangIDs) worldNecessities = []
@supermodel.loadCollection thangNames, 'thang_names'
# for thangID in _.uniq thangIDs @thangIDs = _.uniq thangIDs
# url = "/db/thang.type/#{thangID}/version" thangNames = new ThangNamesCollection(@thangIDs)
# url += "?project=true" if @headless and not @editorMode worldNecessities.push @supermodel.loadCollection(thangNames, 'thang_names')
# res = @maybeLoadURL url, ThangType, 'thang'
# @listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res
for obj in objUniq componentVersions for obj in objUniq componentVersions
url = "/db/level.component/#{obj.original}/version/#{obj.majorVersion}" url = "/db/level.component/#{obj.original}/version/#{obj.majorVersion}"
@maybeLoadURL url, LevelComponent, 'component' worldNecessities.push @maybeLoadURL(url, LevelComponent, 'component')
for obj in objUniq systemVersions for obj in objUniq systemVersions
url = "/db/level.system/#{obj.original}/version/#{obj.majorVersion}" url = "/db/level.system/#{obj.original}/version/#{obj.majorVersion}"
@maybeLoadURL url, LevelSystem, 'system' worldNecessities.push @maybeLoadURL(url, LevelSystem, 'system')
for obj in objUniq articleVersions for obj in objUniq articleVersions
url = "/db/article/#{obj.original}/version/#{obj.majorVersion}" url = "/db/article/#{obj.original}/version/#{obj.majorVersion}"
@maybeLoadURL url, Article, 'article' @maybeLoadURL url, Article, 'article'
@ -128,6 +127,17 @@ module.exports = class LevelLoader extends CocoClass
unless @headless and not @editorMode unless @headless and not @editorMode
wizard = ThangType.loadUniversalWizard() wizard = ThangType.loadUniversalWizard()
@supermodel.loadModel wizard, 'thang' @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) -> maybeLoadURL: (url, Model, resourceName) ->
return if @supermodel.getModel(url) return if @supermodel.getModel(url)
@ -138,7 +148,6 @@ module.exports = class LevelLoader extends CocoClass
@loadLevelSounds() @loadLevelSounds()
@denormalizeSession() @denormalizeSession()
app.tracker.updatePlayState(@level, @session) unless @headless app.tracker.updatePlayState(@level, @session) unless @headless
@initWorld()
denormalizeSession: -> denormalizeSession: ->
return if @headless or @sessionDenormalized or @spectateMode return if @headless or @sessionDenormalized or @spectateMode
@ -201,7 +210,6 @@ module.exports = class LevelLoader extends CocoClass
@world = new World @level.get('name') @world = new World @level.get('name')
serializedLevel = @level.serialize(@supermodel) serializedLevel = @level.serialize(@supermodel)
@world.loadFromLevel serializedLevel, false @world.loadFromLevel serializedLevel, false
console.log 'INITIALIZED!', (new Date().getTime() - @t0) / 1000
# Initial Sound Loading # Initial Sound Loading

View file

@ -48,7 +48,7 @@ module.exports = class SpriteBoss extends CocoClass
toString: -> "<SpriteBoss: #{@spriteArray.length} sprites>" toString: -> "<SpriteBoss: #{@spriteArray.length} sprites>"
thangTypeFor: (type) -> 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: -> createLayers: ->
@spriteLayers = {} @spriteLayers = {}
@ -145,7 +145,7 @@ module.exports = class SpriteBoss extends CocoClass
addThangToSprites: (thang, layer=null) -> addThangToSprites: (thang, layer=null) ->
return console.warn 'Tried to add Thang to the surface it already has:', thang.id if @sprites[thang.id] 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 = @createSpriteOptions thang: thang
options.resolutionFactor = if thangType.get('kind') is 'Floor' then 2 else 4 options.resolutionFactor = if thangType.get('kind') is 'Floor' then 2 else 4
sprite = new CocoSprite thangType, options sprite = new CocoSprite thangType, options

View file

@ -36,6 +36,8 @@ class CocoModel extends Backbone.Model
@loading = false @loading = false
@markToRevert() @markToRevert()
@loadFromBackup() @loadFromBackup()
getNormalizedURL: -> "#{@urlRoot}/#{@id}"
set: -> set: ->
res = super(arguments...) res = super(arguments...)
@ -75,9 +77,9 @@ class CocoModel extends Backbone.Model
return super attrs, options return super attrs, options
fetch: -> fetch: ->
res = super(arguments...) @jqxhr = super(arguments...)
@loading = true @loading = true
res @jqxhr
markToRevert: -> markToRevert: ->
if @type() is 'ThangType' if @type() is 'ThangType'

View file

@ -199,6 +199,7 @@ class ModelResource extends Resource
super(name, value) super(name, value)
@model = modelOrCollection @model = modelOrCollection
@fetchOptions = fetchOptions @fetchOptions = fetchOptions
@jqxhr = @model.jqxhr
load: -> load: ->
@markLoading() @markLoading()