Initial version of the new crafting table

This commit is contained in:
Andrew Miner
2015-01-08 11:03:43 -08:00
parent 9f79722378
commit 9424af9d84
26 changed files with 280 additions and 228 deletions
+4
View File
@@ -22,6 +22,10 @@ module.exports = class BaseModel extends Backbone.Model
sync: -> # do nothing
trigger: (name)->
logger.trace "#{@constructor.name}.#{@cid} triggered a \"#{name}\" event"
super
# Object Overrides #############################################################################
toString: ->
+41
View File
@@ -0,0 +1,41 @@
###
Crafting Guide - crafting_grid.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
util = require 'util'
########################################################################################################################
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.defineProperty @prototype, 'slotCount', get:-> CraftingGrid.SLOT_COUNT
# Public Methods ###############################################################################
getItemDataAt: (index)->
if index >= @slotCount then throw new Error "index (#{index}) must be less than #{@slotCount}"
return null unless @recipe?
logger.debug "recipe: #{@recipe}"
itemSlug = @recipe.getItemSlugAt index
return null unless itemSlug?
item = @modPack.findItem itemSlug
if item?
itemData = item.pathSlugs
itemData.name = item.name
return itemData
else
return modSlug:'minecraft', itemSlug:itemSlug, name:@modPack.findName(itemSlug)
+9 -4
View File
@@ -5,15 +5,17 @@ Copyright (c) 2014-2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
Inventory = require './inventory'
########################################################################################################################
module.exports = class CraftingPlan
module.exports = class CraftingPlan extends BaseModel
constructor: (@modPack, includingTools)->
if not @modPack then throw new Error 'modPack is required'
@includingTools = if includingTools then true else false
constructor: (attributes={}, options={})->
if not attributes.modPack then throw new Error 'modPack is required'
attributes.includingTools ?= false
super attributes, options
@have = new Inventory
@want = new Inventory
@@ -35,6 +37,7 @@ module.exports = class CraftingPlan
@need.clear()
@result.clear()
@trigger 'change', this
return this
craft: ->
@@ -55,6 +58,8 @@ module.exports = class CraftingPlan
@need.addInventory @_need
@steps.reverse()
@trigger 'change', this
# Object Overrides #############################################################################
toString: ->
+15 -3
View File
@@ -6,9 +6,7 @@ All rights reserved.
###
BaseModel = require './base_model'
CraftingPlan = require './crafting_plan'
{Event} = require '../constants'
Inventory = require './inventory'
CraftingGrid = require './crafting_grid'
########################################################################################################################
@@ -16,9 +14,23 @@ 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.step ?= 0
super attributes, options
@plan.on 'change', => @reset()
# Public Methods ###############################################################################
reset: ->
logger.trace "CraftingTable.reset()"
@step = 0
@grid.recipe = @plan.steps[0]
@trigger 'change', this
return this
# Object Overrides #############################################################################
toString: ->
+5
View File
@@ -25,6 +25,8 @@ module.exports = class Item extends BaseModel
Object.defineProperty @prototype, 'isCraftable', get:-> @recipes.length > 0
Object.defineProperty @prototype, 'pathSlugs', get:-> @getPathSlugs()
# Public Methods ###############################################################################
addRecipe: (recipe)->
@@ -38,6 +40,9 @@ module.exports = class Item extends BaseModel
return if this.name < that.name then -1 else +1
return 0
getPathSlugs: ->
return modSlug:@modVersion.slug, itemSlug:@slug
# Object Overrides #############################################################################
toString: ->
+6 -4
View File
@@ -5,9 +5,10 @@ Copyright (c) 2014-2015 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
CraftingPlan = require './crafting_plan'
ModPack = require './mod_pack'
BaseModel = require './base_model'
CraftingPlan = require './crafting_plan'
CraftingTable = require './crafting_table'
ModPack = require './mod_pack'
########################################################################################################################
@@ -15,5 +16,6 @@ module.exports = class ItemPage extends BaseModel
constructor: (attributes={}, options={})->
attributes.modPack ?= new ModPack
attributes.plan ?= new CraftingPlan attributes.modPack
attributes.plan ?= new CraftingPlan modPack:attributes.modPack
attributes.table ?= new CraftingTable plan:attributes.plan
super attributes, options
+7 -1
View File
@@ -6,6 +6,7 @@ All rights reserved.
###
Item = require './item'
Logger = require '../logger'
ModVersion = require './mod_version'
Recipe = require './recipe'
Stack = require './stack'
@@ -28,7 +29,12 @@ module.exports = class ModVersionParser
parser = @_parsers["#{data.dataVersion}"]
if not parser? then throw new Error "cannot parse version #{data.dataVersion} mod descriptions"
return parser.parse data
oldLevel = logger.level
logger.level = Logger.WARNING
result = parser.parse data
logger.level = oldLevel
return result
unparse: (modVersion, dataVersion=ModVersionParser.CURRENT_VERSION)->
if not modVersion? then throw new Error 'modVersion is required'
+20 -1
View File
@@ -16,7 +16,7 @@ module.exports = class Recipe extends BaseModel
if not attributes.input? then throw new Error 'attributes.input is required'
if not attributes.output? then throw new Error 'attributes.output is required'
attributes.pattern ?= (i for i in [0...attributes.input.length]).join('')
attributes.pattern ?= @_computeShapelessPattern attributes.input
attributes.tools ?= []
super attributes, options
@@ -24,6 +24,15 @@ module.exports = class Recipe extends BaseModel
# Public Methods ###############################################################################
getItemSlugAt: (index)->
patternDigit = @pattern[index]
return null unless patternDigit?
stack = @input[parseInt(patternDigit)]
return null unless stack?
return stack.itemSlug
make: (inventory, missing)->
for stack in @input
itemSlug = stack.itemSlug
@@ -71,3 +80,13 @@ module.exports = class Recipe extends BaseModel
result.push '}'
return result.join ''
# Private Methods ##############################################################################
_computeShapelessPattern: (input)->
result = []
for i in [0...input.length]
stack = input[i]
for j in [0...stack.quantity]
result.push "#{i}"
return result.join ''
+26
View File
@@ -0,0 +1,26 @@
###
Crafting Guide - recipe_step.coffee
Copyright (c) 2014 by Redwood Labs
All rights reserved.
###
BaseModel = require './base_model'
########################################################################################################################
module.exports = class RecipeStep extends BaseModel
constructor: (attributes={}, options={})->
if not attributes.recipe? then throw new Error 'attributes.recipe is required'
attributes.multiple ?= 1
super attributes, options
@recipe.on 'change', =>
@trigger 'change:recipe'
@trigger 'change'
# Public Methods ###############################################################################
getItemAt: (index)->
return @recipe.getItemAt index