-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCraft.cs
More file actions
43 lines (34 loc) · 1.55 KB
/
Craft.cs
File metadata and controls
43 lines (34 loc) · 1.55 KB
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
using System.Collections.Generic;
using UnityEngine;
public static class Craft
{
// check inventory for items
public static bool Can(int amount, string itemId, Inventory inventory)
{
ItemInfo item = new(itemId);
return inventory.HaveEnoughList(item.data.requiredMaterials);
}
public static CraftingEntry Start(string itemId, Inventory inventory)
{
ItemInfo info = new(itemId);
inventory.RemoveList(info.data.requiredMaterials);
return new CraftingEntry(itemId);
}
public static InventoryItem Complete(CraftingEntry craft) => new(craft.item.data.ItemId, 1);
public static List<InventoryItem> Cancel(CraftingEntry craft)
{
// destroy materials based on progress
// comment and just return the mats from crafting entry if not desired
List<InventoryItem> leftovers = new List<InventoryItem>();
float elapsedTime = Time.time - craft.startAt;
float completionPercentage = Mathf.Clamp01(elapsedTime / (craft.readyAt - craft.startAt));
float destroyPercentage = completionPercentage / craft.item.data.requiredMaterials.Count * 0.5f;
// comment the rest and just return this part which is the mats from crafting entry if destruction is not desired
foreach (var material in craft.item.data.requiredMaterials)
{
InventoryItem leftover = new(material.item.Id, material.quantity - Mathf.RoundToInt(material.quantity * destroyPercentage));
leftovers.Add(leftover);
}
return leftovers;
}
}