This commit is contained in:
Andrew Miner
2015-01-02 23:03:06 -08:00
parent e389c01468
commit b642c51486
23 changed files with 848 additions and 438 deletions
+49
View File
@@ -0,0 +1,49 @@
###
Crafting Guide - stack.coffee
Copyright (c) 2014 by Redwood Labs
All rights reserved.
###
########################################################################################################################
module.exports = class Stack
constructor: (attributes={})->
if not attributes.item? then throw new Error 'item is required'
attributes.quantity ?= 1
@item = attributes.item
@quantity = attributes.quantity
Object.defineProperty @prototype, 'name', get:-> @item?.name
Object.defineProperty @prototype, 'stackQuantity', get:@getStackQuantity
# Public Methods ###############################################################################
canMerge: (stack)->
return @item.slug is stack.item.slug
merge: (stack)->
if not @canMerge stack
throw new Error "this stack of #{@item.name} cannot merge a stack of #{@stack.name}"
@quantity += stack.quantity
return this
# Property Methods #############################################################################
getStackQuantity: ->
count = 0
extra = @quantity
while extra > @item.stackSize
extra -= @item.stackSize
count += 1
return count:count, extra:extra
# Object Overrides #############################################################################
toString: ->
return "#{@quantity} #{@item.name}"