2014-11-28 20:49:41 -05:00
ModalView = require ' views/core/ModalView '
2014-11-29 15:46:04 -05:00
template = require ' templates/play/menu/inventory-modal '
2014-11-24 13:51:20 -05:00
buyGemsPromptTemplate = require ' templates/play/modal/buy-gems-prompt '
2014-11-28 20:49:41 -05:00
{ me } = require ' core/auth '
2014-08-08 14:36:41 -04:00
ThangType = require ' models/ThangType '
2014-08-13 20:21:37 -04:00
CocoCollection = require ' collections/CocoCollection '
ItemView = require ' ./ItemView '
2014-09-21 02:06:28 -04:00
SpriteBuilder = require ' lib/sprites/SpriteBuilder '
2014-11-10 18:24:05 -05:00
ItemDetailsView = require ' views/play/modal/ItemDetailsView '
Purchase = require ' models/Purchase '
2014-11-10 19:09:41 -05:00
LevelOptions = require ' lib/LevelOptions '
2014-11-24 13:51:20 -05:00
BuyGemsModal = require ' views/play/modal/BuyGemsModal '
2014-08-13 20:21:37 -04:00
2014-11-06 19:23:23 -05:00
hasGoneFullScreenOnce = false
module.exports = class InventoryModal extends ModalView
id: ' inventory-modal '
className: ' modal fade play-modal '
2014-08-08 14:36:41 -04:00
template: template
2014-10-21 16:57:49 -04:00
slots: [ ' head ' , ' eyes ' , ' neck ' , ' torso ' , ' wrists ' , ' gloves ' , ' left-ring ' , ' right-ring ' , ' right-hand ' , ' left-hand ' , ' waist ' , ' feet ' , ' programming-book ' , ' pet ' , ' minion ' , ' flag ' ] #, 'misc-0', 'misc-1'] # TODO: bring in misc slot(s) again when we have space
2014-11-10 18:24:05 -05:00
closesOnClickOutside: false # because draggable somehow triggers hide when you don't drag onto a draggable
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
events:
' click .item-slot ' : ' onItemSlotClick '
2014-11-18 16:02:01 -05:00
' click # unequipped .item ' : ' onUnequippedItemClick '
' doubletap # unequipped .item ' : ' onUnequippedItemDoubleClick '
' doubletap .item-slot .item ' : ' onEquippedItemDoubleClick '
' click button.equip-item ' : ' onClickEquipItemButton '
2014-11-06 19:23:23 -05:00
' shown.bs.modal ' : ' onShown '
' click # choose-hero-button ' : ' onClickChooseHero '
' click # play-level-button ' : ' onClickPlayLevel '
2014-11-10 18:24:05 -05:00
' click .unlock-button ' : ' onUnlockButtonClicked '
' click # equip-item-viewed ' : ' onClickEquipItemViewed '
' click # unequip-item-viewed ' : ' onClickUnequipItemViewed '
' click # close-modal ' : ' hide '
2014-11-24 13:51:20 -05:00
' click .buy-gems-prompt-button ' : ' onBuyGemsPromptButtonClicked '
' click ' : ' onClickedSomewhere '
2014-09-21 02:06:28 -04:00
2014-08-13 20:21:37 -04:00
shortcuts:
' esc ' : ' clearSelection '
2014-11-06 19:23:23 -05:00
' enter ' : ' onClickPlayLevel '
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
#- Setup
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
initialize: (options) ->
super ( arguments . . . )
2014-08-14 18:09:10 -04:00
@items = new CocoCollection ( [ ] , { model: ThangType } )
2014-11-10 18:24:05 -05:00
# TODO: switch to item store loading system?
@items.url = ' /db/thang.type?view=items '
@ items . setProjection [
2014-11-11 01:07:55 -05:00
' name '
' slug '
' components '
' original '
' rasterIcon '
2014-11-26 17:18:17 -05:00
' dollImages '
2014-11-11 01:07:55 -05:00
' gems '
' tier '
' description '
' heroClass '
2014-11-10 18:24:05 -05:00
' i18n '
]
2014-08-13 20:21:37 -04:00
@ supermodel . loadCollection ( @ items , ' items ' )
2014-11-10 19:09:41 -05:00
@equipment = { } # Assign for real when we have loaded the session and items.
2014-09-21 02:06:28 -04:00
2014-11-10 18:24:05 -05:00
onItemsLoaded: ->
2014-11-10 19:09:41 -05:00
item.notInLevel = true for item in @ items . models
@equipment = @ options . equipment or @ options . session ? . get ( ' heroConfig ' ) ? . inventory or me . get ( ' heroConfig ' ) ? . inventory or { }
@equipment = $ . extend true , { } , @ equipment
2014-11-10 18:24:05 -05:00
@ requireLevelEquipment ( )
@itemGroups = { }
@itemGroups.availableItems = new Backbone . Collection ( )
@itemGroups.restrictedItems = new Backbone . Collection ( )
@itemGroups.lockedItems = new Backbone . Collection ( )
2014-11-26 12:22:30 -05:00
itemGroup.comparator = ( (m) -> m . get ( ' tier ' ) ? m . get ( ' gems ' ) ) for itemGroup in _ . values @ itemGroups
2014-11-10 18:24:05 -05:00
equipped = _ . values ( @ equipment )
@ sortItem ( item , equipped ) for item in @ items . models
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
sortItem: (item, equipped) ->
equipped ? = _ . values ( @ equipment )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
# general starting classes
item.classes = _ . clone ( item . getAllowedSlots ( ) )
for heroClass in item . getAllowedHeroClasses ( )
item . classes . push heroClass
item . classes . push ' equipped ' if item . get ( ' original ' ) in equipped
# sort into one of the four groups
2014-11-10 19:09:41 -05:00
locked = not ( item . get ( ' original ' ) in me . items ( ) )
2014-11-21 23:36:56 -05:00
#locked = false if me.get('slug') is 'nick'
2014-11-10 18:24:05 -05:00
2014-11-28 15:11:51 -05:00
if not item . getFrontFacingStats ( ) . props . length and not _ . size ( item . getFrontFacingStats ( ) . stats ) and locked # Temp: while there are placeholder items
2014-11-18 22:37:08 -05:00
null # Don't put into a collection
else if locked and item . get ( ' slug ' ) isnt ' simple-boots '
2014-11-10 18:24:05 -05:00
item . classes . push ' locked '
2014-11-29 20:06:46 -05:00
if item . isSilhouettedItem or not item . get ( ' gems ' )
# Don't even load/show these--don't add to a collection. (Bandwidth optimization.)
null
else
@ itemGroups . lockedItems . add ( item )
2014-11-10 19:09:41 -05:00
else if item . get ( ' slug ' ) in _ . values ( LevelOptions [ @ options . levelID ] ? . restrictedGear ? { } )
2014-11-10 18:24:05 -05:00
@ itemGroups . restrictedItems . add ( item )
item . classes . push ' restricted '
else
@ itemGroups . availableItems . add ( item )
2014-11-10 19:09:41 -05:00
2014-11-26 10:33:07 -05:00
# level to unlock
item.level = item . levelRequiredForItem ( ) if item . get ( ' tier ' ) ?
2014-08-13 20:21:37 -04:00
onLoaded: ->
2014-11-10 19:09:41 -05:00
# Both items and session have been loaded.
@ onItemsLoaded ( )
2014-08-13 20:21:37 -04:00
super ( )
2014-08-08 14:36:41 -04:00
getRenderData: (context={}) ->
context = super ( context )
2014-08-13 20:21:37 -04:00
context.equipped = _ . values ( @ equipment )
2014-08-14 18:09:10 -04:00
context.items = @ items . models
2014-11-10 18:24:05 -05:00
context.itemGroups = @ itemGroups
2014-08-13 20:21:37 -04:00
context.slots = @ slots
2014-11-10 18:24:05 -05:00
context.selectedHero = @ selectedHero
2014-08-13 20:21:37 -04:00
context.equipment = _ . clone @ equipment
2014-11-10 18:24:05 -05:00
context . equipment [ slot ] = @ items . findWhere { original: itemOriginal } for slot , itemOriginal of context . equipment
context.gems = me . gems ( )
2014-08-08 14:36:41 -04:00
context
afterRender: ->
super ( )
2014-11-10 19:09:41 -05:00
@ $el . find ( ' # play-level-button ' ) . css ( ' visibility ' , ' hidden ' )
2014-08-13 20:21:37 -04:00
return unless @ supermodel . finished ( )
2014-11-10 19:09:41 -05:00
@ $el . find ( ' # play-level-button ' ) . css ( ' visibility ' , ' visible ' )
2014-11-10 18:24:05 -05:00
@ setUpDraggableEventsForAvailableEquipment ( )
@ setUpDraggableEventsForEquippedArea ( )
@ delegateEvents ( )
@itemDetailsView = new ItemDetailsView ( )
@ insertSubView ( @ itemDetailsView )
@ requireLevelEquipment ( )
2014-11-12 21:01:35 -05:00
@ $el . find ( ' .nano ' ) . nanoScroller ( { alwaysVisible: true } )
2014-11-18 16:20:51 -05:00
@ onSelectionChanged ( )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
afterInsert: ->
super ( )
@canvasWidth = @ $el . find ( ' canvas ' ) . innerWidth ( )
@canvasHeight = @ $el . find ( ' canvas ' ) . innerHeight ( )
@inserted = true
@ requireLevelEquipment ( )
2014-11-26 22:04:47 -05:00
@ onEquipmentChanged ( )
2014-11-10 18:24:05 -05:00
#- Draggable logic
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
setUpDraggableEventsForAvailableEquipment: ->
2014-11-18 16:02:01 -05:00
for availableItemEl in @ $el . find ( ' # unequipped .item ' )
2014-11-10 18:24:05 -05:00
availableItemEl = $ ( availableItemEl )
continue if availableItemEl . hasClass ( ' locked ' ) or availableItemEl . hasClass ( ' restricted ' )
dragHelper = availableItemEl . clone ( ) . addClass ( ' draggable-item ' )
do (dragHelper, availableItemEl) =>
availableItemEl . draggable
2014-09-21 02:06:28 -04:00
revert: ' invalid '
appendTo: @ $el
cursorAt: { left: 35.5 , top: 35.5 }
helper: -> dragHelper
revertDuration: 200
2014-09-21 23:19:27 -04:00
distance: 10
2014-09-21 02:06:28 -04:00
scroll: false
2014-11-10 18:24:05 -05:00
zIndex: 1100
availableItemEl . on ' dragstart ' , => @ selectUnequippedItem ( availableItemEl )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
setUpDraggableEventsForEquippedArea: ->
2014-09-21 02:06:28 -04:00
for itemSlot in @ $el . find ' .item-slot '
slot = $ ( itemSlot ) . data ' slot '
2014-10-09 18:54:18 -04:00
do (slot, itemSlot) =>
2014-09-21 02:06:28 -04:00
$ ( itemSlot ) . droppable
2014-11-10 18:24:05 -05:00
drop: (e, ui) => @ equipSelectedItem ( )
2014-09-21 02:06:28 -04:00
accept: (el) -> $ ( el ) . parent ( ) . hasClass slot
activeClass: ' droppable '
hoverClass: ' droppable-hover '
2014-09-21 14:24:37 -04:00
tolerance: ' touch '
2014-10-09 18:54:18 -04:00
@ makeEquippedSlotDraggable $ ( itemSlot )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
@ $el . find ( ' # equipped ' ) . droppable
drop: (e, ui) => @ equipSelectedItem ( )
2014-10-27 14:17:34 -04:00
accept: (el) -> true
activeClass: ' droppable '
hoverClass: ' droppable-hover '
tolerance: ' pointer '
2014-11-10 19:09:41 -05:00
2014-10-09 18:54:18 -04:00
makeEquippedSlotDraggable: (slot) ->
2014-11-10 21:13:04 -05:00
unequip = =>
@ unequipItemFromSlot slot
@ requireLevelEquipment ( )
2014-10-09 18:54:18 -04:00
shouldStayEquippedWhenDropped = (isValidDrop) ->
pos = $ ( @ ) . position ( )
revert = Math . abs ( pos . left ) < $ ( @ ) . outerWidth ( ) and Math . abs ( pos . top ) < $ ( @ ) . outerHeight ( )
unequip ( ) if not revert
revert
# TODO: figure out how to make this actually above the available items list (the .ui-draggable-helper img is still inside .item-view and so underlaps...)
$ ( slot ) . find ( ' img ' ) . draggable
revert: shouldStayEquippedWhenDropped
appendTo: @ $el
cursorAt: { left: 35.5 , top: 35.5 }
revertDuration: 200
distance: 10
scroll: false
2014-11-10 18:24:05 -05:00
zIndex: 100
slot . on ' dragstart ' , => @ selectItemSlot ( slot )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
#- Select/equip event handlers
2014-08-13 20:21:37 -04:00
onItemSlotClick: (e) ->
2014-11-05 17:17:11 -05:00
return if @ remainingRequiredEquipment ? . length # Don't let them select a slot if we need them to first equip some require gear.
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-10 18:24:05 -05:00
@ selectItemSlot ( $ ( e . target ) . closest ( ' .item-slot ' ) )
onUnequippedItemClick: (e) ->
return if @ justDoubleClicked
2014-11-18 16:02:01 -05:00
itemEl = $ ( e . target ) . closest ( ' .item ' )
2014-11-26 10:33:07 -05:00
@ playSound ' menu-button-click '
2014-11-10 18:24:05 -05:00
@ selectUnequippedItem ( itemEl )
onUnequippedItemDoubleClick: (e) ->
2014-11-18 16:02:01 -05:00
itemEl = $ ( e . target ) . closest ( ' .item ' )
return if itemEl . hasClass ( ' locked ' ) or itemEl . hasClass ( ' restricted ' )
2014-11-10 18:24:05 -05:00
@ equipSelectedItem ( )
@justDoubleClicked = true
_ . defer => @justDoubleClicked = false
onEquippedItemDoubleClick: -> @ unequipSelectedItem ( )
onClickEquipItemViewed: -> @ equipSelectedItem ( )
onClickUnequipItemViewed: -> @ unequipSelectedItem ( )
2014-11-10 19:09:41 -05:00
2014-11-18 16:02:01 -05:00
onClickEquipItemButton: (e) ->
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-18 16:02:01 -05:00
itemEl = $ ( e . target ) . closest ( ' .item ' )
@ selectUnequippedItem ( itemEl )
@ equipSelectedItem ( )
2014-11-10 18:24:05 -05:00
#- Select/equip higher-level, all encompassing methods the callbacks all use
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
selectItemSlot: (slotEl) ->
@ clearSelection ( )
slotEl . addClass ( ' selected ' )
2014-11-18 16:02:01 -05:00
selectedSlotItemID = slotEl . find ( ' .item ' ) . data ( ' item-id ' )
2014-11-10 18:24:05 -05:00
item = @ items . get ( selectedSlotItemID )
if item then @ showItemDetails ( item , ' unequip ' )
2014-08-13 20:21:37 -04:00
@ onSelectionChanged ( )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
selectUnequippedItem: (itemEl) ->
@ clearSelection ( )
itemEl . addClass ( ' active ' )
showExtra = if itemEl . hasClass ( ' restricted ' ) then ' restricted ' else if not itemEl . hasClass ( ' locked ' ) then ' equip ' else ' '
@ showItemDetails ( @ items . get ( itemEl . data ( ' item-id ' ) ) , showExtra )
2014-08-13 20:21:37 -04:00
@ onSelectionChanged ( )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
equipSelectedItem: ->
selectedItemEl = @ getSelectedUnequippedItem ( )
selectedItem = @ items . get ( selectedItemEl . data ( ' item-id ' ) )
return unless selectedItem
allowedSlots = selectedItem . getAllowedSlots ( )
slotEl = @ $el . find ( " .item-slot[data-slot= ' #{ allowedSlots [ 0 ] } ' ] " )
selectedItemEl . effect ( ' transfer ' , to: slotEl , duration: 500 , easing: ' easeOutCubic ' )
unequipped = @ unequipItemFromSlot ( slotEl )
selectedItemEl . addClass ( ' equipped ' )
2014-11-18 16:02:01 -05:00
slotEl . append ( selectedItemEl . find ( ' img ' ) . clone ( ) . addClass ( ' item ' ) . data ( ' item-id ' , selectedItem . id ) )
2014-11-10 18:24:05 -05:00
@ clearSelection ( )
@ showItemDetails ( selectedItem , ' unequip ' )
slotEl . addClass ( ' selected ' )
selectedItem . classes . push ' equipped '
@ makeEquippedSlotDraggable slotEl
@ requireLevelEquipment ( )
2014-08-13 20:21:37 -04:00
@ onSelectionChanged ( )
2014-11-26 22:04:47 -05:00
@ onEquipmentChanged ( )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
unequipSelectedItem: ->
slotEl = @ getSelectedSlot ( )
@ clearSelection ( )
itemEl = @ unequipItemFromSlot ( slotEl )
return unless itemEl
itemEl . addClass ( ' active ' )
slotEl . effect ( ' transfer ' , to: itemEl , duration: 500 , easing: ' easeOutCubic ' )
selectedSlotItemID = itemEl . data ( ' item-id ' )
item = @ items . get ( selectedSlotItemID )
item.classes = _ . without item . classes , ' equipped '
@ showItemDetails ( item , ' equip ' )
@ requireLevelEquipment ( )
2014-08-14 16:41:32 -04:00
@ onSelectionChanged ( )
2014-11-26 22:04:47 -05:00
@ onEquipmentChanged ( )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
#- Select/equip helpers
clearSelection: ->
@ deselectAllSlots ( )
@ deselectAllUnequippedItems ( )
@ hideItemDetails ( )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
unequipItemFromSlot: (slotEl) ->
2014-11-18 16:02:01 -05:00
itemEl = slotEl . find ( ' .item ' )
2014-11-10 18:24:05 -05:00
itemIDToUnequip = itemEl . data ( ' item-id ' )
return unless itemIDToUnequip
itemEl . remove ( )
2014-11-18 16:02:01 -05:00
@ $el . find ( " # unequipped .item[data-item-id= #{ itemIDToUnequip } ] " ) . removeClass ( ' equipped ' )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
deselectAllSlots: ->
2014-09-21 02:06:28 -04:00
@ $el . find ( ' # equipped .item-slot.selected ' ) . removeClass ( ' selected ' )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
deselectAllUnequippedItems: ->
2014-11-18 16:02:01 -05:00
@ $el . find ( ' # unequipped .item ' ) . removeClass ( ' active ' )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
getSlot: (name) ->
@ $el . find ( " .item-slot[data-slot= #{ name } ] " )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
getSelectedSlot: ->
@ $el . find ( ' # equipped .item-slot.selected ' )
2014-08-14 18:09:10 -04:00
2014-11-10 18:24:05 -05:00
getSelectedUnequippedItem: ->
2014-11-18 16:02:01 -05:00
@ $el . find ( ' # unequipped .item.active ' )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
onSelectionChanged: ->
2014-11-18 16:20:51 -05:00
heroClass = @ selectedHero ? . get ( ' heroClass ' )
itemsCanBeEquipped = @ $el . find ( ' # unequipped .item.available:not(.equipped) ' ) . filter ( ' . ' + heroClass ) . length
toShow = @ $el . find ( ' # double-click-hint, # available-description ' )
if itemsCanBeEquipped then toShow . removeClass ( ' secret ' ) else toShow . addClass ( ' secret ' )
2014-08-13 20:21:37 -04:00
@ delegateEvents ( )
2014-08-14 18:09:10 -04:00
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
showItemDetails: (item, showExtra) ->
@ itemDetailsView . setItem ( item )
@ $el . find ( ' # item-details-extra > * ' ) . addClass ( ' secret ' )
@ $el . find ( " # #{ showExtra } -item-viewed " ) . removeClass ( ' secret ' )
2014-11-10 19:09:41 -05:00
2014-11-10 18:24:05 -05:00
hideItemDetails: ->
@ itemDetailsView . setItem ( null )
@ $el . find ( ' # item-details-extra > * ' ) . addClass ( ' secret ' )
2014-08-13 20:21:37 -04:00
getCurrentEquipmentConfig: ->
config = { }
for slot in @ $el . find ( ' .item-slot ' )
slotName = $ ( slot ) . data ( ' slot ' )
2014-11-18 16:02:01 -05:00
slotItemID = $ ( slot ) . find ( ' .item ' ) . data ( ' item-id ' )
2014-08-13 20:21:37 -04:00
continue unless slotItemID
item = _ . find @ items . models , { id : slotItemID }
config [ slotName ] = item . get ( ' original ' )
2014-08-14 18:09:10 -04:00
config
2014-10-09 18:54:18 -04:00
requireLevelEquipment: ->
2014-11-05 23:56:17 -05:00
# This is temporary, until we have a more general way of awarding items and configuring required/restricted items per level.
2014-11-10 19:09:41 -05:00
requiredGear = LevelOptions [ @ options . levelID ] ? . requiredGear ? { }
restrictedGear = LevelOptions [ @ options . levelID ] ? . restrictedGear ? { }
2014-10-09 22:59:57 -04:00
if @ inserted
if @ supermodel . finished ( )
equipment = @ getCurrentEquipmentConfig ( ) # Make sure @equipment is updated
else
equipment = @ equipment
hadRequired = @ remainingRequiredEquipment ? . length
@remainingRequiredEquipment = [ ]
@ $el . find ( ' .should-equip ' ) . removeClass ( ' should-equip ' )
inWorldMap = $ ( ' # world-map-view ' ) . length
2014-11-11 19:36:44 -05:00
if heroClass = @ selectedHero ? . get ( ' heroClass ' )
for slot , item of _ . clone equipment
itemModel = @ items . findWhere original: item
unless itemModel and heroClass in itemModel . classes
console . log ' Unequipping ' , itemModel . get ( ' heroClass ' ) , ' item ' , itemModel . get ( ' name ' ) , ' from slot due to class restrictions. '
@ unequipItemFromSlot @ $el . find ( " .item-slot[data-slot= ' #{ slot } ' ] " )
2014-11-21 15:20:24 -05:00
delete equipment [ slot ]
2014-11-05 23:56:17 -05:00
for slot , item of restrictedGear
equipped = equipment [ slot ]
if equipped and equipped is gear [ restrictedGear [ slot ] ]
console . log ' Unequipping restricted item ' , restrictedGear [ slot ] , ' for ' , slot , ' before level ' , @ options . levelID
@ unequipItemFromSlot @ $el . find ( " .item-slot[data-slot= ' #{ slot } ' ] " )
2014-11-21 15:20:24 -05:00
delete equipment [ slot ]
2014-11-22 01:35:03 -05:00
if ( heroClass is ' Warrior ' or
( heroClass is ' Ranger ' and @ options . levelID in [ ' swift-dagger ' , ' shrapnel ' ] ) or
( heroClass is ' Wizard ' and @ options . levelID in [ ' touch-of-death ' , ' bonemender ' ] ) )
2014-11-11 19:36:44 -05:00
# After they switch to a ranger or wizard, we stop being so finicky about gear.
for slot , item of requiredGear
2014-11-21 19:23:26 -05:00
continue if item is ' tarnished-bronze-breastplate ' and inWorldMap and @ options . levelID is ' the-raised-sword ' # Don't tell them they need it until they need it in the level
2014-11-11 19:36:44 -05:00
equipped = equipment [ slot ]
continue if equipped and not (
2014-11-17 12:20:44 -05:00
( item is ' crude-builders-hammer ' and equipped in [ gear [ ' simple-sword ' ] , gear [ ' long-sword ' ] , gear [ ' sharpened-sword ' ] , gear [ ' roughedge ' ] ] ) or
( item in [ ' simple-sword ' , ' long-sword ' , ' roughedge ' , ' sharpened-sword ' ] and equipped is gear [ ' crude-builders-hammer ' ] ) or
2014-11-11 19:36:44 -05:00
( item is ' leather-boots ' and equipped is gear [ ' simple-boots ' ] ) or
( item is ' simple-boots ' and equipped is gear [ ' leather-boots ' ] )
)
itemModel = @ items . findWhere { slug: item }
continue unless itemModel
availableSlotSelector = " # unequipped .item[data-item-id= ' #{ itemModel . id } ' ] "
@ highlightElement availableSlotSelector , delay: 500 , sides: [ ' right ' ] , rotation: Math . PI / 2
@ $el . find ( availableSlotSelector ) . addClass ' should-equip '
@ $el . find ( " # equipped div[data-slot= ' #{ slot } ' ] " ) . addClass ' should-equip '
@ remainingRequiredEquipment . push slot: slot , item: gear [ item ]
2014-10-09 22:59:57 -04:00
if hadRequired and not @ remainingRequiredEquipment . length
@ endHighlight ( )
2014-11-10 19:09:41 -05:00
@ highlightElement ' # play-level-button ' , duration: 5000
2014-10-09 22:59:57 -04:00
$ ( ' # play-level-button ' ) . prop ( ' disabled ' , @ remainingRequiredEquipment . length > 0 )
2014-09-22 23:44:45 -04:00
2014-11-10 18:24:05 -05:00
setHero: (@selectedHero) ->
2014-11-11 19:36:44 -05:00
if @ selectedHero . loading
@ listenToOnce @ selectedHero , ' sync ' , => @ setHero ? @ selectedHero
return
2014-11-10 21:13:04 -05:00
@ $el . removeClass ( ' Warrior Ranger Wizard ' ) . addClass ( @ selectedHero . get ( ' heroClass ' ) )
2014-11-11 19:36:44 -05:00
@ requireLevelEquipment ( )
2014-11-10 18:24:05 -05:00
@ render ( )
2014-11-26 22:04:47 -05:00
@ onEquipmentChanged ( )
2014-09-21 02:06:28 -04:00
onShown: ->
# Called when we switch tabs to this within the modal
2014-10-09 18:54:18 -04:00
@ requireLevelEquipment ( )
2014-09-21 02:06:28 -04:00
2014-08-14 18:09:10 -04:00
onHidden: ->
2014-09-21 02:06:28 -04:00
# Called when the modal itself is dismissed
2014-10-09 18:54:18 -04:00
@ endHighlight ( )
2014-11-09 19:19:18 -05:00
super ( )
2014-11-05 23:56:17 -05:00
2014-11-06 19:23:23 -05:00
onClickChooseHero: ->
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-06 19:23:23 -05:00
@ hide ( )
@ trigger ' choose-hero-click '
onClickPlayLevel: (e) ->
return if @ $el . find ( ' # play-level-button ' ) . prop ' disabled '
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-06 19:23:23 -05:00
@ showLoading ( )
ua = navigator . userAgent . toLowerCase ( )
unless hasGoneFullScreenOnce or ( /safari/ . test ( ua ) and not /chrome/ . test ( ua ) ) or $ ( window ) . height ( ) >= 658 # Min vertical resolution needed at 1366px wide
@ toggleFullscreen ( )
hasGoneFullScreenOnce = true
@ updateConfig =>
2014-11-13 13:49:37 -05:00
@ trigger ? ' play-click '
2014-11-28 15:05:34 -05:00
window . tracker ? . trackEvent ' Inventory Play ' , category: ' Play Level ' , [ ' Google Analytics ' ]
2014-11-10 19:09:41 -05:00
2014-11-06 19:23:23 -05:00
updateConfig: (callback, skipSessionSave) ->
sessionHeroConfig = @ options . session . get ( ' heroConfig ' ) ? { }
lastHeroConfig = me . get ( ' heroConfig ' ) ? { }
inventory = @ getCurrentEquipmentConfig ( )
patchSession = patchMe = false
patchSession || = not _ . isEqual inventory , sessionHeroConfig . inventory
sessionHeroConfig.inventory = inventory
2014-11-11 19:36:44 -05:00
if hero = @ selectedHero . get ( ' original ' )
patchSession || = not _ . isEqual hero , sessionHeroConfig . thangType
sessionHeroConfig.thangType = hero
patchMe || = not _ . isEqual inventory , lastHeroConfig . inventory
2014-11-06 19:23:23 -05:00
lastHeroConfig.inventory = inventory
if patchMe
console . log ' setting me.heroConfig to ' , JSON . stringify ( lastHeroConfig )
me . set ' heroConfig ' , lastHeroConfig
me . patch ( )
if patchSession
console . log ' setting session.heroConfig to ' , JSON . stringify ( sessionHeroConfig )
@ options . session . set ' heroConfig ' , sessionHeroConfig
@ options . session . patch success: callback unless skipSessionSave
else
callback ? ( )
2014-11-05 23:56:17 -05:00
2014-11-26 09:58:23 -05:00
#- TODO: DRY this between PlayItemsModal and InventoryModal and PlayHeroesModal
2014-11-24 13:51:20 -05:00
onUnlockButtonClicked: (e) ->
e . stopPropagation ( )
button = $ ( e . target ) . closest ( ' button ' )
item = @ items . get ( button . data ( ' item-id ' ) )
affordable = item . affordable
if not affordable
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-24 13:51:20 -05:00
@ askToBuyGems button
else if button . hasClass ( ' confirm ' )
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-unlock-end '
2014-11-24 13:51:20 -05:00
purchase = Purchase . makeFor ( item )
purchase . save ( )
#- set local changes to mimic what should happen on the server...
purchased = me . get ( ' purchased ' ) ? { }
purchased . items ? = [ ]
purchased . items . push ( item . get ( ' original ' ) )
me . set ( ' purchased ' , purchased )
me . set ( ' spent ' , ( me . get ( ' spent ' ) ? 0 ) + item . get ( ' gems ' ) )
#- ...then rerender key bits
@ itemGroups . lockedItems . remove ( item )
# Redo all item sorting to make sure that we don't clobber state changes since last render.
equipped = _ . values @ getCurrentEquipmentConfig ( )
@ sortItem ( item , equipped ) for item in @ items . models
@ renderSelectors ( ' # unequipped ' , ' # gems-count ' )
@ requireLevelEquipment ( )
@ delegateEvents ( )
@ setUpDraggableEventsForAvailableEquipment ( )
@ itemDetailsView . setItem ( item )
Backbone . Mediator . publish ' store:item-purchased ' , item: item , itemSlug: item . get ( ' slug ' )
else
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-unlock-start '
2014-11-24 13:51:20 -05:00
button . addClass ( ' confirm ' ) . text ( $ . i18n . t ( ' play.confirm ' ) )
@ $el . one ' click ' , (e) ->
button . removeClass ( ' confirm ' ) . text ( $ . i18n . t ( ' play.unlock ' ) ) if e . target isnt button [ 0 ]
askToBuyGems: (unlockButton) ->
if me . getGemPromptGroup ( ) is ' no-prompt '
return @ openModalView new BuyGemsModal ( )
@ $el . find ( ' .unlock-button ' ) . popover ' destroy '
popoverTemplate = buyGemsPromptTemplate { }
unlockButton . popover (
animation: true
trigger: ' manual '
placement: ' top '
content: ' ' # template has it
container: @ $el
template: popoverTemplate
) . popover ' show '
popover = unlockButton . data ( ' bs.popover ' )
popover ? . $tip ? . i18n ( )
onBuyGemsPromptButtonClicked: (e) ->
2014-11-26 09:58:23 -05:00
@ playSound ' menu-button-click '
2014-11-24 13:51:20 -05:00
@ openModalView new BuyGemsModal ( )
onClickedSomewhere: (e) ->
return if @ destroyed
@ $el . find ( ' .unlock-button ' ) . popover ' destroy '
2014-11-26 22:04:47 -05:00
#- Paper doll equipment updating
onEquipmentChanged: ->
@ removeDollImages ( )
heroClass = @ selectedHero ? . get ( ' heroClass ' ) ? ' Warrior '
gender = if @ selectedHero ? . get ( ' slug ' ) in heroGenders . male then ' male ' else ' female '
equipment = @ getCurrentEquipmentConfig ( )
slotsWithImages = [ ]
for slot , original of equipment
item = _ . find @ items . models , (item) -> item . get ( ' original ' ) is original
continue unless dollImages = item ? . get ( ' dollImages ' )
didAdd = @ addDollImage slot , dollImages , heroClass , gender
slotsWithImages . push slot if didAdd
2014-11-28 15:11:51 -05:00
@ $el . find ( ' # hero-image, # hero-image-hair, # hero-image-head, # hero-image-thumb ' ) . removeClass ( ) . addClass " #{ gender } #{ heroClass } "
2014-11-26 22:04:47 -05:00
@ $el . find ( ' # hero-image-hair ' ) . toggle not ( ' head ' in slotsWithImages )
@ $el . find ( ' # hero-image-thumb ' ) . toggle not ( ' gloves ' in slotsWithImages )
removeDollImages: ->
@ $el . find ( ' .doll-image ' ) . remove ( )
addDollImage: (slot, dollImages, heroClass, gender) ->
heroClass = @ selectedHero ? . get ( ' heroClass ' ) ? ' Warrior '
gender = if @ selectedHero ? . get ( ' slug ' ) in heroGenders . male then ' male ' else ' female '
didAdd = false
if slot is ' gloves '
if heroClass is ' Ranger '
imageKeys = [ " #{ gender } #{ heroClass } " , " #{ gender } #{ heroClass } Thumb " ]
else
imageKeys = [ " #{ gender } " , " #{ gender } Thumb " ]
else
imageKeys = [ gender ]
for imageKey in imageKeys
imageURL = dollImages [ imageKey ]
if not imageURL
console . log " Hmm, should have #{ slot } #{ imageKey } paper doll image, but don ' t have it. "
else
imageEl = $ ( ' <img> ' ) . attr ( ' src ' , " /file/ #{ imageURL } " ) . addClass ( " doll-image #{ slot } #{ heroClass } #{ gender } #{ _ . string . underscored ( imageKey ) . replace ( /_/g , ' - ' ) } " ) . attr ( ' draggable ' , false )
@ $el . find ( ' # equipped ' ) . append imageEl
didAdd = true
didAdd
2014-11-10 18:24:05 -05:00
destroy: ->
2014-11-24 13:51:20 -05:00
@ $el . find ( ' .unlock-button ' ) . popover ' destroy '
2014-11-10 18:24:05 -05:00
@ stage ? . removeAllChildren ( )
super ( )
2014-11-26 22:04:47 -05:00
heroGenders =
male: [ ' knight ' , ' samurai ' , ' trapper ' , ' potion-master ' ]
female: [ ' captain ' , ' ninja ' , ' forest-archer ' , ' librarian ' , ' sorcerer ' ]
2014-11-10 18:24:05 -05:00
2014-11-05 23:56:17 -05:00
gear =
' simple-boots ' : ' 53e237bf53457600003e3f05 '
' simple-sword ' : ' 53e218d853457600003e3ebe '
2014-11-21 19:23:26 -05:00
' tarnished-bronze-breastplate ' : ' 53e22eac53457600003e3efc '
2014-11-05 23:56:17 -05:00
' leather-boots ' : ' 53e2384453457600003e3f07 '
' leather-belt ' : ' 5437002a7beba4a82024a97d '
' programmaticon-i ' : ' 53e4108204c00d4607a89f78 '
2014-11-22 01:35:03 -05:00
' programmaticon-ii ' : ' 546e25d99df4a17d0d449be1 '
2014-11-05 23:56:17 -05:00
' crude-glasses ' : ' 53e238df53457600003e3f0b '
2014-11-17 12:20:44 -05:00
' crude-builders-hammer ' : ' 53f4e6e3d822c23505b74f42 '
2014-11-08 22:33:00 -05:00
' long-sword ' : ' 544d7d1f8494308424f564a3 '
' sundial-wristwatch ' : ' 53e2396a53457600003e3f0f '
' bronze-shield ' : ' 544c310ae0017993fce214bf '
' wooden-glasses ' : ' 53e2167653457600003e3eb3 '
' basic-flags ' : ' 545bacb41e649a4495f887da '
2014-11-14 14:03:01 -05:00
' roughedge ' : ' 544d7d918494308424f564a7 '
' sharpened-sword ' : ' 544d7deb8494308424f564ab '
2014-11-22 01:35:03 -05:00
' crude-crossbow ' : ' 544d7ffd8494308424f564c3 '
' crude-dagger ' : ' 544d952b8494308424f56517 '
' weak-charge ' : ' 544d957d8494308424f5651f '
' enchanted-stick ' : ' 544d87188494308424f564f1 '
' unholy-tome-i ' : ' 546374bc3839c6e02811d308 '
' book-of-life-i ' : ' 546375653839c6e02811d30b '