2014-01-03 13:32:13 -05:00
|
|
|
Level = require 'models/Level'
|
|
|
|
CocoClass = require 'lib/CocoClass'
|
|
|
|
AudioPlayer = require 'lib/AudioPlayer'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
|
|
|
ThangType = require 'models/ThangType'
|
|
|
|
app = require 'application'
|
2014-01-14 16:16:30 -05:00
|
|
|
World = require 'lib/world/world'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# This is an initial stab at unifying loading and setup into a single place which can
|
|
|
|
# monitor everything and keep a LoadingScreen visible overall progress.
|
|
|
|
#
|
|
|
|
# Would also like to incorporate into here:
|
|
|
|
# * World Building
|
|
|
|
# * Sprite map generation
|
|
|
|
# * Connecting to Firebase
|
|
|
|
|
|
|
|
module.exports = class LevelLoader extends CocoClass
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
spriteSheetsBuilt: 0
|
|
|
|
spriteSheetsToBuild: 0
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-15 18:44:45 -05:00
|
|
|
constructor: (options) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
super()
|
2014-02-15 18:44:45 -05:00
|
|
|
@supermodel = options.supermodel
|
|
|
|
@levelID = options.levelID
|
|
|
|
@sessionID = options.sessionID
|
|
|
|
@opponentSessionID = options.opponentSessionID
|
|
|
|
@team = options.team
|
|
|
|
@headless = options.headless
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@loadSession()
|
|
|
|
@loadLevelModels()
|
|
|
|
@loadAudio()
|
|
|
|
@playJingle()
|
2014-02-12 15:41:41 -05:00
|
|
|
_.defer @update # Lets everything else resolve first
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
playJingle: ->
|
2014-02-15 18:44:45 -05:00
|
|
|
return if @headless
|
2014-01-03 13:32:13 -05:00
|
|
|
jingles = ["ident_1", "ident_2"]
|
|
|
|
AudioPlayer.playInterfaceSound jingles[Math.floor Math.random() * jingles.length]
|
|
|
|
|
|
|
|
# Session Loading
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
loadSession: ->
|
2014-02-13 18:51:41 -05:00
|
|
|
if @sessionID
|
|
|
|
url = "/db/level_session/#{@sessionID}"
|
|
|
|
else
|
|
|
|
url = "/db/level/#{@levelID}/session"
|
|
|
|
url += "?team=#{@team}" if @team
|
2014-02-15 18:44:45 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@session = new LevelSession()
|
|
|
|
@session.url = -> url
|
2014-02-14 18:35:54 -05:00
|
|
|
|
|
|
|
# Unless you specify cache:false, sometimes the browser will use a cached session
|
|
|
|
# and players will 'lose' code
|
|
|
|
@session.fetch({cache:false})
|
2014-02-11 18:47:59 -05:00
|
|
|
@session.once 'sync', @onSessionLoaded, @
|
2014-02-15 18:44:45 -05:00
|
|
|
|
2014-02-13 19:42:35 -05:00
|
|
|
if @opponentSessionID
|
|
|
|
@opponentSession = new LevelSession()
|
|
|
|
@opponentSession.url = "/db/level_session/#{@opponentSessionID}"
|
|
|
|
@opponentSession.fetch()
|
|
|
|
@opponentSession.once 'sync', @onSessionLoaded, @
|
2014-02-15 18:44:45 -05:00
|
|
|
|
2014-02-13 19:42:35 -05:00
|
|
|
sessionsLoaded: ->
|
|
|
|
@session.loaded and ((not @opponentSession) or @opponentSession.loaded)
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-11 18:47:59 -05:00
|
|
|
onSessionLoaded: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
# TODO: maybe have all non versioned models do this? Or make it work to PUT/PATCH to relative urls
|
2014-02-13 19:42:35 -05:00
|
|
|
if @session.loaded
|
|
|
|
@session.url = -> '/db/level.session/' + @id
|
|
|
|
@update() if @sessionsLoaded()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# Supermodel (Level) Loading
|
|
|
|
|
|
|
|
loadLevelModels: ->
|
2014-02-11 18:47:59 -05:00
|
|
|
@supermodel.on 'loaded-one', @onSupermodelLoadedOne, @
|
|
|
|
@supermodel.once 'error', @onSupermodelError, @
|
2014-01-03 13:32:13 -05:00
|
|
|
@level = @supermodel.getModel(Level, @levelID) or new Level _id: @levelID
|
2014-02-11 18:38:36 -05:00
|
|
|
levelID = @levelID
|
2014-02-15 18:44:45 -05:00
|
|
|
headless = @headless
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-11 18:38:36 -05:00
|
|
|
@supermodel.shouldPopulate = (model) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
# if left unchecked, the supermodel would load this level
|
|
|
|
# and every level next on the chain. This limits the population
|
|
|
|
handles = [model.id, model.get 'slug']
|
2014-02-11 18:38:36 -05:00
|
|
|
return model.constructor.className isnt "Level" or levelID in handles
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-15 20:29:54 -05:00
|
|
|
@supermodel.shouldLoadProjection = (model) ->
|
|
|
|
return true if headless and model.constructor.className is 'ThangType'
|
|
|
|
false
|
2014-02-15 18:44:45 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@supermodel.populateModel @level
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-11 18:47:59 -05:00
|
|
|
onSupermodelError: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
msg = $.i18n.t('play_level.level_load_error',
|
|
|
|
defaultValue: "Level could not be loaded.")
|
2014-02-17 14:53:52 -05:00
|
|
|
$('body').append('<div class="alert">' + msg + '</div>')
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-02-11 18:47:59 -05:00
|
|
|
onSupermodelLoadedOne: (e) ->
|
2014-02-16 21:29:24 -05:00
|
|
|
@update()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-07 02:45:33 -05:00
|
|
|
# Things to do when either the Session or Supermodel load
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-12 15:41:41 -05:00
|
|
|
update: =>
|
2014-01-03 13:32:13 -05:00
|
|
|
@notifyProgress()
|
|
|
|
|
|
|
|
return if @updateCompleted
|
2014-02-17 11:15:53 -05:00
|
|
|
return unless @supermodel?.finished() and @sessionsLoaded()
|
2014-01-03 13:32:13 -05:00
|
|
|
@denormalizeSession()
|
|
|
|
@loadLevelSounds()
|
|
|
|
app.tracker.updatePlayState(@level, @session)
|
|
|
|
@updateCompleted = true
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
denormalizeSession: ->
|
|
|
|
return if @session.get 'levelName'
|
|
|
|
patch =
|
|
|
|
'levelName': @level.get('name')
|
|
|
|
'levelID': @level.get('slug') or @level.id
|
|
|
|
if me.id is @session.get 'creator'
|
|
|
|
patch.creatorName = me.get('name')
|
|
|
|
|
|
|
|
@session.set key, value for key, value of patch
|
|
|
|
tempSession = new LevelSession _id: @session.id
|
|
|
|
tempSession.save(patch, {patch: true})
|
|
|
|
@sessionDenormalized = true
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-14 16:16:30 -05:00
|
|
|
# World init
|
|
|
|
|
|
|
|
initWorld: ->
|
2014-02-06 17:00:27 -05:00
|
|
|
return if @initialized
|
|
|
|
@initialized = true
|
2014-01-14 16:16:30 -05:00
|
|
|
@world = new World @level.get('name')
|
|
|
|
serializedLevel = @level.serialize(@supermodel)
|
|
|
|
@world.loadFromLevel serializedLevel, false
|
|
|
|
@buildSpriteSheets()
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-14 16:16:30 -05:00
|
|
|
buildSpriteSheets: ->
|
2014-02-15 18:44:45 -05:00
|
|
|
return if @headless
|
2014-01-14 16:16:30 -05:00
|
|
|
thangTypes = {}
|
|
|
|
thangTypes[tt.get('name')] = tt for tt in @supermodel.getModels(ThangType)
|
|
|
|
|
|
|
|
colorConfigs = @world.getTeamColors()
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-14 16:16:30 -05:00
|
|
|
thangsProduced = {}
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-14 16:16:30 -05:00
|
|
|
for thang in @world.thangs
|
|
|
|
continue unless thang.spriteName
|
|
|
|
thangType = thangTypes[thang.spriteName]
|
|
|
|
options = thang.getSpriteOptions(colorConfigs)
|
|
|
|
options.async = true
|
2014-02-17 20:38:49 -05:00
|
|
|
if thangType.get('kind') is 'Floor'
|
|
|
|
options.resolutionFactor = 2
|
2014-01-14 16:16:30 -05:00
|
|
|
thangsProduced[thang.spriteName] = true
|
|
|
|
@buildSpriteSheet(thangType, options)
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-14 16:16:30 -05:00
|
|
|
for thangName, thangType of thangTypes
|
|
|
|
continue if thangsProduced[thangName]
|
|
|
|
thangType.spriteOptions = {resolutionFactor: 4, async: true}
|
2014-02-17 20:38:49 -05:00
|
|
|
if thangType.get('kind') is 'Floor'
|
|
|
|
thangType.spriteOptions.resolutionFactor = 2
|
2014-01-14 16:16:30 -05:00
|
|
|
@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
|
|
|
|
@spriteSheetsToBuild += 1
|
2014-02-12 15:41:41 -05:00
|
|
|
thangType.once 'build-complete', =>
|
2014-01-14 16:16:30 -05:00
|
|
|
@spriteSheetsBuilt += 1
|
|
|
|
@notifyProgress()
|
2014-01-16 14:37:04 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
# Initial Sound Loading
|
|
|
|
|
|
|
|
loadAudio: ->
|
2014-02-15 18:44:45 -05:00
|
|
|
return if @headless
|
2014-01-03 13:32:13 -05:00
|
|
|
AudioPlayer.preloadInterfaceSounds ["victory"]
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
loadLevelSounds: ->
|
2014-02-15 18:44:45 -05:00
|
|
|
return if @headless
|
2014-01-03 13:32:13 -05:00
|
|
|
scripts = @level.get 'scripts'
|
|
|
|
return unless scripts
|
|
|
|
|
|
|
|
for script in scripts when script.noteChain
|
|
|
|
for noteGroup in script.noteChain when noteGroup.sprites
|
|
|
|
for sprite in noteGroup.sprites when sprite.say?.sound
|
|
|
|
AudioPlayer.preloadSoundReference(sprite.say.sound)
|
|
|
|
|
|
|
|
thangTypes = @supermodel.getModels(ThangType)
|
|
|
|
for thangType in thangTypes
|
|
|
|
for trigger, sounds of thangType.get('soundTriggers') or {} when trigger isnt 'say'
|
|
|
|
AudioPlayer.preloadSoundReference sound for sound in sounds
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
# everything else sound wise is loaded as needed as worlds are generated
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
allDone: ->
|
2014-02-13 19:42:35 -05:00
|
|
|
@supermodel.finished() and @sessionsLoaded() and @spriteSheetsBuilt is @spriteSheetsToBuild
|
2014-01-07 02:45:33 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
progress: ->
|
|
|
|
return 0 unless @level.loaded
|
|
|
|
overallProgress = 0
|
|
|
|
supermodelProgress = @supermodel.progress()
|
|
|
|
overallProgress += supermodelProgress * 0.7
|
2014-02-13 19:42:35 -05:00
|
|
|
overallProgress += 0.1 if @sessionsLoaded()
|
2014-02-15 18:44:45 -05:00
|
|
|
if @headless
|
|
|
|
spriteMapProgress = 0.2
|
|
|
|
else
|
|
|
|
spriteMapProgress = if supermodelProgress is 1 then 0.2 else 0
|
|
|
|
spriteMapProgress *= @spriteSheetsBuilt / @spriteSheetsToBuild if @spriteSheetsToBuild
|
2014-01-03 13:32:13 -05:00
|
|
|
overallProgress += spriteMapProgress
|
|
|
|
return overallProgress
|
|
|
|
|
|
|
|
notifyProgress: ->
|
|
|
|
Backbone.Mediator.publish 'level-loader:progress-changed', progress: @progress()
|
2014-01-14 16:16:30 -05:00
|
|
|
@initWorld() if @allDone()
|
|
|
|
@trigger 'loaded-all' if @progress() is 1
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
@supermodel.off 'loaded-one', @onSupermodelLoadedOne
|
2014-02-15 20:29:54 -05:00
|
|
|
@world = null # don't hold onto garbage
|
2014-02-15 18:44:45 -05:00
|
|
|
@update = null
|
|
|
|
super()
|