-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpickaxe.coffee
126 lines (91 loc) · 5.11 KB
/
pickaxe.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
ItemPile = require 'itempile'
{Recipe, AmorphousRecipe, PositionalRecipe, CraftingThesaurus, RecipeLocator} = require 'craftingrecipes'
module.exports = (game, opts) ->
return new ToolsPlugin(game, opts)
module.exports.pluginInfo =
loadAfter: ['voxel-recipes', 'voxel-registry']
class ToolsPlugin
constructor: (@game, opts) ->
@registry = game.plugins?.get('voxel-registry') ? throw new Error('voxel-pickaxe requires "voxel-registry" plugin')
# TODO: require/warning if voxel-mine missing? without, 'speed' property won't have any effect
@enable()
enable: () ->
@registry.registerBlock 'plankOak', {displayName: 'Oak Planks', texture: 'planks_oak'}
@registry.registerItem 'stick', {itemTexture: 'items/stick'}
@registry.registerItem 'pickaxeWood', {displayName: 'Wooden Pickaxe', itemTexture: 'items/wood_pickaxe', speed: 5.0, maxDamage:8, toolClass: 'pickaxe'}
@registry.registerItem 'pickaxeStone', {displayName: 'Stone Pickaxe', itemTexture: 'items/stone_pickaxe', speed: 10.0, maxDamage:128, toolClass: 'pickaxe'}
@registry.registerItem 'pickaxeIron', {displayName: 'Iron Pickaxe', itemTexture: 'items/iron_pickaxe', speed: 25.0, maxDamage:512, toolClass: 'pickaxe'}
@registry.registerItem 'spadeWood', {displayName: 'Wooden Spade', itemTexture: 'items/wood_shovel', speed: 5.0, maxDamage:8, toolClass: 'spade'}
@registry.registerItem 'spadeStone', {displayName: 'Stone Spade', itemTexture: 'items/stone_shovel', speed: 10.0, maxDamage:128, toolClass: 'spade'}
@registry.registerItem 'spadeIron', {displayName: 'Iron Spade', itemTexture: 'items/iron_shovel', speed: 25.0, maxDamage:512, toolClass: 'spade'}
@registry.registerItem 'axeWood', {displayName: 'Wooden Axe', itemTexture: 'items/wood_axe', speed: 5.0, maxDamage:8, toolClass: 'axe'}
@registry.registerItem 'axeStone', {displayName: 'Stone Axe', itemTexture: 'items/stone_axe', speed: 10.0, maxDamage:128, toolClass: 'axe'}
@registry.registerItem 'axeIron', {displayName: 'Iron Axe', itemTexture: 'items/iron_axe', speed: 25.0, maxDamage:512, toolClass: 'axe'}
# recipes
recipes = @game.plugins?.get('voxel-recipes')
if recipes?
recipes.thesaurus.registerName 'wood.plank', 'plankOak'
recipes.register new AmorphousRecipe(['wood.log'], new ItemPile('plankOak', 2))
recipes.register new AmorphousRecipe(['wood.plank', 'wood.plank'], new ItemPile('stick', 4))
recipes.register @pickaxeRecipe('wood.plank', 'pickaxeWood')
recipes.register @pickaxeRecipe('cobblestone', 'pickaxeStone')
recipes.register @pickaxeRecipe('ingotIron', 'pickaxeIron')
recipes.register new RepairRecipe('pickaxeWood', 'plankOak', 4)
recipes.register new RepairRecipe('pickaxeStone', 'cobblestone', 20)
recipes.register new RepairRecipe('pickaxeIron', 'ingotIron', 200)
recipes.register @spadeRecipe('wood.plank', 'spadeWood')
recipes.register @spadeRecipe('cobblestone', 'spadeStone')
recipes.register @spadeRecipe('ingotIron', 'spadeIron')
recipes.register @axeRecipe('wood.plank', 'axeWood')
recipes.register @axeRecipe('cobblestone', 'axeStone')
recipes.register @axeRecipe('ingotIron', 'axeIron')
pickaxeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') ->
return new PositionalRecipe([
[headMaterial, headMaterial, headMaterial],
[undefined, handleMaterial, undefined],
[undefined, handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0}))
spadeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') ->
return new PositionalRecipe([
[headMaterial],
[handleMaterial],
[handleMaterial]],
new ItemPile(toolMaterial, 1, {damage:0}))
axeRecipe: (headMaterial, toolMaterial, handleMaterial='stick') ->
# TODO: support mirrored recipe
return new PositionalRecipe([
[headMaterial, headMaterial],
[handleMaterial, headMaterial],
[handleMaterial, undefined]], new ItemPile(toolMaterial, 1, {damage:0}))
disable: () ->
# TODO
class RepairRecipe extends Recipe
constructor: (@toolItem, @repairingItem, @repairAmount) ->
findMatchingSlots: (inventory) ->
# tool + repairing item, side by side in that order
foundIndices = {}
if inventory.get(0)?.item == @toolItem # TODO: thesaurus
foundIndices.tool = 0
else
return undefined
if inventory.get(1)?.item == @repairingItem # TODO: thesaurus
foundIndices.repairing = 1
else
return undefined
return foundIndices
computeOutput: (inventory) ->
slots = @findMatchingSlots(inventory)
return undefined if not slots?
tool = inventory.get(slots.tool)
repairedTool = tool.clone()
oldDamage = repairedTool.tags.damage ? 0
newDamage = oldDamage - @repairAmount
newDamage = 0 if newDamage < 0
repairedTool.tags.damage = newDamage
return repairedTool
# TODO: refactor with craftingrecipes module
craft: (inventory) ->
slots = @findMatchingSlots(inventory)
return undefined if !slots?
inventory.takeAt slots.tool, 1 # TODO: check return
inventory.takeAt slots.repairing, 1 # TODO: check return
return @computeOutput(inventory)