A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
InventoryManager.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
7public class InventoryManager : SingletonMonobehaviour<InventoryManager>
8{
9 private Dictionary<int, ItemDetails> m_ItemDetailsDictionary;
10
13
16 public List<InventoryItem>[] m_InventoryLists;
17
20 [HideInInspector]
22
23 [SerializeField]
24 private SOItemList m_ItemList = null;
25
32 protected override void Awake()
33 {
34 base.Awake();
35
36 // Create Inventory List
38
39 // Create item details dictionary
41
42 // Initialize selected inventory item array
43 m_SelectedInventoryItem = new int[(int)InventoryLocation.Count];
44
45 for( int i = 0; i < m_SelectedInventoryItem.Length; i++ )
46 {
48 }
49 }
50
54 private void CreateInventoryLists()
55 {
56
57 m_InventoryLists = new List<InventoryItem>[(int)InventoryLocation.Count];
58
59 for( int i = 0; i < (int)InventoryLocation.Count; i++ )
60 {
61 m_InventoryLists[i] = new List<InventoryItem>();
62 }
63
64 // Initialize inventory list capacity array
66
67 // Initialize player inventory list capacity
69
70 }
71
76 {
77 m_ItemDetailsDictionary = new Dictionary<int, ItemDetails>();
78
79 foreach( ItemDetails itemDetails in m_ItemList.m_ItemDetails )
80 {
81 m_ItemDetailsDictionary.Add( itemDetails.m_ItemCode, itemDetails );
82 }
83 }
84
90 public void AddItem( InventoryLocation inventoryLocation, Item item )
91 {
92 int itemCode = item.ItemCode;
93 List<InventoryItem> inventoryList = m_InventoryLists[(int)inventoryLocation];
94
95 // Check if inventory already contains the item
96 int itemPosition = FindItemInInventory( inventoryLocation, itemCode );
97
98 if( itemPosition != -1 )
99 {
100 AddItemAtPosition( inventoryList, itemCode, itemPosition );
101 }
102 else
103 {
104 AddItemAtPosition( inventoryList, itemCode );
105 }
106
107 // Send event that inventory has been updated
108 EventHandler.CallInventoryUpdatedEvent( inventoryLocation, m_InventoryLists[(int)inventoryLocation] );
109 }
110
117 public void AddItem( InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete )
118 {
119 AddItem( inventoryLocation, item );
120 Destroy( gameObjectToDelete );
121 }
122
128 private void AddItemAtPosition( List<InventoryItem> inventoryList, int itemCode )
129 {
130 InventoryItem inventoryItem = new InventoryItem();
131
132 inventoryItem._ItemCode = itemCode;
133 inventoryItem._ItemQuantity = 1;
134 inventoryList.Add( inventoryItem );
135
136 // DebugPrintInventoryList( inventoryList );
137 }
138
142 private void AddItemAtPosition( List<InventoryItem> inventoryList, int itemCode, int position )
143 {
144 InventoryItem inventoryItem = new InventoryItem();
145
146 int quantity = inventoryList[position]._ItemQuantity + 1;
147 inventoryItem._ItemCode = itemCode;
148 inventoryItem._ItemQuantity = quantity;
149 inventoryList[position] = inventoryItem;
150
151 Debug.ClearDeveloperConsole();
152 // DebugPrintInventoryList( inventoryList );
153 }
154
161 public void SwapInventoryItems( InventoryLocation inventoryLocation, int fromItem, int toItem )
162 {
163 // If fromItem index and toItemIndex are within the bounds of the list, not the same, and greater than or equal to zero
164 if( fromItem < m_InventoryLists[(int)inventoryLocation].Count && toItem < m_InventoryLists[(int)inventoryLocation].Count
165 && fromItem != toItem && fromItem >= 0 && toItem >= 0 )
166 {
167 InventoryItem fromInventoryItem = m_InventoryLists[(int)inventoryLocation][fromItem];
168 InventoryItem toInventoryItem = m_InventoryLists[(int)inventoryLocation][toItem];
169
170 m_InventoryLists[(int)inventoryLocation][toItem] = fromInventoryItem;
171 m_InventoryLists[(int)inventoryLocation][fromItem] = toInventoryItem;
172
173 // Send event that inventory has been updated.
174 EventHandler.CallInventoryUpdatedEvent( inventoryLocation, m_InventoryLists[(int)inventoryLocation] );
175 }
176 }
177
181 public void ClearSelectedInventoryItem( InventoryLocation inventoryLocation )
182 {
183 m_SelectedInventoryItem[(int)inventoryLocation] = -1;
184 }
185
193 public int FindItemInInventory( InventoryLocation inventoryLocation, int itemCode )
194 {
195 List<InventoryItem> inventoryList = m_InventoryLists[(int)inventoryLocation];
196
197 for( int i = 0; i < inventoryList.Count; i++ )
198 {
199 if( inventoryList[i]._ItemCode == itemCode )
200 {
201 return i;
202 }
203 }
204
205 return -1;
206 }
207
213 public ItemDetails GetItemDetails( int itemCode )
214 {
215 ItemDetails itemDetails;
216
217 if( m_ItemDetailsDictionary.TryGetValue( itemCode, out itemDetails) )
218 {
219 return itemDetails;
220 }
221
222 return null;
223 }
224
231 {
232 int itemCode = GetSelectedInventoryItem( inventoryLocation );
233
234 if( itemCode == -1 )
235 {
236 return null;
237 }
238 else
239 {
240 return GetItemDetails( itemCode );
241 }
242 }
243
248 private int GetSelectedInventoryItem( InventoryLocation inventoryLocation )
249 {
250 return m_SelectedInventoryItem[(int)inventoryLocation];
251 }
252
258 public string GetItemTypeDescription( ItemType itemType )
259 {
260 string itemTypeDescription;
261
262 switch( itemType )
263 {
264 case ItemType.BreakingTool:
265 itemTypeDescription = Settings.m_BreakingTool;
266 break;
267
268 case ItemType.ChoppingTool:
269 itemTypeDescription = Settings.m_ChoppingTool;
270 break;
271
272 case ItemType.HoeingTool:
273 itemTypeDescription = Settings.m_HoeingTool;
274 break;
275
276 case ItemType.ReapingTool:
277 itemTypeDescription = Settings.m_ReapingTool;
278 break;
279
280 case ItemType.WateringTool:
281 itemTypeDescription = Settings.m_WateringTool;
282 break;
283
284 case ItemType.CollectingTool:
285 itemTypeDescription = Settings.m_CollectingTool;
286 break;
287
288 default:
289 itemTypeDescription = itemType.ToString();
290 break;
291 }
292
293 return itemTypeDescription;
294 }
295
299 public void RemoveItem( InventoryLocation inventoryLocation, int itemCode )
300 {
301 List<InventoryItem> inventoryList = m_InventoryLists[(int)inventoryLocation];
302
303 // Check if inventory already contains the item.
304 int itemPosition = FindItemInInventory( inventoryLocation, itemCode );
305
306 if( itemPosition != -1 )
307 {
308 RemoveItemAtPosition( inventoryList, itemCode, itemPosition );
309 }
310
311 // Send event that inventory has been updated
312 EventHandler.CallInventoryUpdatedEvent( inventoryLocation, m_InventoryLists[(int)inventoryLocation] );
313 }
314
321 private void RemoveItemAtPosition( List<InventoryItem> inventoryList, int itemCode, int position )
322 {
323 InventoryItem inventoryItem = new InventoryItem();
324
325 int quantity = inventoryList[position]._ItemQuantity - 1;
326
327 if( quantity > 0 )
328 {
329 inventoryItem._ItemQuantity = quantity;
330 inventoryItem._ItemCode = itemCode;
331 inventoryList[position] = inventoryItem;
332 }
333 else
334 {
335 inventoryList.RemoveAt(position);
336 }
337 }
338
344 public void SetSelectedInventoryItem( InventoryLocation inventoryLocation, int itemCode )
345 {
346 m_SelectedInventoryItem[(int)inventoryLocation] = itemCode;
347 }
348
352 //private void DebugPrintInventoryList( List<InventoryItem> inventoryList )
353 //{
354 // foreach( InventoryItem inventoryItem in inventoryList )
355 // {
356 // Debug.Log("Item Description: " + InventoryManager.Instance.GetItemDetails( inventoryItem._ItemCode ).m_ItemDescription + " Item Quantity: " + inventoryItem._ItemQuantity );
357 // }
358 // Debug.Log("********************************************************");
359 //}
360
361}
InventoryLocation
Describes the type of inventory and count Ex: player inventory or chest inventory
Definition: Enums.cs:108
ItemType
Describe the item type
Definition: Enums.cs:162
Raise an handle event when a movement is triggered.
Definition: EventHandler.cs:15
static void CallInventoryUpdatedEvent(InventoryLocation inventoryLocation, List< InventoryItem > inventoryList)
Update the event
Definition: EventHandler.cs:35
This class will manage all the inventory related operations.
string GetItemTypeDescription(ItemType itemType)
Return the desctiption of the item type.
void AddItemAtPosition(List< InventoryItem > inventoryList, int itemCode)
Add item to the end of the inventory.
void RemoveItemAtPosition(List< InventoryItem > inventoryList, int itemCode, int position)
Remove an item from a specific position
int GetSelectedInventoryItem(InventoryLocation inventoryLocation)
Get the selected item for inventoryLocation
void CreateItemDetailsDictionary()
Populates the itemDetailsDictionary from scriptable object item list.
int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode)
Find if an itemCode is already in the inventory. Returns the item position in the inventory list,...
void RemoveItem(InventoryLocation inventoryLocation, int itemCode)
Remove an item from the inventory, and create a gameobject at the position it was dropped
override void Awake()
Create item details dictionary Keep the original awake function in Singleton class and the override i...
void AddItem(InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete)
Add an item to the inventory list for the inventoryLocation and then destroy the gameObjectToDelete
void ClearSelectedInventoryItem(InventoryLocation inventoryLocation)
Clear the selected inventory item for inventory Location.
void SwapInventoryItems(InventoryLocation inventoryLocation, int fromItem, int toItem)
Swap item at fromItem index with item at toItem index in inventoryLocation inventory list.
Dictionary< int, ItemDetails > m_ItemDetailsDictionary
int[] m_SelectedInventoryItem
The index of the array is the inventory list, and the value is the item code.
int[] m_InventoryListCapacityIntArray
The index of the array is the inventory list (From the inventory location enum), and the value is the...
List< InventoryItem >[] m_InventoryLists
We have two inventory lists, one for player and other for chest. index 0 is for player.
void AddItem(InventoryLocation inventoryLocation, Item item)
Add an item to the inventory List for the inventory location
void SetSelectedInventoryItem(InventoryLocation inventoryLocation, int itemCode)
Set the selected inventory item for inventoryLocation to itemCode
void CreateInventoryLists()
Create Inventory Lists
ItemDetails GetItemDetails(int itemCode)
Returns the itemDetails (from the SOItemList) for the itemCode, or null if the item code doesn't exis...
void AddItemAtPosition(List< InventoryItem > inventoryList, int itemCode, int position)
Add item to postion in the inventory
SOItemList m_ItemList
ItemDetails GetSelectedInventoryItemDetails(InventoryLocation inventoryLocation)
Returns the item details (From the So_ItemList) of the currently selected item.
This class holds information of items.
Definition: ItemDetails.cs:8
int m_ItemCode
Definition: ItemDetails.cs:16
Definition: Item.cs:4
int ItemCode
Definition: Item.cs:11
List< ItemDetails > m_ItemDetails
Definition: SOItemList.cs:8
This class handle the game settings.
Definition: Settings.cs:9
static int m_PlayerInitialInventoryCapacity
Inventory.
Definition: Settings.cs:26
const string m_BreakingTool
Definition: Settings.cs:61
const string m_HoeingTool
Definition: Settings.cs:59
const string m_ChoppingTool
Definition: Settings.cs:60
const string m_CollectingTool
Definition: Settings.cs:64
const string m_ReapingTool
Definition: Settings.cs:62
const string m_WateringTool
Definition: Settings.cs:63
Abstract class that inherited by all the game managers. All the game managers use singleton since gam...