From 706ab7a2516371710dfbfc2ad3737747728e77ca Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Sun, 10 Aug 2014 22:09:13 -0700 Subject: [PATCH] Trying to get item ThangType nodes and inventory loading working. --- app/lib/LevelLoader.coffee | 17 ++++++++++------- app/lib/surface/SpriteBoss.coffee | 1 + app/lib/world/world.coffee | 1 + app/models/Level.coffee | 8 ++++---- .../component/ThangComponentConfigView.coffee | 7 ++++--- app/views/editor/level/treema_nodes.coffee | 11 ++++++++++- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/app/lib/LevelLoader.coffee b/app/lib/LevelLoader.coffee index df5989c1c..38ec587af 100644 --- a/app/lib/LevelLoader.coffee +++ b/app/lib/LevelLoader.coffee @@ -73,13 +73,13 @@ module.exports = class LevelLoader extends CocoClass @opponentSessionResource = @supermodel.loadModel(opponentSession, 'opponent_session') @opponentSession = @opponentSessionResource.model @listenToOnce @opponentSession, 'sync', @onSessionLoaded - + onSessionLoaded: (session) -> session.url = -> '/db/level.session/' + @id if heroConfig = session.get('heroConfig') url = "/db/thang.type/#{heroConfig.thangType}/version?project=name,components" @worldNecessities.push @maybeLoadURL(url, ThangType, 'thang') - + for itemThangType in _.values(heroConfig.inventory) url = "/db/thang.type/#{itemThangType}/version?project=name,components" @worldNecessities.push @maybeLoadURL(url, ThangType, 'thang') @@ -148,21 +148,24 @@ module.exports = class LevelLoader extends CocoClass @supermodel.loadModel wizard, 'thang' @worldNecessities = @worldNecessities.concat worldNecessities - + loadItemThangsEquippedByLevelThang: (levelThang) -> return unless levelThang.components for component in levelThang.components if component.original is LevelComponent.EquipsID and inventory = component.config?.inventory for itemThangType in _.values(inventory) + unless itemThangType + console.warn "Empty item in inventory for", levelThang + continue url = "/db/thang.type/#{itemThangType}/version?project=name,components" @worldNecessities.push @maybeLoadURL(url, ThangType, 'thang') - + onThangNamesLoaded: (thangNames) -> if @level.get('type') is 'hero' for thangType in thangNames.models @loadDefaultComponentsForThangType(thangType) @loadEquippedItemsInheritedFromThangType(thangType) - + loadDefaultComponentsForThangType: (thangType) -> return unless components = thangType.get('components') for component in components @@ -183,10 +186,10 @@ module.exports = class LevelLoader extends CocoClass onWorldNecessityLoaded: (resource) -> index = @worldNecessities.indexOf(resource) - if (@level.loading or (@level.get('type') is 'hero')) and resource.name is 'thang' + if (@level?.loading or (@level?.get('type') is 'hero')) and resource.name is 'thang' @loadDefaultComponentsForThangType(resource.model) @loadEquippedItemsInheritedFromThangType(resource.model) - + return unless index >= 0 @worldNecessities.splice(index, 1) @worldNecessities = (r for r in @worldNecessities when r?) diff --git a/app/lib/surface/SpriteBoss.coffee b/app/lib/surface/SpriteBoss.coffee index 634d5ea2d..b9cf6278e 100644 --- a/app/lib/surface/SpriteBoss.coffee +++ b/app/lib/surface/SpriteBoss.coffee @@ -149,6 +149,7 @@ module.exports = class SpriteBoss extends CocoClass return false unless m.get('actions') or m.get('raster') return m.get('name') is thang.spriteName thangType ?= _.find @options.thangTypes, (m) -> return m.get('name') is thang.spriteName + return console.error "Couldn't find ThangType for", thang unless thangType options = @createSpriteOptions thang: thang options.resolutionFactor = if thangType.get('kind') is 'Floor' then 2 else SPRITE_RESOLUTION_FACTOR diff --git a/app/lib/world/world.coffee b/app/lib/world/world.coffee index ca61daddb..f87df6fe0 100644 --- a/app/lib/world/world.coffee +++ b/app/lib/world/world.coffee @@ -143,6 +143,7 @@ module.exports = class World @aborted = true loadFromLevel: (level, willSimulate=true) -> + @level = level @loadSystemsFromLevel level @loadThangsFromLevel level, willSimulate @loadScriptsFromLevel level diff --git a/app/models/Level.coffee b/app/models/Level.coffee index d458031ce..4d1aebc5d 100644 --- a/app/models/Level.coffee +++ b/app/models/Level.coffee @@ -22,9 +22,9 @@ module.exports = class Level extends CocoModel o.systems = @sortSystems o.systems, systemModels @fillInDefaultSystemConfiguration o.systems - o.thangTypes = (original: tt.get('original'), name: tt.get('name') for tt in supermodel.getModels ThangType) + o.thangTypes = (original: tt.get('original'), name: tt.get('name'), components: tt.get('components') for tt in supermodel.getModels ThangType) o - + denormalize: (supermodel) -> o = $.extend true, {}, @attributes for levelThang in o.thangs @@ -37,13 +37,13 @@ module.exports = class Level extends CocoModel configs = {} for thangComponent in levelThang.components configs[thangComponent.original] = thangComponent - + for defaultThangComponent in thangType.get('components') if levelThangComponent = configs[defaultThangComponent.original] # take the thang type default components and merge level-specific component config into it copy = $.extend true, {}, defaultThangComponent.config levelThangComponent.config = _.merge copy, levelThangComponent.config - + else # just add the component as is levelThang.components.push $.extend true, {}, defaultThangComponent diff --git a/app/views/editor/component/ThangComponentConfigView.coffee b/app/views/editor/component/ThangComponentConfigView.coffee index 68ba90601..bd8be6117 100644 --- a/app/views/editor/component/ThangComponentConfigView.coffee +++ b/app/views/editor/component/ThangComponentConfigView.coffee @@ -9,7 +9,7 @@ module.exports = class ThangComponentConfigView extends CocoView className: 'thang-component-config-view' template: template changed: false - + events: 'click .treema-shortened': -> console.log 'clicked treema root' @@ -20,7 +20,7 @@ module.exports = class ThangComponentConfigView extends CocoView @world = options.world @level = options.level @callback = options.callback - + getRenderData: (context={}) -> context = super(context) context.component = @component.attributes @@ -59,11 +59,12 @@ module.exports = class ThangComponentConfigView extends CocoView 'seconds': nodes.SecondsNode 'speed': nodes.SpeedNode 'acceleration': nodes.AccelerationNode + 'item-thang-type': nodes.ItemThangTypeNode @editThangTreema = @$el.find('.treema').treema treemaOptions @editThangTreema.build() @editThangTreema.open(2) - if _.isEqual(@config, {}) and not @editThangTreema.canAddChild() + if _.isEqual(@editThangTreema.data, {}) and not @editThangTreema.canAddChild() @$el.find('.panel-body').hide() onConfigEdited: => diff --git a/app/views/editor/level/treema_nodes.coffee b/app/views/editor/level/treema_nodes.coffee index 92a40cdb8..5baac355a 100644 --- a/app/views/editor/level/treema_nodes.coffee +++ b/app/views/editor/level/treema_nodes.coffee @@ -200,7 +200,7 @@ module.exports.ThangTypeNode = class ThangTypeNode extends TreemaNode.nodeMap.st constructor: (args...) -> super args... @thangType = _.find @settings.supermodel.getModels(ThangType), (m) => m.get('original') is @data if @data - console.log 'ThangTypeNode found ThangType', @thangType, 'for data', @data + #console.log 'ThangTypeNode found ThangType', @thangType, 'for data', @data buildValueForDisplay: (valEl) -> @buildValueForDisplaySimply(valEl, @thangType?.get('name') or 'None') @@ -219,3 +219,12 @@ module.exports.ThangTypeNode = class ThangTypeNode extends TreemaNode.nodeMap.st @data = @thangType.get('original') else @data = null + +module.exports.ItemThangTypeNode = class ThangTypeNode extends ThangTypeNode + valueClass: 'treema-item-thang-type' + buildValueForEditing: (valEl) -> + super(valEl) + thangTypeNames = (m.get('name') for m in @settings.supermodel.getModels ThangType when m.get('kind') is 'Item') + input = valEl.find('input').autocomplete(source: thangTypeNames, minLength: 0, delay: 0, autoFocus: true) + input.val(@thangType?.get('name') or 'None') + valEl