-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCraftingManager.cs
229 lines (212 loc) · 7.23 KB
/
CraftingManager.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using UnityEngine;
public class CraftingManager : MonoBehaviour
{
private PlayerController playerController;
public InventoryManager inventoryManager;
public InventoryManager[] storageComputerInventoryManager;
public ConduitItem conduitItem;
public bool missingItem;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
playerController = GetComponent<PlayerController>();
}
//! Used to disable the conduit item if this block is destroyed.
public void OnDestroy()
{
if (conduitItem != null)
{
conduitItem.active = false;
}
}
//! Called when the player crafts an item from the inventory GUI.
public void CraftItemAsPlayer(CraftingRecipe recipe)
{
int outputAmount = recipe.outputAmount;
int[] amounts = new int[recipe.amounts.Length];
recipe.amounts.CopyTo(amounts, 0);
if (Input.GetKey(KeyCode.LeftShift))
{
for (int i = 0; i < amounts.Length; i++)
{
amounts[i] = recipe.amounts[i] * 10;
}
outputAmount = recipe.outputAmount * 10;
}
InventorySlot[] slots = new InventorySlot[recipe.ingredients.Length];
bool[] found = new bool[recipe.ingredients.Length];
for (int i = 0; i < recipe.ingredients.Length; i++)
{
int currentSlot = 0;
foreach (InventorySlot slot in inventoryManager.inventory)
{
if (slot.amountInSlot >= amounts[i])
{
if (slot.typeInSlot.Equals(recipe.ingredients[i]))
{
found[i] = true;
slots[i] = slot;
}
}
currentSlot++;
}
}
bool itemNotFound = false;
foreach (bool b in found)
{
if (!itemNotFound)
{
itemNotFound = !b;
}
}
missingItem = itemNotFound;
if (!missingItem || playerController.creativeMode == true)
{
inventoryManager.AddItem(recipe.output, outputAmount);
if (inventoryManager.itemAdded)
{
if (playerController.creativeMode == false)
{
for (int i = 0; i < recipe.ingredients.Length; i++)
{
slots[i].amountInSlot -= amounts[i];
if (slots[i].amountInSlot <= 0)
{
slots[i].typeInSlot = "nothing";
}
}
}
playerController.PlayCraftingSound();
}
else
{
GetComponent<InventoryGUI>().outOfSpace = true;
playerController.PlayMissingItemsSound();
}
}
else
{
GetComponent<InventoryGUI>().missingItem = true;
playerController.PlayMissingItemsSound();
}
}
//! Called by the auto crafter when it is connected to a storage container.
public void CraftItemUsingStorageContainer(CraftingRecipe recipe)
{
InventorySlot[] slots = new InventorySlot[recipe.ingredients.Length];
bool[] found = new bool[recipe.ingredients.Length];
for (int i = 0; i < recipe.ingredients.Length; i++)
{
int currentSlot = 0;
foreach (InventorySlot slot in inventoryManager.inventory)
{
if (slot.amountInSlot >= recipe.amounts[i])
{
if (slot.typeInSlot.Equals(recipe.ingredients[i]))
{
found[i] = true;
slots[i] = slot;
}
}
currentSlot++;
}
}
bool itemNotFound = false;
foreach (bool b in found)
{
if (!itemNotFound)
{
itemNotFound = !b;
}
}
missingItem = itemNotFound;
if (!missingItem)
{
inventoryManager.AddItem(recipe.output, recipe.outputAmount);
if (inventoryManager.itemAdded)
{
for (int i = 0; i < recipe.ingredients.Length; i++)
{
slots[i].amountInSlot -= recipe.amounts[i];
if (slots[i].amountInSlot <= 0)
{
slots[i].typeInSlot = "nothing";
}
}
}
}
}
//! Called by the auto crafter when it is connected to a storage computer.
public void CraftItemUsingStorageComputer(CraftingRecipe recipe)
{
InventorySlot[] slots = new InventorySlot[recipe.ingredients.Length];
bool[] found = new bool[recipe.ingredients.Length];
foreach (InventoryManager manager in storageComputerInventoryManager)
{
for (int i = 0; i < recipe.ingredients.Length; i++)
{
int currentSlot = 0;
foreach (InventorySlot slot in manager.inventory)
{
if (slot.amountInSlot >= recipe.amounts[i])
{
if (slot.typeInSlot.Equals(recipe.ingredients[i]))
{
found[i] = true;
slots[i] = slot;
}
}
currentSlot++;
}
}
}
bool itemNotFound = false;
foreach (bool b in found)
{
if (!itemNotFound)
{
itemNotFound = !b;
}
}
missingItem = itemNotFound;
if (!missingItem)
{
bool itemAdded = false;
foreach (InventoryManager manager in storageComputerInventoryManager)
{
if (!itemAdded)
{
manager.AddItem(recipe.output, recipe.outputAmount);
if (manager.itemAdded)
{
itemAdded = true;
for (int i = 0; i < recipe.ingredients.Length; i++)
{
slots[i].amountInSlot -= recipe.amounts[i];
if (slots[i].amountInSlot <= 0)
{
slots[i].typeInSlot = "nothing";
}
}
if (conduitItem != null)
{
conduitItem.target = manager.gameObject;
if (conduitItem.textureDictionary != null)
{
conduitItem.billboard.GetComponent<Renderer>().material.mainTexture = conduitItem.textureDictionary[recipe.output];
}
conduitItem.active = true;
}
}
}
}
}
else
{
if (conduitItem != null)
{
conduitItem.active = false;
}
}
}
}