Factor out the ReceipeController

This commit is contained in:
Andrew Miner
2015-01-18 16:37:23 -08:00
parent 0811a868af
commit ecc839cca8
12 changed files with 208 additions and 164 deletions
-34
View File
@@ -1,34 +0,0 @@
###
Crafting Guide - crafting_grid.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
########################################################################################################################
module.exports = class CraftingGrid extends BaseModel
@SLOT_COUNT = 9
constructor: (attributes={}, options={})->
if not attributes.modPack? then throw new Error 'attributes.modPack is required'
attributes.recipe ?= null
super attributes, options
Object.defineProperties this,
'slotCount': { get:-> CraftingGrid.SLOT_COUNT }
# Public Methods ###############################################################################
getItemDisplayAt: (slot)->
if slot >= @slotCount then throw new Error "slot (#{slot}) must be less than #{@slotCount}"
return null unless @recipe?
slug = @recipe.getItemSlugAt slot
return null unless slug?
itemDisplay = @modPack.findItemDisplay slug
return itemDisplay
+23 -42
View File
@@ -5,8 +5,8 @@ Copyright (c) 2014-2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
CraftingGrid = require './crafting_grid'
BaseModel = require './base_model'
{Event} = require '../constants'
########################################################################################################################
@@ -14,63 +14,51 @@ module.exports = class CraftingTable extends BaseModel
constructor: (attributes={}, options={})->
if not attributes.plan? then throw new Error "attributes.plan is required"
attributes.grid ?= new CraftingGrid modPack:attributes.plan.modPack
attributes.modPack ?= attributes.plan.modPack
super attributes, options
@plan.on 'change', => @reset()
@_step = 0
@_stepIndex = 0
Object.defineProperties this, {
hasNextStep: { get:@hasNextStep }
hasPrevStep: { get:@hasPrevStep }
hasSteps: { get:@hasSteps }
multiplier: { get:@getMultiplier }
output: { get:@getOutput }
step: { get:@getStep, set:@setStep }
stepCount: { get:@getStepCount }
toolNames: { get:@getToolNames }
currentStep: { get:@getCurrentStep }
hasNextStep: { get:@hasNextStep }
hasPrevStep: { get:@hasPrevStep }
hasSteps: { get:@hasSteps }
stepCount: { get:@getStepCount }
stepIndex: { get:@getStepIndex, set:@setStepIndex }
}
# Public Methods ###############################################################################
reset: ->
@step = 0
@stepIndex = 0
return this
# Property Methods #############################################################################
hasNextStep: ->
return @_step + 1 < @plan.steps.length
return @_stepIndex + 1 < @plan.steps.length
hasPrevStep: ->
return @_step > 0
return @_stepIndex > 0
hasSteps: ->
return @plan.steps.length > 0
getMultiplier: ->
step = @plan.steps[@_step]
return unless step?
return step.multiplier
getCurrentStep: ->
return @plan.steps[@_stepIndex]
getOutput: ->
step = @plan.steps[@_step]
return unless step?
return step.recipe.output[0]
getStepIndex: ->
return @_stepIndex
getStep: ->
return @_step
setStepIndex: (newStepIndex)->
oldStepIndex = @_stepIndex
newStepIndex = Math.max 0, Math.min @plan.steps.length - 1, newStepIndex
setStep: (newStep)->
oldStep = @_step
newStep = Math.max 0, Math.min @plan.steps.length - 1, newStep
@_stepIndex = newStepIndex
@_step = newStep
@grid.recipe = @plan.steps[@_step]?.recipe
@trigger 'change:step', this, oldStep, newStep
@trigger 'change', this
@trigger Event.change + ':stepIndex', this, oldStepIndex, newStepIndex
@trigger Event.change, this
return this
@@ -78,14 +66,7 @@ module.exports = class CraftingTable extends BaseModel
return 0 unless @plan.steps?
return @plan.steps.length
getToolNames: ->
recipe = @plan.steps[@_step]?.recipe
return '' unless recipe?
toolSlugs = (stack.slug for stack in recipe.tools)
toolNames = (@modPack.findName(slug) for slug in toolSlugs).join ', '
return toolNames
# Object Overrides #############################################################################
toString: ->
return "#{@constructor.name} (#{@cid}) { plan:#{@plan}, step:#{@_step} }"
return "#{@constructor.name} (#{@cid}) { plan:#{@plan}, step:#{@_stepIndex} }"