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

76 lines
2.7 KiB
CoffeeScript
Raw Normal View History

2014-08-23 22:00:35 -04:00
CocoView = require 'views/kinds/CocoView'
template = require 'templates/play/level/level-flags-view'
{me} = require 'lib/auth'
module.exports = class LevelFlagsView extends CocoView
id: 'level-flags-view'
template: template
subscriptions:
'playback:real-time-playback-started': 'onRealTimePlaybackStarted'
'playback:real-time-playback-ended': 'onRealTimePlaybackEnded'
'surface:stage-mouse-down': 'onStageMouseDown'
'god:new-world-created': 'onNewWorld'
'god:streaming-world-updated': 'onNewWorld'
2014-08-24 19:09:06 -04:00
'surface:remove-flag': 'onRemoveFlag'
2014-08-23 22:00:35 -04:00
events:
'click .green-flag': -> @onFlagSelected color: 'green', source: 'button'
'click .black-flag': -> @onFlagSelected color: 'black', source: 'button'
'click .violet-flag': -> @onFlagSelected color: 'violet', source: 'button'
shortcuts:
'g': -> @onFlagSelected color: 'green', source: 'shortcut'
'b': -> @onFlagSelected color: 'black', source: 'shortcut'
'v': -> @onFlagSelected color: 'violet', source: 'shortcut'
'esc': -> @onFlagSelected color: null, source: 'shortcut'
2014-08-24 19:09:06 -04:00
'delete, del, backspace': 'onDeletePressed'
2014-08-23 22:00:35 -04:00
constructor: (options) ->
super options
@world = options.world
onRealTimePlaybackStarted: (e) ->
@realTime = true
@$el.show()
@flags = {}
@flagHistory = []
onRealTimePlaybackEnded: (e) ->
@realTime = false
2014-08-24 19:48:59 -04:00
@onFlagSelected color: null
2014-08-23 22:00:35 -04:00
@$el.hide()
onFlagSelected: (e) ->
2014-08-24 19:09:06 -04:00
return if @flagColor is e.color
2014-08-23 22:00:35 -04:00
color = if e.source is 'button' and e.color is @flagColor then null else e.color
@flagColor = color
2014-08-24 19:09:06 -04:00
Backbone.Mediator.publish 'level:flag-color-selected', color: color
2014-08-23 22:00:35 -04:00
@$el.find('.flag-button').removeClass('active')
@$el.find(".#{color}-flag").addClass('active') if color
onStageMouseDown: (e) ->
return unless @flagColor and @realTime
pos = x: e.worldPos.x, y: e.worldPos.y
flag = player: me.id, team: me.team, color: @flagColor, pos: pos, time: @world.dt * @world.frames.length, active: true
2014-08-23 22:00:35 -04:00
@flags[@flagColor] = flag
@flagHistory.push flag
Backbone.Mediator.publish 'level:flag-updated', flag
2014-08-24 19:09:06 -04:00
#console.log 'trying to place flag at', @world.age, 'and think it will happen by', flag.time
2014-08-23 22:00:35 -04:00
2014-08-24 19:09:06 -04:00
onDeletePressed: (e) ->
return unless @realTime
Backbone.Mediator.publish 'surface:remove-selected-flag', {}
@onFlagSelected color: null, source: 'shortcut'
onRemoveFlag: (e) ->
2014-08-23 22:00:35 -04:00
delete @flags[e.color]
flag = player: me.id, team: me.team, color: e.color, time: @world.dt * @world.frames.length, active: false
2014-08-23 22:00:35 -04:00
@flagHistory.push flag
Backbone.Mediator.publish 'level:flag-updated', flag
2014-08-24 19:09:06 -04:00
#console.log e.color, 'deleted at time', flag.time
2014-08-23 22:00:35 -04:00
onNewWorld: (event) ->
return unless event.world.name is @world.name
@world = event.world