Construct sprite sheets as ThangTypes load.

This commit is contained in:
Nick Winter 2014-02-27 22:14:52 -08:00
parent 97197bd89d
commit e20e9d68cf
2 changed files with 47 additions and 43 deletions

View file

@ -99,8 +99,7 @@ module.exports = class LevelLoader extends CocoClass
onSupermodelError: ->
onSupermodelLoadedOne: (e) ->
#if e.model instanceof ThangType
# console.log "LevelLoader loaded ThangType", e.model.get('name'), "so we should figure out how to build it."
@buildSpriteSheetsForThangType e.model if not @headless and e.model instanceof ThangType
@update()
# Things to do when either the Session or Supermodel load
@ -131,6 +130,51 @@ module.exports = class LevelLoader extends CocoClass
tempSession.save(patch, {patch: true})
@sessionDenormalized = true
# Building sprite sheets
grabThangTypeTeams: ->
@grabTeamConfigs()
@thangTypeTeams = {}
for thang in @level.get('thangs')
for component in thang.components
if team = component.config?.team
@thangTypeTeams[thang.thangType] ?= []
@thangTypeTeams[thang.thangType].push team unless team in @thangTypeTeams[thang.thangType]
break
@thangTypeTeams
grabTeamConfigs: ->
for system in @level.get('systems')
if @teamConfigs = system.config?.teamConfigs
break
unless @teamConfigs
# Hack: pulled from Alliance System code. TODO: put in just one place.
@teamConfigs = {"humans":{"superteam":"humans","color":{"hue":0,"saturation":0.75,"lightness":0.5},"playable":true},"ogres":{"superteam":"ogres","color":{"hue":0.66,"saturation":0.75,"lightness":0.5},"playable":false},"neutral":{"superteam":"neutral","color":{"hue":0.33,"saturation":0.75,"lightness":0.5}}}
@teamConfigs
buildSpriteSheetsForThangType: (thangType) ->
@grabThangTypeTeams() unless @thangTypeTeams
for team in @thangTypeTeams[thangType.get('original')] ? [null]
spriteOptions = {resolutionFactor: 4, async: true}
if thangType.get('kind') is 'Floor'
spriteOptions.resolutionFactor = 2
if team and color = @teamConfigs[team]?.color
spriteOptions.colorConfig = team: color
@buildSpriteSheet thangType, spriteOptions
buildSpriteSheet: (thangType, options) ->
if thangType.get('name') is 'Wizard'
options.colorConfig = me.get('wizard')?.colorConfig or {}
building = thangType.buildSpriteSheet options
return unless building
#console.log 'Building:', thangType.get('name'), options
t0 = new Date()
@spriteSheetsToBuild += 1
thangType.once 'build-complete', =>
@spriteSheetsBuilt += 1
@notifyProgress()
console.log "Built", thangType.get('name'), 'after', ((new Date()) - t0), 'ms'
# World init
initWorld: ->
@ -139,46 +183,6 @@ module.exports = class LevelLoader extends CocoClass
@world = new World @level.get('name')
serializedLevel = @level.serialize(@supermodel)
@world.loadFromLevel serializedLevel, false
@buildSpriteSheets()
buildSpriteSheets: ->
return if @headless
thangTypes = {}
thangTypes[tt.get('name')] = tt for tt in @supermodel.getModels(ThangType)
colorConfigs = @world.getTeamColors()
thangsProduced = {}
for thang in @world.thangs
continue unless thang.spriteName
thangType = thangTypes[thang.spriteName]
options = thang.getSpriteOptions(colorConfigs)
options.async = true
if thangType.get('kind') is 'Floor'
options.resolutionFactor = 2
thangsProduced[thang.spriteName] = true
@buildSpriteSheet(thangType, options)
for thangName, thangType of thangTypes
continue if thangsProduced[thangName]
thangType.spriteOptions = {resolutionFactor: 4, async: true}
if thangType.get('kind') is 'Floor'
thangType.spriteOptions.resolutionFactor = 2
@buildSpriteSheet(thangType, thangType.spriteOptions)
buildSpriteSheet: (thangType, options) ->
if thangType.get('name') is 'Wizard'
options.colorConfig = me.get('wizard')?.colorConfig or {}
building = thangType.buildSpriteSheet options
return unless building
console.log 'Building:', thangType.get('name'), options
t0 = new Date()
@spriteSheetsToBuild += 1
thangType.once 'build-complete', =>
@spriteSheetsBuilt += 1
@notifyProgress()
console.log "Built", thangType.get('name'), 'after', ((new Date()) - t0), 'ms'
# Initial Sound Loading

View file

@ -139,7 +139,7 @@ module.exports = class ThangType extends CocoModel
@builder.buildAsync()
@builder.on 'complete', @onBuildSpriteSheetComplete, @, true, key
return true
console.warn 'Building', @get('name'), 'and blocking the main thread.'
console.warn 'Building', @get('name'), @options, 'and blocking the main thread.'
spriteSheet = @builder.build()
@spriteSheets[key] = spriteSheet
delete @building[key]