2014-11-06 19:23:23 -05:00
ModalView = require ' views/kinds/ModalView '
template = require ' templates/game-menu/inventory-modal '
2014-08-08 14:36:41 -04:00
{ me } = require ' lib/auth '
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 15:47:24 -05:00
LevelOptions = require ' lib/LevelOptions '
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-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
events:
' click .item-slot ' : ' onItemSlotClick '
2014-09-24 23:15:17 -04:00
' click # available-equipment .list-group-item:not(.equipped) ' : ' onAvailableItemClick '
' dblclick # available-equipment .list-group-item:not(.equipped) ' : ' onAvailableItemDoubleClick '
2014-10-27 16:32:25 -04:00
' doubletap # available-equipment .list-group-item:not(.equipped) ' : ' onAvailableItemDoubleClick '
2014-08-13 20:21:37 -04:00
' dblclick .item-slot .item-view ' : ' onEquippedItemDoubleClick '
2014-10-27 16:32:25 -04:00
' doubletap .item-slot .item-view ' : ' onEquippedItemDoubleClick '
2014-11-06 19:23:23 -05:00
' shown.bs.modal ' : ' onShown '
' click # choose-hero-button ' : ' onClickChooseHero '
' click # play-level-button ' : ' onClickPlayLevel '
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-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-05 23:56:17 -05:00
@items.url = ' /db/thang.type?view=items&project=name,slug,components,original,rasterIcon,gems,description,heroClass '
2014-08-13 20:21:37 -04:00
@ supermodel . loadCollection ( @ items , ' items ' )
2014-11-10 15:47:24 -05:00
@equipment = { } # Assign for real when we have loaded the session
2014-08-14 18:09:10 -04:00
2014-09-21 02:06:28 -04:00
destroy: ->
@ stage ? . removeAllChildren ( )
super ( )
2014-08-13 20:21:37 -04:00
onLoaded: ->
2014-09-22 01:10:52 -04:00
item.notInLevel = true for item in @ items . models
2014-11-10 15:47:24 -05:00
@equipment = @ options . equipment or @ options . session ? . get ( ' heroConfig ' ) ? . inventory or me . get ( ' heroConfig ' ) ? . inventory or { }
@equipment = $ . extend true , { } , @ equipment
@ requireLevelEquipment ( )
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-08-13 20:21:37 -04:00
for item in @ items . models
item.classes = item . getAllowedSlots ( )
item . classes . push ' equipped ' if item . get ( ' original ' ) in context . equipped
2014-11-10 15:47:24 -05:00
locked = not ( item . get ( ' original ' ) in me . items ( ) )
2014-10-30 16:07:04 -04:00
item . classes . push ' locked ' if locked and item . get ( ' slug ' ) isnt ' simple-boots '
for heroClass in item . getAllowedHeroClasses ( )
item . classes . push heroClass
item . classes . push ' silhouette ' if item . isSilhouettedItem ( )
2014-11-10 15:47:24 -05:00
item . classes . push ' restricted ' if item . get ( ' slug ' ) in _ . values ( LevelOptions [ @ options . levelID ] ? . restrictedGear ? { } )
2014-10-30 16:07:04 -04:00
@ items . models . sort (a, b) ->
lockScore = 90019001 * ( ( ' locked ' in a . classes ) - ( ' locked ' in b . classes ) )
gemScore = a . get ( ' gems ' ) - b . get ( ' gems ' )
lockScore + gemScore
context.unlockedItems = [ ]
context.lockedItems = [ ]
for item in @ items . models
( if ' locked ' in item . classes then context . lockedItems else context . unlockedItems ) . push item
2014-08-13 20:21:37 -04:00
context.slots = @ slots
context.equipment = _ . clone @ equipment
for slot , itemOriginal of context . equipment
item = _ . find @ items . models , (item) -> item . get ( ' original ' ) is itemOriginal
context . equipment [ slot ] = item
2014-08-08 14:36:41 -04:00
context
afterRender: ->
super ( )
2014-11-10 10:49:23 -05:00
@ $el . find ( ' .modal-footer button ' ) . css ( ' visibility ' , ' hidden ' )
2014-08-13 20:21:37 -04:00
return unless @ supermodel . finished ( )
2014-11-10 10:49:23 -05:00
@ $el . find ( ' .modal-footer button ' ) . css ( ' visibility ' , ' visible ' )
2014-08-14 18:09:10 -04:00
2014-09-21 14:24:37 -04:00
keys = ( item . get ( ' original ' ) for item in @ items . models )
2014-08-13 20:21:37 -04:00
itemMap = _ . zipObject keys , @ items . models
# Fill in equipped items
for slottedItemStub in @ $el . find ( ' .replace-me ' )
itemID = $ ( slottedItemStub ) . data ( ' item-id ' )
item = itemMap [ itemID ]
2014-09-21 02:06:28 -04:00
itemView = new ItemView ( { item: item , includes: { } } )
2014-08-13 20:21:37 -04:00
itemView . render ( )
$ ( slottedItemStub ) . replaceWith ( itemView . $el )
@ registerSubView ( itemView )
for availableItemEl in @ $el . find ( ' # available-equipment .list-group-item ' )
itemID = $ ( availableItemEl ) . data ( ' item-id ' )
item = itemMap [ itemID ]
2014-08-14 18:09:10 -04:00
itemView = new ItemView ( { item: item , includes: { name: true } } )
2014-08-13 20:21:37 -04:00
itemView . render ( )
$ ( availableItemEl ) . append ( itemView . $el )
@ registerSubView ( itemView )
2014-11-05 23:56:17 -05:00
continue if $ ( availableItemEl ) . hasClass ( ' locked ' ) or $ ( availableItemEl ) . hasClass ( ' restricted ' )
2014-09-21 14:24:37 -04:00
dragHelper = itemView . $el . find ( ' img ' ) . clone ( ) . addClass ( ' draggable-item ' )
2014-09-21 02:06:28 -04:00
do (dragHelper, itemView) =>
itemView . $el . draggable
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 15:47:24 -05:00
zIndex: 10000
2014-09-21 02:06:28 -04:00
itemView . $el . on ' dragstart ' , =>
2014-09-21 14:24:37 -04:00
@ onAvailableItemClick target: itemView . $el . parent ( ) unless itemView . $el . parent ( ) . hasClass ' active '
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
drop: (e, ui) => @ onAvailableItemDoubleClick ( )
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-10-27 14:17:34 -04:00
@ $el . find ( ' .hero-container ' ) . droppable
drop: (e, ui) => @ onAvailableItemDoubleClick ( )
accept: (el) -> true
activeClass: ' droppable '
hoverClass: ' droppable-hover '
tolerance: ' pointer '
2014-09-21 12:53:58 -04:00
@ $el . find ( ' # selected-items ' ) . hide ( ) # Hide until one is selected
2014-08-13 20:21:37 -04:00
@ delegateEvents ( )
2014-08-14 18:09:10 -04:00
2014-10-02 14:18:43 -04:00
if @ selectedHero and not @ startedLoadingFirstHero
@ loadHero ( )
2014-10-09 18:54:18 -04:00
@ requireLevelEquipment ( )
2014-10-02 14:18:43 -04:00
2014-09-21 02:06:28 -04:00
afterInsert: ->
super ( )
@canvasWidth = @ $el . find ( ' canvas ' ) . innerWidth ( )
@canvasHeight = @ $el . find ( ' canvas ' ) . innerHeight ( )
2014-10-09 22:59:57 -04:00
@inserted = true
2014-09-21 02:06:28 -04:00
2014-10-09 18:54:18 -04:00
makeEquippedSlotDraggable: (slot) ->
unequip = => @ unequipItemFromSlot slot
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 15:47:24 -05:00
zIndex: 10000
2014-10-09 18:54:18 -04:00
2014-08-13 20:21:37 -04:00
clearSelection: ->
2014-09-21 02:06:28 -04:00
@ $el . find ( ' .item-slot.selected ' ) . removeClass ' selected '
2014-08-13 20:21:37 -04:00
@ $el . find ( ' .list-group-item ' ) . removeClass ( ' active ' )
@ onSelectionChanged ( )
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-09-21 02:06:28 -04:00
slot = $ ( e . target ) . closest ( ' .item-slot ' )
wasActive = slot . hasClass ( ' selected ' )
2014-08-14 16:41:32 -04:00
@ unselectAllSlots ( )
@ unselectAllAvailableEquipment ( ) if slot . hasClass ( ' disabled ' )
2014-09-21 12:53:58 -04:00
if wasActive
@ hideSelectedSlotItem ( )
2014-09-21 14:24:37 -04:00
@ unselectAllAvailableEquipment ( )
2014-09-21 12:53:58 -04:00
else
@ selectSlot ( slot )
2014-08-13 20:21:37 -04:00
@ onSelectionChanged ( )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
onAvailableItemClick: (e) ->
2014-08-14 16:41:32 -04:00
itemContainer = $ ( e . target ) . closest ( ' .list-group-item ' )
2014-11-05 23:56:17 -05:00
return if itemContainer . hasClass ( ' locked ' ) or itemContainer . hasClass ( ' restricted ' )
2014-09-21 12:53:58 -04:00
wasActive = itemContainer . hasClass ' active '
2014-08-14 16:41:32 -04:00
@ unselectAllAvailableEquipment ( )
2014-09-21 12:53:58 -04:00
@ selectAvailableItem ( itemContainer ) unless wasActive
2014-08-13 20:21:37 -04:00
@ onSelectionChanged ( )
2014-08-14 16:41:32 -04:00
onAvailableItemDoubleClick: (e) ->
2014-09-22 23:44:45 -04:00
if e
itemContainer = $ ( e . target ) . closest ( ' .list-group-item ' )
2014-11-05 23:56:17 -05:00
return if itemContainer . hasClass ( ' locked ' ) or itemContainer . hasClass ( ' restricted ' )
2014-09-22 23:44:45 -04:00
@ selectAvailableItem itemContainer
2014-09-21 23:19:27 -04:00
@ onSelectionChanged ( )
2014-08-14 16:41:32 -04:00
slot = @ getSelectedSlot ( )
2014-09-21 02:06:28 -04:00
slot = @ $el . find ( ' .item-slot:not(.disabled):first ' ) if not slot . length
2014-10-30 16:07:04 -04:00
$ ( e . target ) . effect ( ' transfer ' , to: slot , duration: 500 , easing: ' easeOutCubic ' ) if e
2014-08-13 20:21:37 -04:00
@ unequipItemFromSlot ( slot )
@ equipSelectedItemToSlot ( slot )
@ onSelectionChanged ( )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
onEquippedItemDoubleClick: (e) ->
2014-08-14 16:41:32 -04:00
@ unselectAllAvailableEquipment ( )
2014-08-13 20:21:37 -04:00
slot = $ ( e . target ) . closest ( ' .item-slot ' )
2014-08-14 16:41:32 -04:00
@ selectAvailableItem ( @ unequipItemFromSlot ( slot ) )
@ onSelectionChanged ( )
2014-08-14 18:09:10 -04:00
getSelectedSlot: ->
2014-09-21 02:06:28 -04:00
@ $el . find ( ' # equipped .item-slot.selected ' )
2014-08-14 18:09:10 -04:00
unselectAllAvailableEquipment: ->
2014-08-14 16:41:32 -04:00
@ $el . find ( ' # available-equipment .list-group-item ' ) . removeClass ( ' active ' )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
unselectAllSlots: ->
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-08-14 16:41:32 -04:00
selectSlot: (slot) ->
2014-09-21 02:06:28 -04:00
slot . addClass ( ' selected ' )
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-08-14 16:41:32 -04:00
getSelectedAvailableItemContainer: ->
@ $el . find ( ' # available-equipment .list-group-item.active ' )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
getAvailableItemContainer: (itemID) ->
@ $el . find ( " # available-equipment .list-group-item[data-item-id= ' #{ itemID } ' ] " )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
selectAvailableItem: (itemContainer) ->
itemContainer ? . addClass ( ' active ' )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
unequipItemFromSlot: (slot) ->
itemIDToUnequip = slot . find ( ' .item-view ' ) . data ( ' item-id ' )
return unless itemIDToUnequip
slot . find ( ' .item-view ' ) . detach ( )
for el in @ $el . find ( ' # available-equipment .list-group-item ' )
itemID = $ ( el ) . find ( ' .item-view ' ) . data ( ' item-id ' )
if itemID is itemIDToUnequip
2014-10-09 18:54:18 -04:00
unequipped = $ ( el ) . removeClass ( ' equipped ' )
break
2014-11-05 17:17:11 -05:00
if unequipped
@ clearSelection ( )
@ requireLevelEquipment ( )
2014-10-09 18:54:18 -04:00
return unequipped
2014-08-13 20:21:37 -04:00
equipSelectedItemToSlot: (slot) ->
2014-08-14 16:41:32 -04:00
selectedItemContainer = @ getSelectedAvailableItemContainer ( )
2014-08-13 20:21:37 -04:00
newItemHTML = selectedItemContainer . html ( )
2014-09-21 02:06:28 -04:00
selectedItemContainer . addClass ( ' equipped ' )
slotContainer = slot . find ( ' .item-container ' )
2014-08-14 16:41:32 -04:00
slotContainer . html ( newItemHTML )
slotContainer . find ( ' .item-view ' ) . data ( ' item-id ' , selectedItemContainer . find ( ' .item-view ' ) . data ( ' item-id ' ) )
2014-08-13 20:21:37 -04:00
@ $el . find ( ' .list-group-item ' ) . removeClass ( ' active ' )
2014-10-09 18:54:18 -04:00
@ makeEquippedSlotDraggable slot
@ requireLevelEquipment ( )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
onSelectionChanged: ->
@ $el . find ( ' .item-slot ' ) . show ( )
2014-08-14 18:09:10 -04:00
2014-09-21 02:06:28 -04:00
selectedSlot = @ $el . find ( ' .item-slot.selected ' )
2014-08-13 20:21:37 -04:00
selectedItem = @ $el . find ( ' # available-equipment .list-group-item.active ' )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
if selectedSlot . length
@ $el . find ( ' # available-equipment .list-group-item ' ) . hide ( )
2014-09-24 23:15:17 -04:00
unlockedCount = @ $el . find ( " # available-equipment .list-group-item. #{ selectedSlot . data ( ' slot ' ) } :not(.locked) " ) . show ( ) . length
lockedCount = @ $el . find ( " # available-equipment .list-group-item. #{ selectedSlot . data ( ' slot ' ) } .locked " ) . show ( ) . length
@ $el . find ( ' # unlocked-description ' ) . text ( " #{ unlockedCount } #{ selectedSlot . data ( ' slot ' ) } items owned " ) . toggle unlockedCount > 0
@ $el . find ( ' # locked-description ' ) . text ( " #{ lockedCount } #{ selectedSlot . data ( ' slot ' ) } items locked " ) . toggle lockedCount > 0
2014-08-13 20:21:37 -04:00
selectedSlotItemID = selectedSlot . find ( ' .item-view ' ) . data ( ' item-id ' )
if selectedSlotItemID
2014-09-24 23:15:17 -04:00
item = _ . find @ items . models , { id: selectedSlotItemID }
2014-08-14 16:41:32 -04:00
@ showSelectedSlotItem ( item )
2014-08-13 20:21:37 -04:00
else
2014-08-14 16:41:32 -04:00
@ hideSelectedSlotItem ( )
2014-08-13 20:21:37 -04:00
else
2014-09-24 23:15:17 -04:00
unlockedCount = @ $el . find ( ' # available-equipment .list-group-item:not(.locked) ' ) . show ( ) . length
2014-10-30 16:07:04 -04:00
@ $el . find ( ' # available-equipment .list-group-item.locked ' ) . hide ( )
2014-09-24 23:15:17 -04:00
@ $el . find ( ' # unlocked-description ' ) . text ( " #{ unlockedCount } items owned " ) . toggle unlockedCount > 0
2014-10-30 16:07:04 -04:00
@ $el . find ( ' # locked-description ' ) . text ( " #{ lockedCount } items locked " ) . hide ( )
2014-09-24 23:15:17 -04:00
#@$el.find('#available-equipment .list-group-item.equipped').hide()
2014-08-13 20:21:37 -04:00
@ $el . find ( ' .item-slot ' ) . removeClass ( ' disabled ' )
if selectedItem . length
item = _ . find @ items . models , { id : selectedItem . find ( ' .item-view ' ) . data ( ' item-id ' ) }
# update which slots are enabled
allowedSlots = item . getAllowedSlots ( )
for slotEl in @ $el . find ( ' .item-slot ' )
slotName = $ ( slotEl ) . data ( ' slot ' )
if slotName not in allowedSlots
$ ( slotEl ) . addClass ( ' disabled ' )
2014-08-14 16:41:32 -04:00
@ showSelectedAvailableItem ( item )
2014-08-13 20:21:37 -04:00
else
2014-08-14 16:41:32 -04:00
@ hideSelectedAvailableItem ( )
2014-08-14 18:09:10 -04:00
2014-08-13 20:21:37 -04:00
@ delegateEvents ( )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
showSelectedSlotItem: (item) ->
if not @ selectedEquippedItemView
@selectedEquippedItemView = new ItemView ( {
2014-08-31 15:21:25 -04:00
item: item , includes: { name: true , stats: true , props: true } } )
2014-08-14 16:41:32 -04:00
@ insertSubView ( @ selectedEquippedItemView , @ $el . find ( ' # selected-equipped-item .item-view-stub ' ) )
else
@ selectedEquippedItemView . $el . show ( )
@selectedEquippedItemView.item = item
@ selectedEquippedItemView . render ( )
2014-09-21 12:53:58 -04:00
@ $el . find ( ' # selected-items ' ) . show ( )
2014-09-24 23:15:17 -04:00
@ $el . find ( ' # selected-equipped-item ' ) . show ( )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
hideSelectedSlotItem: ->
2014-09-24 23:15:17 -04:00
@ selectedEquippedItemView ? . $el . hide ( ) . parent ( ) . hide ( )
2014-09-21 12:53:58 -04:00
@ $el . find ( ' # selected-items ' ) . hide ( ) unless @ selectedEquippedItemView ? . $el ? . is ( ' :visible ' )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
showSelectedAvailableItem: (item) ->
if not @ selectedAvailableItemView
@selectedAvailableItemView = new ItemView ( {
2014-08-31 15:21:25 -04:00
item: item , includes: { name: true , stats: true , props: true } } )
2014-08-14 16:41:32 -04:00
@ insertSubView ( @ selectedAvailableItemView , @ $el . find ( ' # selected-available-item .item-view-stub ' ) )
else
@ selectedAvailableItemView . $el . show ( )
@selectedAvailableItemView.item = item
@ selectedAvailableItemView . render ( )
2014-09-21 12:53:58 -04:00
@ $el . find ( ' # selected-items ' ) . show ( )
2014-09-24 23:15:17 -04:00
@ $el . find ( ' # selected-available-item ' ) . show ( )
2014-08-14 18:09:10 -04:00
2014-08-14 16:41:32 -04:00
hideSelectedAvailableItem: ->
2014-09-24 23:15:17 -04:00
@ selectedAvailableItemView ? . $el . hide ( ) . parent ( ) . hide ( )
2014-09-21 12:53:58 -04:00
@ $el . find ( ' # selected-items ' ) . hide ( ) unless @ selectedEquippedItemView ? . $el ? . is ( ' :visible ' )
2014-08-13 20:21:37 -04:00
getCurrentEquipmentConfig: ->
config = { }
for slot in @ $el . find ( ' .item-slot ' )
slotName = $ ( slot ) . data ( ' slot ' )
slotItemID = $ ( slot ) . find ( ' .item-view ' ) . data ( ' item-id ' )
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 15:47:24 -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-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-10 15:47:24 -05:00
for slot , item of requiredGear
#continue if item is 'leather-tunic' and inWorldMap and @options.levelID is 'the-raised-sword' # Don't tell them they need it until they need it in the level # ... when we make it so that you can buy it
2014-11-05 23:56:17 -05:00
equipped = equipment [ slot ]
2014-11-10 15:47:24 -05:00
continue if equipped and not (
( item is ' builders-hammer ' and equipped in [ gear [ ' simple-sword ' ] , gear [ ' long-sword ' ] ] ) or
( item in [ ' simple-sword ' , ' long-sword ' ] and equipped is gear [ ' builders-hammer ' ] ) or
( item is ' leather-boots ' and equipped is gear [ ' simple-boots ' ] ) or
( item is ' simple-boots ' and equipped is gear [ ' leather-boots ' ] )
)
2014-10-09 22:59:57 -04:00
availableSlotSelector = " # available-equipment li[data-item-id= ' #{ gear [ item ] } ' ] "
@ 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 ]
if hadRequired and not @ remainingRequiredEquipment . length
@ endHighlight ( )
2014-11-10 15:47:24 -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-06 19:23:23 -05:00
setHero: (@selectedHero) ->
2014-09-21 02:06:28 -04:00
@ loadHero ( )
2014-10-30 16:07:04 -04:00
@ $el . removeClass ( ' Warrior Ranger Wizard ' ) . addClass ( @ selectedHero . get ( ' heroClass ' ) )
2014-09-21 02:06:28 -04:00
loadHero: ->
2014-10-02 14:18:43 -04:00
return unless @ supermodel . finished ( ) and @ selectedHero and not @ $el . hasClass ' secret '
@startedLoadingFirstHero = true
2014-09-21 02:06:28 -04:00
@ stage ? . removeAllChildren ( )
2014-10-14 12:54:18 -04:00
if featureImage = @ selectedHero . get ' featureImage '
@ $el . find ( " .equipped-hero-canvas " ) . hide ( )
@ $el . find ( " .hero-feature-image " ) . show ( ) . find ( ' img ' ) . prop ( ' src ' , ' /file/ ' + featureImage )
return
2014-09-21 02:06:28 -04:00
if @ selectedHero . loaded and movieClip = @ movieClips ? [ @ selectedHero . get ( ' original ' ) ]
@ stage . addChild ( movieClip )
@ stage . update ( )
return
onLoaded = =>
2014-10-14 12:54:18 -04:00
return unless canvas = @ $el . find ( " .equipped-hero-canvas " )
2014-09-21 02:06:28 -04:00
@ canvasWidth || = canvas . width ( )
@ canvasHeight || = canvas . height ( )
canvas . prop width: @ canvasWidth , height: @ canvasHeight
builder = new SpriteBuilder ( @ selectedHero )
movieClip = builder . buildMovieClip ( @ selectedHero . get ( ' actions ' ) . attack ? . animation ? @ selectedHero . get ( ' actions ' ) . idle . animation )
movieClip.scaleX = movieClip.scaleY = canvas . prop ( ' height ' ) / 120 # Average hero height is ~110px at normal resolution
if @ selectedHero . get ( ' name ' ) in [ ' Knight ' , ' Robot Walker ' ] # These are too big, so shrink them.
movieClip . scaleX *= 0.7
movieClip . scaleY *= 0.7
movieClip.regX = - @ selectedHero . get ( ' positions ' ) . registration . x
movieClip.regY = - @ selectedHero . get ( ' positions ' ) . registration . y
movieClip.x = canvas . prop ( ' width ' ) * 0.5
movieClip.y = canvas . prop ( ' height ' ) * 0.95 # This is where the feet go.
movieClip . gotoAndPlay 0
@ stage ? = new createjs . Stage ( canvas [ 0 ] )
@ stage . addChild movieClip
@ stage . update ( )
@ movieClips ? = { }
@ movieClips [ @ selectedHero . get ( ' original ' ) ] = movieClip
if @ selectedHero . loaded
if @ selectedHero . isFullyLoaded ( )
_ . defer onLoaded
else
console . error ' Hmm, trying to render a hero we have not loaded...? ' , @ selectedHero
else
@ listenToOnce @ selectedHero , ' sync ' , onLoaded
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
@ loadHero ( )
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: ->
@ hide ( )
@ trigger ' choose-hero-click '
onClickPlayLevel: (e) ->
return if @ $el . find ( ' # play-level-button ' ) . prop ' disabled '
@ 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 =>
@ trigger ' play-click '
window . tracker ? . trackEvent ' Play Level Modal ' , Action: ' Play '
updateConfig: (callback, skipSessionSave) ->
sessionHeroConfig = @ options . session . get ( ' heroConfig ' ) ? { }
lastHeroConfig = me . get ( ' heroConfig ' ) ? { }
inventory = @ getCurrentEquipmentConfig ( )
patchSession = patchMe = false
patchSession || = not _ . isEqual inventory , sessionHeroConfig . inventory
patchMe || = not _ . isEqual inventory , lastHeroConfig . inventory
sessionHeroConfig.inventory = inventory
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
gear =
' simple-boots ' : ' 53e237bf53457600003e3f05 '
' simple-sword ' : ' 53e218d853457600003e3ebe '
' leather-tunic ' : ' 53e22eac53457600003e3efc '
' leather-boots ' : ' 53e2384453457600003e3f07 '
' leather-belt ' : ' 5437002a7beba4a82024a97d '
' programmaticon-i ' : ' 53e4108204c00d4607a89f78 '
' crude-glasses ' : ' 53e238df53457600003e3f0b '
' 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 '