codecombat/app/views/play/level/thang_avatar_view.coffee

77 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/CocoView'
template = require 'templates/play/level/thang_avatar'
ThangType = require 'models/ThangType'
2014-01-03 13:32:13 -05:00
module.exports = class ThangAvatarView extends View
className: 'thang-avatar-view'
template: template
subscriptions:
2014-06-30 22:16:26 -04:00
'tome:problems-updated': 'onProblemsUpdated'
'god:new-world-created': 'onNewWorld'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
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
2014-06-30 22:16:26 -04:00
unless @thangType.isFullyLoaded() or @thangType.loading
@thangType.fetch()
2014-06-30 22:16:26 -04:00
# couldn't get the level view to load properly through the supermodel
# so just doing it manually this time.
@listenTo @thangType, 'sync', @render
2014-05-20 13:46:52 -04:00
@listenTo @thangType, 'build-complete', @render
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
2014-01-03 13:32:13 -05:00
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super context
context.thang = @thang
2014-01-15 18:16:31 -05:00
options = @thang?.getSpriteOptions() or {}
2014-05-20 13:46:52 -04:00
options.async = true
context.avatarURL = @thangType.getPortraitSource(options) unless @thangType.loading
2014-01-03 13:32:13 -05:00
context.includeName = @includeName
context
setProblems: (problemCount, level) ->
badge = @$el.find('.badge.problems').text(if problemCount then 'x' else '')
badge.removeClass('error warning info')
badge.addClass level if level
setSharedThangs: (sharedThangCount) ->
badge = @$el.find('.badge.shared-thangs').text(if sharedThangCount > 1 then sharedThangCount else '')
# TODO: change the alert color based on whether any of those things that aren't us have problems
#badge.removeClass('error warning info')
#badge.addClass level if level
setSelected: (selected) ->
@$el.toggleClass 'selected', Boolean(selected)
onProblemsUpdated: (e) ->
return unless @thang?.id of e.spell.thangs
2014-01-03 13:32:13 -05:00
myProblems = []
for thangID, spellThang of e.spell.thangs when thangID is @thang.id
#aether = if e.isCast and spellThang.castAether then spellThang.castAether else spellThang.aether
aether = spellThang.castAether # try only paying attention to the actually cast ones
myProblems = myProblems.concat aether.getAllProblems() if aether
worstLevel = null
for level in ['error', 'warning', 'info'] when _.some myProblems, {level: level}
worstLevel = level
break
@setProblems myProblems.length, worstLevel
onNewWorld: (e) ->
@options.thang = @thang = e.world.thangMap[@thang.id] if @thang
2014-02-11 15:02:49 -05:00
destroy: ->
2014-06-30 22:16:26 -04:00
super()