Add visitor pattern to crafting nodes

This commit is contained in:
Andrew Miner
2015-08-28 19:16:12 -07:00
parent badb854107
commit f6b71c9464
7 changed files with 203 additions and 91 deletions
@@ -29,6 +29,24 @@ module.exports = class CraftingNode
queue.push child
return queue
acceptVisitor: (visitor)->
enter = visitor[@ENTER_METHOD]
if enter?
enter.call visitor, this
else
enter = visitor['onEnterOtherNode']
if enter? then enter.call visitor, this
for child in @_children
child.acceptVisitor visitor
leave = visitor[@LEAVE_METHOD]
if leave?
leave.call visitor, this
else
leave = visitor['onLeaveOtherNode']
if leave? then leave.call visitor, this
# Property Methods #############################################################################
getChildren: ->
@@ -12,6 +12,9 @@ ItemNode = require './item_node'
module.exports = class InventoryNode extends CraftingNode
@::ENTER_METHOD = 'onEnterInventoryNode'
@::LEAVE_METHOD = 'onLeaveInventoryNode'
constructor: (options={})->
if not options.inventory? then throw new Error 'options.inventory is required'
super options
@@ -38,10 +41,14 @@ module.exports = class InventoryNode extends CraftingNode
# Object Overrides #############################################################################
toString: (indent='')->
toString: (options={})->
options.indent ?= ''
options.recursive ?= true
completeText = if @complete then 'complete' else 'incomplete'
parts = ["#{indent}#{@completeText} InventoryNode for #{@inventory}"]
nextIndent = indent + ' '
for child in @children
parts.push child.toString nextIndent
parts = ["#{options.indent}#{@completeText} InventoryNode for #{@inventory}"]
nextIndent = options.indent + ' '
if options.recursive
for child in @children
parts.push child.toString indent:nextIndent
return parts.join '\n'
+12 -5
View File
@@ -12,6 +12,9 @@ RecipeNode = require './recipe_node'
module.exports = class ItemNode extends CraftingNode
@::ENTER_METHOD = 'onEnterItemNode'
@::LEAVE_METHOD = 'onLeaveItemNode'
constructor: (options={})->
if not options.item? then throw new Error 'options.item is required'
super options
@@ -60,10 +63,14 @@ module.exports = class ItemNode extends CraftingNode
# Object Overrides #############################################################################
toString: (indent)->
toString: (options={})->
options.indent ?= ''
options.recursive ?= true
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
parts = ["#{options.indent}#{@completeText} ItemNode for #{@item.name}"]
nextIndent = options.indent + ' '
if options.recursive
for child in @children
parts.push child.toString indent:nextIndent
return parts.join '\n'
+12 -5
View File
@@ -12,6 +12,9 @@ CraftingNode = require './crafting_node'
module.exports = class RecipeNode extends CraftingNode
@::ENTER_METHOD = 'onEnterRecipeNode'
@::LEAVE_METHOD = 'onLeaveRecipeNode'
constructor: (options={})->
if not options.recipe? then throw new Error 'options.recipe is required'
super options
@@ -52,10 +55,14 @@ module.exports = class RecipeNode extends CraftingNode
# Object Overrides ############################################################################
toString: (indent)->
toString: (options={})->
options.indent ?= ''
options.recursive ?= true
completeText = if @complete then 'complete' else 'incomplete'
parts = ["#{indent}#{@completeText} RecipeNode for #{@recipe.slug}"]
nextIndent = indent + ' '
for child in @children
parts.push child.toString nextIndent
parts = ["#{options.indent}#{@completeText} RecipeNode for #{@recipe.slug}"]
nextIndent = options.indent + ' '
if options.recursive
for child in @children
parts.push child.toString indent:nextIndent
return parts.join '\n'