Add code to build crafting trees

This commit is contained in:
Andrew Miner
2015-08-25 21:15:08 -07:00
parent 4ff2915d85
commit ea82518b0d
8 changed files with 492 additions and 2 deletions
@@ -0,0 +1,47 @@
###
Crafting Guide - crafting_plan_node.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
CraftingNode = require './crafting_node'
ItemNode = require './item_node'
########################################################################################################################
module.exports = class InventoryNode extends CraftingNode
constructor: (options={})->
if not options.inventory? then throw new Error 'options.inventory is required'
super options
@inventory = options.inventory
# CraftingNode Overrides #######################################################################
_createChildren: (result=[])->
@inventory.each (stack)=>
item = @modPack.findItem stack.itemSlug
if not item? then throw new Error "Could not find an item for slug: #{stack.itemSlug}"
result.push new ItemNode modPack:@modPack, item:item
return result
_checkCompleteness: ->
for child in @children
return false unless child.isComplete
return true
_checkValidity: ->
for child in @children
return false unless child.isValid
# Object Overrides #############################################################################
toString: (indent='')->
completeText = if @complete then 'complete' else 'incomplete'
parts = ["#{indent}#{@completeText} InventoryNode for #{@inventory}"]
nextIndent = indent + ' '
for child in @children
parts.push child.toString nextIndent
return parts.join '\n'