2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/play/level/hud'
|
|
|
|
prop_template = require 'templates/play/level/hud_prop'
|
2014-11-29 16:09:38 -05:00
|
|
|
utils = require 'core/utils'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-23 10:02:45 -04:00
|
|
|
module.exports = class LevelHUDView extends CocoView
|
2014-01-03 13:32:13 -05:00
|
|
|
id: 'thang-hud'
|
|
|
|
template: template
|
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'surface:frame-changed': 'onFrameChanged'
|
2014-08-27 15:24:03 -04:00
|
|
|
'level:disable-controls': 'onDisableControls'
|
|
|
|
'level:enable-controls': 'onEnableControls'
|
2014-01-03 13:32:13 -05:00
|
|
|
'surface:sprite-selected': 'onSpriteSelected'
|
2014-08-27 15:24:03 -04:00
|
|
|
'sprite:thang-began-talking': 'onThangBeganTalking'
|
|
|
|
'sprite:thang-finished-talking': 'onThangFinishedTalking'
|
2014-01-15 16:04:48 -05:00
|
|
|
'god:new-world-created': 'onNewWorld'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
2014-02-16 18:30:00 -05:00
|
|
|
'click': 'onClick'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
afterRender: ->
|
2014-01-09 14:04:22 -05:00
|
|
|
super()
|
|
|
|
@$el.addClass 'no-selection'
|
2014-12-28 16:25:20 -05:00
|
|
|
if @options.level.get('hidesHUD')
|
2014-10-30 19:59:32 -04:00
|
|
|
@hidesHUD = true
|
|
|
|
@$el.addClass 'hide-hud-properties'
|
2014-01-09 14:04:22 -05:00
|
|
|
|
2014-02-16 18:30:00 -05:00
|
|
|
onClick: (e) ->
|
2014-08-27 21:43:17 -04:00
|
|
|
Backbone.Mediator.publish 'tome:focus-editor', {} unless $(e.target).parents('.thang-props').length
|
2014-02-16 18:30:00 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onFrameChanged: (e) ->
|
|
|
|
@timeProgress = e.progress
|
|
|
|
@update()
|
|
|
|
|
|
|
|
onDisableControls: (e) ->
|
|
|
|
return if e.controls and not ('hud' in e.controls)
|
|
|
|
@disabled = true
|
2014-11-08 11:58:36 -05:00
|
|
|
@$el.addClass 'controls-disabled'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onEnableControls: (e) ->
|
|
|
|
return if e.controls and not ('hud' in e.controls)
|
|
|
|
@disabled = false
|
2014-11-08 11:58:36 -05:00
|
|
|
@$el.removeClass 'controls-disabled'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onSpriteSelected: (e) ->
|
2014-11-07 22:15:55 -05:00
|
|
|
return if @disabled
|
2014-01-09 14:04:22 -05:00
|
|
|
@setThang e.thang, e.sprite?.thangType
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-15 16:04:48 -05:00
|
|
|
onNewWorld: (e) ->
|
2014-02-25 15:39:22 -05:00
|
|
|
hadThang = @thang
|
2014-01-15 16:04:48 -05:00
|
|
|
@thang = e.world.thangMap[@thang.id] if @thang
|
2014-02-25 15:39:22 -05:00
|
|
|
if hadThang and not @thang
|
2014-02-05 18:54:34 -05:00
|
|
|
@setThang null, null
|
2014-01-15 16:04:48 -05:00
|
|
|
|
2014-01-09 14:04:22 -05:00
|
|
|
setThang: (thang, thangType) ->
|
2014-11-07 22:15:55 -05:00
|
|
|
if not thang? and not @thang? then return
|
|
|
|
if thang? and @thang? and thang.id is @thang.id then return
|
2014-11-21 13:14:02 -05:00
|
|
|
if thang? and @hidesHUD and thang.id isnt 'Hero Placeholder' then return # Don't let them find the names of their opponents this way
|
2014-11-23 00:36:14 -05:00
|
|
|
return unless thang # Don't let them deselect anything, ever.
|
2014-01-03 13:32:13 -05:00
|
|
|
@thang = thang
|
2014-01-09 14:04:22 -05:00
|
|
|
@thangType = thangType
|
2014-11-07 22:15:55 -05:00
|
|
|
return unless @thang
|
2014-01-15 18:16:31 -05:00
|
|
|
@createAvatar thangType, @thang
|
2014-01-03 13:32:13 -05:00
|
|
|
@createProperties()
|
|
|
|
@update()
|
|
|
|
|
2014-02-19 16:43:44 -05:00
|
|
|
createAvatar: (thangType, thang, colorConfig) ->
|
2014-05-13 13:26:33 -04:00
|
|
|
unless thangType.isFullyLoaded()
|
|
|
|
args = arguments
|
|
|
|
unless @listeningToCreateAvatar
|
|
|
|
@listenToOnce thangType, 'sync', -> @createAvatar(args...)
|
|
|
|
@listeningToCreateAvatar = true
|
|
|
|
return
|
|
|
|
@listeningToCreateAvatar = false
|
2014-09-28 17:00:48 -04:00
|
|
|
options = thang.getLankOptions() or {}
|
2014-01-15 18:16:31 -05:00
|
|
|
options.async = false
|
2014-02-20 18:13:15 -05:00
|
|
|
options.colorConfig = colorConfig if colorConfig
|
2014-01-09 14:04:22 -05:00
|
|
|
wrapper = @$el.find '.thang-canvas-wrapper'
|
2014-11-07 22:15:55 -05:00
|
|
|
team = @thang?.team
|
2014-01-30 15:03:33 -05:00
|
|
|
wrapper.removeClass (i, css) -> (css.match(/\bteam-\S+/g) or []).join ' '
|
|
|
|
wrapper.addClass "team-#{team}"
|
2014-05-02 20:03:30 -04:00
|
|
|
if thangType.get('raster')
|
2014-11-19 19:36:06 -05:00
|
|
|
wrapper.empty().append($('<img draggable="false"/>').addClass('avatar').attr('src', '/file/'+thangType.get('raster')))
|
2014-05-02 20:03:30 -04:00
|
|
|
else
|
2014-11-07 22:15:55 -05:00
|
|
|
return unless stage = thangType.getPortraitStage options, 100
|
|
|
|
newCanvas = $(stage.canvas).addClass('thang-canvas avatar')
|
2014-05-02 20:03:30 -04:00
|
|
|
wrapper.empty().append(newCanvas)
|
|
|
|
stage.update()
|
|
|
|
@stage?.stopTalking()
|
|
|
|
@stage = stage
|
2014-11-19 19:36:06 -05:00
|
|
|
wrapper.append($('<img draggable="false" />').addClass('avatar-frame').attr('src', '/images/level/thang_avatar_frame.png'))
|
2014-01-09 14:04:22 -05:00
|
|
|
|
|
|
|
onThangBeganTalking: (e) ->
|
|
|
|
return unless @stage and @thang is e.thang
|
|
|
|
@stage?.startTalking()
|
|
|
|
|
|
|
|
onThangFinishedTalking: (e) ->
|
|
|
|
return unless @stage and @thang is e.thang
|
|
|
|
@stage?.stopTalking()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
createProperties: ->
|
2014-11-11 19:36:44 -05:00
|
|
|
if @thang.id in ['Hero Placeholder', 'Hero Placeholder 1']
|
|
|
|
name = {knight: 'Tharin', captain: 'Anya', librarian: 'Hushbaum', sorcerer: 'Pender', 'potion-master': 'Omarn', samurai: 'Hattori', ninja: 'Amara'}[@thang.type] ? 'Hero'
|
2014-09-22 17:05:13 -04:00
|
|
|
else
|
2015-01-31 01:07:23 -05:00
|
|
|
name = @thang.hudName or (if @thang.type then "#{@thang.id} - #{@thang.type}" else @thang.id)
|
2014-11-29 16:09:38 -05:00
|
|
|
utils.replaceText @$el.find('.thang-name'), name
|
2014-11-07 22:15:55 -05:00
|
|
|
props = @$el.find('.thang-props')
|
|
|
|
props.find('.prop').remove()
|
|
|
|
#propNames = _.without @thang.hudProperties ? [], 'action'
|
|
|
|
propNames = @thang.hudProperties
|
2014-03-03 19:45:03 -05:00
|
|
|
for prop, i in propNames
|
2014-01-03 13:32:13 -05:00
|
|
|
pel = @createPropElement prop
|
|
|
|
continue unless pel?
|
|
|
|
if pel.find('.bar').is('*') and props.find('.bar').is('*')
|
|
|
|
props.find('.bar-prop').last().after pel # Keep bars together
|
|
|
|
else
|
2014-11-07 22:15:55 -05:00
|
|
|
props.append pel
|
2014-03-01 14:06:44 -05:00
|
|
|
null
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
update: ->
|
2014-11-07 22:15:55 -05:00
|
|
|
return unless @thang
|
2014-03-13 18:35:28 -04:00
|
|
|
@$el.find('.thang-props-column').toggleClass 'nonexistent', not @thang.exists
|
|
|
|
if @thang.exists
|
|
|
|
@updatePropElement(prop, @thang[prop]) for prop in @thang.hudProperties ? []
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
createPropElement: (prop) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
if prop in ['maxHealth']
|
2014-01-03 13:32:13 -05:00
|
|
|
return null # included in the bar
|
|
|
|
context =
|
|
|
|
prop: prop
|
2014-12-23 20:14:09 -05:00
|
|
|
hasIcon: prop in ['health', 'pos', 'target', 'collectedThangIDs', 'gold', 'bountyGold', 'value', 'visualRange', 'attackDamage', 'attackRange', 'maxSpeed', 'attackNearbyEnemyRange']
|
2014-06-30 22:16:26 -04:00
|
|
|
hasBar: prop in ['health']
|
2014-01-03 13:32:13 -05:00
|
|
|
$(prop_template(context))
|
|
|
|
|
|
|
|
updatePropElement: (prop, val) ->
|
|
|
|
pel = @$el.find '.thang-props *[name=' + prop + ']'
|
2014-06-30 22:16:26 -04:00
|
|
|
if prop in ['maxHealth']
|
2014-03-01 14:06:44 -05:00
|
|
|
return # Don't show maxes--they're built into bar labels.
|
2014-06-30 22:16:26 -04:00
|
|
|
if prop in ['health']
|
|
|
|
max = @thang['max' + prop.charAt(0).toUpperCase() + prop.slice(1)]
|
|
|
|
regen = @thang[prop + 'ReplenishRate']
|
2014-01-03 13:32:13 -05:00
|
|
|
percent = Math.round 100 * val / max
|
2014-06-30 22:16:26 -04:00
|
|
|
pel.find('.bar').css 'width', percent + '%'
|
|
|
|
labelText = prop + ': ' + @formatValue(prop, val) + ' / ' + @formatValue(prop, max)
|
2014-01-03 13:32:13 -05:00
|
|
|
if regen
|
2014-06-30 22:16:26 -04:00
|
|
|
labelText += ' (+' + @formatValue(prop, regen) + '/s)'
|
2014-11-29 16:09:38 -05:00
|
|
|
utils.replaceText pel.find('.bar-prop-value'), Math.round(val)
|
2014-01-03 13:32:13 -05:00
|
|
|
else
|
|
|
|
s = @formatValue(prop, val)
|
2014-03-01 14:06:44 -05:00
|
|
|
labelText = "#{prop}: #{s}"
|
|
|
|
if prop is 'attackDamage'
|
|
|
|
cooldown = @thang.actions.attack.cooldown
|
|
|
|
dps = @thang.attackDamage / cooldown
|
|
|
|
labelText += " / #{cooldown.toFixed(2)}s (DPS: #{dps.toFixed(2)})"
|
2014-11-29 16:09:38 -05:00
|
|
|
utils.replaceText pel.find('.prop-value'), s
|
2014-03-01 14:06:44 -05:00
|
|
|
pel.attr 'title', labelText
|
2014-01-03 13:32:13 -05:00
|
|
|
pel
|
|
|
|
|
|
|
|
formatValue: (prop, val) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
if prop is 'target' and not val
|
|
|
|
val = @thang['targetPos']
|
2014-01-03 13:32:13 -05:00
|
|
|
val = null if val?.isZero()
|
2014-06-30 22:16:26 -04:00
|
|
|
if prop is 'rotation'
|
|
|
|
return (val * 180 / Math.PI).toFixed(0) + '˚'
|
2014-03-01 14:06:44 -05:00
|
|
|
if prop.search(/Range$/) isnt -1
|
|
|
|
return val + 'm'
|
2014-01-03 13:32:13 -05:00
|
|
|
if typeof val is 'number'
|
2014-02-26 21:09:09 -05:00
|
|
|
if Math.round(val) == val or prop is 'gold' then return val.toFixed(0) # int
|
2014-01-03 13:32:13 -05:00
|
|
|
if -10 < val < 10 then return val.toFixed(2)
|
|
|
|
if -100 < val < 100 then return val.toFixed(1)
|
|
|
|
return val.toFixed(0)
|
2014-06-30 22:16:26 -04:00
|
|
|
if val and typeof val is 'object'
|
2014-01-03 13:32:13 -05:00
|
|
|
if val.id
|
|
|
|
return val.id
|
|
|
|
else if val.x and val.y
|
2014-01-29 11:38:37 -05:00
|
|
|
return "x: #{val.x.toFixed(0)} y: #{val.y.toFixed(0)}"
|
|
|
|
#return "x: #{val.x.toFixed(0)} y: #{val.y.toFixed(0)}, z: #{val.z.toFixed(0)}" # Debugging: include z
|
2014-01-03 13:32:13 -05:00
|
|
|
else if not val?
|
2014-06-30 22:16:26 -04:00
|
|
|
return 'No ' + prop
|
2014-01-03 13:32:13 -05:00
|
|
|
return val
|
|
|
|
|
2014-01-09 14:04:22 -05:00
|
|
|
destroy: ->
|
2014-01-09 14:05:51 -05:00
|
|
|
@stage?.stopTalking()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|