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,69 @@
###
Crafting Guide - item_node.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
CraftingNode = require './crafting_node'
RecipeNode = require './recipe_node'
########################################################################################################################
module.exports = class ItemNode extends CraftingNode
constructor: (options={})->
if not options.item? then throw new Error 'options.item is required'
super options
@item = options.item
@_recipes = null
# Property Methods #############################################################################
getRecipes: ->
if not @_recipes?
@_recipes = @modPack.findRecipes @item.slug
return @_recipes or []
isGatherable: ->
return true if @item.isGatherable
return true unless @getRecipes().length > 0
return false
Object.defineProperties @prototype,
gatherable: { get:@prototype.isGatherable }
recipes: { get:@prototype.getRecipes }
# CraftingNode Overrides #######################################################################
_createChildren: (result=[])->
recipes = @getRecipes()
return [] unless recipes.length > 0
for recipe in recipes
result.push new RecipeNode modPack:@modPack, recipe:recipe
return result
_checkCompleteness: ->
return true if @gatherable
return false unless @children?
for child in @children
return true if child.isComplete
return false
_checkValidity: ->
for child in @children
return true if child.isValid
# Object Overrides #############################################################################
toString: (indent)->
completeText = if @complete then 'complete' else 'incomplete'
parts = ["#{indent}#{@completeText} ItemNode for #{@item.name}"]
nextIndent = indent + ' '
for child in @children
parts.push child.toString nextIndent
return parts.join '\n'