-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.cs
149 lines (122 loc) · 4.6 KB
/
Item.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
using Newtonsoft.Json;
namespace inventory_example.InventorySystem
{
[JsonObject(MemberSerialization.OptIn)]
public class Item : IDisposable, ISlotContainer
{
#region UI Properties
[JsonProperty("id")]
public string ID { get; private set; }
[JsonProperty("name")]
public string Name { get; private set; }
[JsonProperty("tags")]
public List<string> Tags { get; private set; }
[JsonProperty("description")]
public string Description { get; private set; }
#endregion
#region Mechanics
[JsonProperty("quantity")]
public int Quantity { get; private set; }
[field: NonSerialized]
public Inventory? Context { get; private set; }
[JsonProperty("currentSlot")]
public int CurrentSlot { get; private set; }
public int StackSize { get; private set; }
[JsonProperty("baseSell")]
public int BaseSellPrice { get; private set; }
[JsonProperty("baseBuy")]
public int BaseBuyPrice { get; private set; }
#endregion
public string Icon { get => m_data.Icon; }
[JsonProperty("guid")]
private readonly string m_guid;
protected dynamic m_data;
protected bool m_disposedValue;
public bool IsStackableWith(string itemID) => ID == itemID;
public Item(dynamic data, int quantity = 1, Inventory? context = null, int currentSlot = 0, string guid = default)
{
//Set Members
m_data = data;
m_guid = guid ?? Guid.NewGuid().ToString();
//Set Properties
ID = m_data.id;
Name = m_data.name;
Tags = (m_data.tags as string).Split(',').ToList();
Description = m_data.description;
StackSize = m_data.stackSize;
BaseBuyPrice = m_data.buyValue * 2;
BaseSellPrice = m_data.buyValue;
//Set Instance Properties
Context = context;
Quantity = quantity;
CurrentSlot = currentSlot;
}
public bool IsStackFull => Quantity >= m_data.StackSize;
/// <summary>Adds amount to item stack. Returns carry, if there's overflow. Also returns negative when there's not enough to consume. Does not automatically set to 0 when negative.</summary>
/// <param name="amount">Amount to add.</param>
public int AddQuantity(int amount)
{
int newQuantity = Quantity + amount;
if (newQuantity < 0) return newQuantity;
Quantity = Math.Min(StackSize, newQuantity);
return newQuantity - Quantity;
}
/// <summary>Splits an item stack by amount. Doesn't split by quantity (it should move instead). Returns the remaining item.</summary>
public Item? SplitBy(int amount)
{
if (Quantity < 2 || amount >= Quantity) return null;
int newItemAmount = Quantity - amount;
AddQuantity(-amount);
return Copy(newItemAmount);
}
/// <summary>Splits an item stack in half. Returns the remaining item.</summary>
public Item? SplitHalf()
{
if (Quantity < 2) return null;
int removedAmount = Quantity / 2;
int newItemAmount = Quantity - removedAmount;
AddQuantity(-removedAmount);
return Copy(newItemAmount);
}
/// <summary>Returns a detached no-context item. Must call SetInventoryContext next.</summary>
/// <param name="quantity"></param>
public virtual Item Copy(int quantity = 0)
{
return new(m_data, quantity == 0 ? Quantity : quantity);
}
public void SetEmptyContext()
{
Context = null;
CurrentSlot = -1;
}
public void SetContext(Inventory context, int slot)
{
Context = context;
CurrentSlot = slot;
}
protected virtual void Dispose(bool disposing)
{
if (!m_disposedValue)
{
if (disposing)
{
//Dispose managed objects
m_data = null;
Tags = null;
Context = null;
}
//Free unmanaged objects
m_disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
public override string ToString()
{
return $"[{Quantity} {Name}] (H: <i>{m_guid}</i>)";
}
}
}