Most of the way there getting it to work with loading thang types dynamically throughout the play view.

This commit is contained in:
Scott Erickson 2014-05-13 10:26:33 -07:00
parent 1aa72541ff
commit 1d88b6eefe
8 changed files with 42 additions and 10 deletions

View file

@ -70,7 +70,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@age = 0
@scaleFactor = @targetScaleFactor = 1
@displayObject = new createjs.Container()
if @thangType.get('actions') or @thangType.get('raster')
if @thangType.isFullyLoaded()
@setupSprite()
else
@stillLoading = true
@ -146,6 +146,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
queueAction: (action) ->
# The normal way to have an action play
return unless @thangType.isFullyLoaded()
action = @actions[action] if _.isString(action)
action ?= @actions.idle
@actionQueue = []
@ -266,6 +267,7 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
@hasMoved = true
updateScale: ->
return unless @imageObject
if @thangType.get('matchWorldDimensions') and @thang
if @thang.width isnt @lastThangWidth or @thang.height isnt @lastThangHeight
bounds = @imageObject.getBounds()

View file

@ -101,4 +101,5 @@ module.exports = class Layer extends createjs.Container
cache: ->
return unless @children.length
bounds = @getBounds()
return unless bounds
super bounds.x, bounds.y, bounds.width, bounds.height, 2

View file

@ -200,7 +200,7 @@ module.exports = class Mark extends CocoClass
Backbone.Mediator.publish 'sprite:loaded'
update: (pos=null) ->
return false unless @on and @mark
return false unless @on and @mark and @sprite?.thangType.isFullyLoaded()
@mark.visible = not @hidden
@updatePosition pos
@updateRotation()
@ -242,7 +242,7 @@ module.exports = class Mark extends CocoClass
oldMark.parent.removeChild oldMark
return unless @name in ["selection", "target", "repair", "highlight"]
scale = 0.5
if @sprite
if @sprite?.imageObject
size = @sprite.getAverageDimension()
size += 60 if @name is 'selection'
size += 60 if @name is 'repair'

View file

@ -201,6 +201,8 @@ module.exports = class SpriteBoss extends CocoClass
cache: (update=false) ->
return if @cached and not update
wallSprites = (sprite for sprite in @spriteArray when sprite.thangType?.get('name').search(/(dungeon|indoor).wall/i) isnt -1)
unless _.all (s.thangType.isFullyLoaded() for s in wallSprites)
return
walls = (sprite.thang for sprite in wallSprites)
@world.calculateBounds()
wallGrid = new Grid walls, @world.size()...

View file

@ -19,7 +19,7 @@ module.exports = class SuperModel extends Backbone.Model
loadModel: (model, name, fetchOptions, value=1) ->
cachedModel = @getModelByURL(model.getURL())
if cachedModel
console.debug 'Model cache hit', cachedModel.getURL(), 'already loaded', cachedModel.loaded
console.debug 'Model cache hit', cachedModel.get('name') or cachedModel.getURL()
if cachedModel.loaded
res = @addModelResource(cachedModel, name, fetchOptions, 0)
res.markLoaded()

View file

@ -26,12 +26,18 @@ module.exports = class ThangType extends CocoModel
@buildActions()
@spriteSheets = {}
@building = {}
isFullyLoaded: ->
# TODO: Come up with a better way to identify when the model doesn't have everything needed to build the sprite. ie when it's a projection without all the required data.
return @get('actions') or @get('raster') # needs one of these two things
getActions: ->
return {} unless @isFullyLoaded()
return @actions or @buildActions()
buildActions: ->
@actions = $.extend(true, {}, @get('actions') or {})
return null unless @isFullyLoaded()
@actions = $.extend(true, {}, @get('actions'))
for name, action of @actions
action.name = name
for relatedName, relatedAction of action.relatedActions ? {}
@ -52,6 +58,7 @@ module.exports = class ThangType extends CocoModel
options
buildSpriteSheet: (options) ->
return false unless @isFullyLoaded()
@options = @fillOptions options
key = @spriteSheetKey(@options)
return if @building[key]
@ -146,7 +153,7 @@ module.exports = class ThangType extends CocoModel
return true
t0 = new Date()
spriteSheet = @builder.build()
console.warn "Built #{@get('name')} in #{new Date() - t0}ms on main thread."
console.debug "Built #{@get('name')} in #{new Date() - t0}ms."
@spriteSheets[key] = spriteSheet
delete @building[key]
spriteSheet
@ -180,6 +187,7 @@ module.exports = class ThangType extends CocoModel
stage?.toDataURL()
getPortraitStage: (spriteOptionsOrKey, size=100) ->
return unless @isFullyLoaded()
key = spriteOptionsOrKey
key = if _.isString(key) then key else @spriteSheetKey(@fillOptions(key))
spriteSheet = @spriteSheets[key]

View file

@ -109,6 +109,13 @@ module.exports = class HUDView extends View
@update()
createAvatar: (thangType, thang, colorConfig) ->
unless thangType.isFullyLoaded()
args = arguments
unless @listeningToCreateAvatar
@listenToOnce thangType, 'sync', -> @createAvatar(args...)
@listeningToCreateAvatar = true
return
@listeningToCreateAvatar = false
options = thang.getSpriteOptions() or {}
options.async = false
options.colorConfig = colorConfig if colorConfig

View file

@ -14,16 +14,28 @@ module.exports = class ThangAvatarView extends View
super options
@thang = options.thang
@includeName = options.includeName
@thangType = @getSpriteThangType()
if not @thangType
console.error 'Thang avatar view expected a thang type to be provided.'
return
unless @thangType.isFullyLoaded() or @thangType.loading
@thangType.fetch()
@supermodel.loadModel @thangType, 'thang'
getSpriteThangType: ->
thangs = @supermodel.getModels(ThangType)
thangs = (t for t in thangs when t.get('name') is @thang.spriteName)
loadedThangs = (t for t in thangs when t.isFullyLoaded())
return loadedThangs[0] or thangs[0] # try to return one with all the goods, otherwise a projection
getRenderData: (context={}) ->
context = super context
context.thang = @thang
thangs = @supermodel.getModels(ThangType)
thangs = (t for t in thangs when t.get('name') is @thang.spriteName)
thang = thangs[0]
options = @thang?.getSpriteOptions() or {}
options.async = false
context.avatarURL = thang.getPortraitSource(options)
context.avatarURL = @thangType.getPortraitSource(options)
context.includeName = @includeName
context