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

@ -9,3 +9,8 @@ module.exports = class CocoCollection extends Backbone.Collection
getURL: ->
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
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)

View file

@ -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'
@ -129,6 +128,17 @@ module.exports = class LevelLoader extends CocoClass
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)
model = new Model().setURL 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

View file

@ -48,7 +48,7 @@ module.exports = class SpriteBoss extends CocoClass
toString: -> "<SpriteBoss: #{@spriteArray.length} sprites>"
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

View file

@ -37,6 +37,8 @@ class CocoModel extends Backbone.Model
@markToRevert()
@loadFromBackup()
getNormalizedURL: -> "#{@urlRoot}/#{@id}"
set: ->
res = super(arguments...)
@saveBackup() if @saveBackups and @loaded and @hasLocalChanges()
@ -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'

View file

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