-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInventorySystem.cs
More file actions
180 lines (151 loc) · 4.56 KB
/
InventorySystem.cs
File metadata and controls
180 lines (151 loc) · 4.56 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
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
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Events;
public class InventorySystem : MonoBehaviour
{
public static InventorySystem Instance { get; private set; }
[Header("Settings")]
[SerializeField] private int maxInventorySlots = 20;
// Events
public UnityEvent<InventoryItem> OnItemAdded;
public UnityEvent<InventoryItem> OnItemRemoved;
public UnityEvent<InventoryItem> OnItemUsed;
public UnityEvent OnInventoryChanged;
private List<InventoryItem> inventoryItems = new List<InventoryItem>();
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
// Initialize events if null
if (OnItemAdded == null) OnItemAdded = new UnityEvent<InventoryItem>();
if (OnItemRemoved == null) OnItemRemoved = new UnityEvent<InventoryItem>();
if (OnItemUsed == null) OnItemUsed = new UnityEvent<InventoryItem>();
if (OnInventoryChanged == null) OnInventoryChanged = new UnityEvent();
}
public bool AddItem(InventoryItem item)
{
if (inventoryItems.Count >= maxInventorySlots)
{
Debug.Log("Inventory is full!");
return false;
}
// Check if the item is stackable and already exists
if (item.isStackable)
{
InventoryItem existingItem = inventoryItems.Find(i => i.itemID == item.itemID);
if (existingItem != null)
{
existingItem.quantity += item.quantity;
OnInventoryChanged.Invoke();
OnItemAdded.Invoke(item);
return true;
}
}
// Add new item
inventoryItems.Add(item);
OnInventoryChanged.Invoke();
OnItemAdded.Invoke(item);
return true;
}
public bool RemoveItem(string itemID, int quantity = 1)
{
InventoryItem item = inventoryItems.Find(i => i.itemID == itemID);
if (item == null)
{
Debug.Log("Item not found in inventory!");
return false;
}
if (item.quantity < quantity)
{
Debug.Log("Not enough items to remove!");
return false;
}
item.quantity -= quantity;
if (item.quantity <= 0)
{
inventoryItems.Remove(item);
}
OnInventoryChanged.Invoke();
OnItemRemoved.Invoke(item);
return true;
}
public bool UseItem(string itemID)
{
InventoryItem item = inventoryItems.Find(i => i.itemID == itemID);
if (item == null)
{
Debug.Log("Item not found in inventory!");
return false;
}
// Handle consumable items
if (item.isConsumable)
{
item.quantity--;
if (item.quantity <= 0)
{
inventoryItems.Remove(item);
}
}
OnInventoryChanged.Invoke();
OnItemUsed.Invoke(item);
return true;
}
public List<InventoryItem> GetAllItems()
{
return new List<InventoryItem>(inventoryItems);
}
public InventoryItem GetItem(string itemID)
{
return inventoryItems.Find(i => i.itemID == itemID);
}
public int GetItemCount(string itemID)
{
InventoryItem item = inventoryItems.Find(i => i.itemID == itemID);
return item != null ? item.quantity : 0;
}
public bool HasItem(string itemID, int quantity = 1)
{
return GetItemCount(itemID) >= quantity;
}
public void ClearInventory()
{
inventoryItems.Clear();
OnInventoryChanged.Invoke();
}
}
[System.Serializable]
public class InventoryItem
{
public string itemID;
public string itemName;
public string description;
public Sprite icon;
public int quantity = 1;
public bool isStackable = true;
public bool isConsumable = false;
public ItemType itemType = ItemType.Misc;
public Dictionary<string, float> stats = new Dictionary<string, float>();
public InventoryItem(string id, string name, string desc, Sprite itemIcon, int qty = 1)
{
itemID = id;
itemName = name;
description = desc;
icon = itemIcon;
quantity = qty;
}
}
public enum ItemType
{
Weapon,
Armor,
Consumable,
Quest,
Misc
}