Add ability to craft multipl items at once

* Remove the old crafting table view
* Complete UI/UX of the inventory view
This commit is contained in:
Andrew Miner
2015-01-07 08:31:53 -08:00
parent a1a7ee5dc9
commit f51082a1cb
10 changed files with 72 additions and 37 deletions
+1 -1
View File
@@ -180,7 +180,7 @@ label {
.view__header { .view__header {
background: $color-brick-light url('/images/stone_brick_light.png'); background: $color-brick-light url('/images/stone_brick_light.png');
position: relative; height: 10em; position: relative; height: 10em;
margin: 5em 0 2em 0; margin-top: 5em;
h1 { position: relative; top: -3em; left: -3em; } h1 { position: relative; top: -3em; left: -3em; }
+3 -1
View File
@@ -6,8 +6,10 @@ All rights reserved.
*/ */
.view__item_page { .view__item_page {
& > div { & > div {
display: inline-block; display: inline-block;
margin-top: 2em;
padding: 0 2em; padding: 0 2em;
position: relative; position: relative;
vertical-align: top; vertical-align: top;
@@ -15,5 +17,5 @@ All rights reserved.
.view__inventory { width: 50%; height: 26em; } .view__inventory { width: 50%; height: 26em; }
.view__crafting_table, .view__mod_pack { margin-top: 2em; width: 100%; } .view__mod_pack { margin-top: 2em; width: 100%; }
} }
Binary file not shown.

After

Width:  |  Height:  |  Size: 317 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 489 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 B

@@ -23,23 +23,39 @@ module.exports = class ItemPageController extends BaseController
# Public Methods ############################################################################### # Public Methods ###############################################################################
setParams: (params)-> setParams: (params)->
@model.table.name = params.name if params.name? return unless params.name?
@model.table.quantity = params.quantity if params.quantity?
item = @model.modPack.findItemByName params.name
return unless item? and item.isCraftable
quantity = if params.quantity? then parseInt(params.quantity) else 1
@model.want.add item.slug, quantity
# BaseController Overrides ##################################################################### # BaseController Overrides #####################################################################
onDidRender: -> onDidRender: ->
options = model:@model.table.have, modPack:@model.modPack, editable:true options =
@haveController = @addChild InventoryController, '.view__inventory.have', options editable: true,
model: @model.plan.have,
modPack: @model.modPack,
title: 'Items you have'
@haveController = @addChild InventoryController, '.have', options
options =
editable: true
icon: '/images/fishing_rod.png',
model: @model.plan.want,
modPack: @model.modPack,
title: 'Items you want'
@wantController = @addChild InventoryController, '.want', options
options = options =
editable: false editable: false
icon: '/images/workbench_top.png', icon: '/images/boots.png',
model: @model.table.want, model: @model.plan.need,
modPack: @model.modPack, modPack: @model.modPack,
title: 'Items to Craft' title: "Items you'll need"
@wantController = @addChild InventoryController, '.view__inventory.want', options @needController = @addChild InventoryController, '.need', options
@modPackController = @addChild ModPackController, '.view__mod_pack', model:@model.modPack @modPackController = @addChild ModPackController, '.view__mod_pack', model:@model.modPack
@tableController = @addChild CraftingTableController, '.view__crafting_table', model:@model.table
super super
+36 -21
View File
@@ -14,41 +14,57 @@ module.exports = class CraftingPlan
constructor: (@modPack, includingTools)-> constructor: (@modPack, includingTools)->
if not @modPack then throw new Error 'modPack is required' if not @modPack then throw new Error 'modPack is required'
@includingTools = if includingTools then true else false @includingTools = if includingTools then true else false
@have = new Inventory
@want = new Inventory
@need = new Inventory
@result = new Inventory
@have.on 'change', => @craft()
@want.on 'change', => @craft()
@clear() @clear()
# Public Methods ############################################################################### # Public Methods ###############################################################################
clear: -> clear: ->
@make = new Inventory @steps = []
@need = new Inventory @_expected = new Inventory
@result = new Inventory @_pending = null
@steps = []
@_expected = new Inventory @need.clear()
@_pending = null @result.clear()
return this return this
craft: (name, quantity=1, have=null)-> craft: ->
logger.trace "craft(#{name}, #{quantity}, #{have})" logger.trace "starting crafting plan for: #{@want} starting with #{@have}"
item = @modPack.findItemByName name
if not item? then throw new Error "cannot find an item named: #{name}"
@clear() @clear()
@result.addInventory(have) if have? return if @want.isEmpty
@_expected.add item.slug, quantity @want.each (stack)=>
@_pending = @_expected.clone() @_expected = @want.clone()
@_pending = @want.clone()
@result.addInventory @have
@_need = new Inventory
while not @_pending.isEmpty while not @_pending.isEmpty
@_processPending() @_processPending()
@need.addInventory @_need
@steps.reverse() @steps.reverse()
# Object Overrides ############################################################################# # Object Overrides #############################################################################
toString: -> toString: ->
return "#{@constructor.name} { need:#{@need}, make:#{@make}, result:#{@result} }" return "#{@constructor.name} {
have:#{@have},
want:#{@want},
need:#{@_need},
result:#{@result},
steps:#{@steps}
}"
# Private Methods ############################################################################## # Private Methods ##############################################################################
@@ -88,18 +104,17 @@ module.exports = class CraftingPlan
@result.remove stack.itemSlug, quantityUsed @result.remove stack.itemSlug, quantityUsed
@_pending.add stack.itemSlug, quantityNeeded @_pending.add stack.itemSlug, quantityNeeded
@need.add stack.itemSlug, quantityNeeded @_need.add stack.itemSlug, quantityNeeded
_processOutputStack: (stack)-> _processOutputStack: (stack)->
quantityMissing = @need.quantityOf stack.itemSlug quantityMissing = @_need.quantityOf stack.itemSlug
quantityUsed = Math.min quantityMissing, stack.quantity quantityUsed = Math.min quantityMissing, stack.quantity
quantityLeft = stack.quantity - quantityUsed quantityLeft = stack.quantity - quantityUsed
logger.trace "processing output:#{stack.itemSlug}, logger.trace "processing output:#{stack.itemSlug},
m:#{quantityMissing}, u:#{quantityUsed}, l:#{quantityLeft}" m:#{quantityMissing}, u:#{quantityUsed}, l:#{quantityLeft}"
@make.add stack.itemSlug, stack.quantity @_need.remove stack.itemSlug, quantityUsed
@need.remove stack.itemSlug, quantityUsed
@result.add stack.itemSlug, quantityLeft @result.add stack.itemSlug, quantityLeft
_totalQuantityOf: (itemSlug)-> _totalQuantityOf: (itemSlug)->
return @result.quantityOf(itemSlug) - @need.quantityOf(itemSlug) return @result.quantityOf(itemSlug) - @_need.quantityOf(itemSlug)
+2
View File
@@ -24,6 +24,8 @@ module.exports = class CraftingTable extends BaseModel
attributes.want ?= new Inventory attributes.want ?= new Inventory
super attributes, options super attributes, options
Object.defineProperty @prototype, 'need', -> @plan?.need
# Public Methods ############################################################################### # Public Methods ###############################################################################
craft: -> craft: ->
+4 -4
View File
@@ -5,9 +5,9 @@ Copyright (c) 2014-2015 by Redwood Labs
All rights reserved. All rights reserved.
### ###
BaseModel = require './base_model' BaseModel = require './base_model'
CraftingTable = require './crafting_table' CraftingPlan = require './crafting_plan'
ModPack = require './mod_pack' ModPack = require './mod_pack'
######################################################################################################################## ########################################################################################################################
@@ -15,5 +15,5 @@ module.exports = class ItemPage extends BaseModel
constructor: (attributes={}, options={})-> constructor: (attributes={}, options={})->
attributes.modPack ?= new ModPack attributes.modPack ?= new ModPack
attributes.table ?= new CraftingTable modPack:attributes.modPack attributes.plan ?= new CraftingPlan attributes.modPack
super attributes, options super attributes, options
+1 -1
View File
@@ -8,6 +8,6 @@
.view__item_page .view__item_page
.view__inventory.have .view__inventory.have
.view__inventory.want .view__inventory.want
.view__inventory.need
.view__crafting_table
.view__mod_pack .view__mod_pack