mirror of
				https://github.com/DerTyp7/example-top-down-unity.git
				synced 2025-10-30 12:57:08 +01:00 
			
		
		
		
	-Added Items
-Added item stacking -adding and removing items
This commit is contained in:
		
							
								
								
									
										10
									
								
								Assets/Scripts/Inventory/FoodItem.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Assets/Scripts/Inventory/FoodItem.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| [CreateAssetMenu(fileName = "FoodItem", menuName = "Items/FoodItem", order = 2)] | ||||
| public class FoodItem : Item | ||||
| { | ||||
|     public int health; | ||||
|     public int water; | ||||
|     public int regeneration; | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/FoodItem.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/FoodItem.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 0f6fecdcda7efd84baa350f4c41432a6 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										117
									
								
								Assets/Scripts/Inventory/Inventory.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								Assets/Scripts/Inventory/Inventory.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,117 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class Inventory : MonoBehaviour | ||||
| { | ||||
|  | ||||
|     [SerializeField]public List<Slot> inventory = new List<Slot>(); | ||||
|     [SerializeField] int maxSize; | ||||
|  | ||||
|     public List<Slot> getInventory { get => inventory; set => inventory = value; } | ||||
|     public void createEmptyInventory(int size)  | ||||
|     { | ||||
|         inventory = new List<Slot>(); | ||||
|         for (int i = 0; i < size; i++) | ||||
|         { | ||||
|             inventory.Add(new Slot(10)); | ||||
|         } | ||||
|          | ||||
|     } | ||||
|     bool indexIsInRange(int index)  | ||||
|     { | ||||
|         if (index < maxSize && index >= 0) | ||||
|         { | ||||
|             return true; | ||||
|         } | ||||
|         else  | ||||
|         { | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|     public int addItemAt(int index, Item itemType,int count)  | ||||
|     { | ||||
|  | ||||
|         if (indexIsInRange(index) && (inventory[index].ItemType == null || inventory[index].ItemType.id == itemType.id)) | ||||
|         { | ||||
|             if (inventory[index].ItemType == null)  | ||||
|             { | ||||
|                 inventory[index].ItemType = Instantiate(itemType); | ||||
|             } | ||||
|  | ||||
|             if (inventory[index].MaxItems == inventory[index].Count)  | ||||
|             { | ||||
|                 return count; | ||||
|             } | ||||
|             else if (inventory[index].MaxItems >= inventory[index].Count + count) | ||||
|             { | ||||
|                 inventory[index].addItem(count); | ||||
|                 return 0; | ||||
|             } else  | ||||
|             { | ||||
|                 int rest = count - inventory[index].MaxItems - inventory[index].Count; | ||||
|                 inventory[index].addItem(inventory[index].MaxItems - inventory[index].Count); | ||||
|                 return rest; | ||||
|             } | ||||
|         } | ||||
|         else  | ||||
|         { | ||||
|             //Wrong item or index not in range | ||||
|             return -1; | ||||
|         } | ||||
|     } | ||||
|     public int removeItemAt(int index, int count) | ||||
|     { | ||||
|  | ||||
|         if (inventory[index].ItemType != null && indexIsInRange(index)) | ||||
|         { | ||||
|             for (int i = 0; i < count; i++) | ||||
|             { | ||||
|                 if (!inventory[index].removeItem()) | ||||
|                 { | ||||
|                     return count - i; | ||||
|                 } | ||||
|             } | ||||
|             return 0; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             //index not in range | ||||
|             return -1; | ||||
|         } | ||||
|     } | ||||
|     /* | ||||
|     Item findItem(Item itemType)  | ||||
|     { | ||||
|         for (int i = 0; i < items.Length; i++) | ||||
|         { | ||||
|             if (items[i] == null) | ||||
|             { | ||||
|                 return i; | ||||
|             } | ||||
|         } | ||||
|         return -1; | ||||
|     } | ||||
|     bool removeItem(Item item,int count = 1)  | ||||
|     { | ||||
|  | ||||
|         return true; | ||||
|     } | ||||
|     bool removeItemAt(int index,int count = -1) | ||||
|     { | ||||
|          | ||||
|     } | ||||
|     Item getItemAt(int index)  | ||||
|     { | ||||
|         if (index < maxSize && index >= 0) | ||||
|         { | ||||
|             return items[index]; | ||||
|         } | ||||
|         else  | ||||
|         { | ||||
|             return null; | ||||
|         } | ||||
|          | ||||
|     } | ||||
|     */ | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/Inventory.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/Inventory.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: ee2fcb6d299019740bbede78bbc6a1d7 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										33
									
								
								Assets/Scripts/Inventory/InventoryController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								Assets/Scripts/Inventory/InventoryController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class InventoryController : MonoBehaviour | ||||
| { | ||||
|     [SerializeField] Item item1;// not needed | ||||
|     [SerializeField] Item item2;// not needed | ||||
|     public Inventory inventory; | ||||
|     [SerializeField] UI_Inventory uiInventory; | ||||
|     private void Awake() | ||||
|     { | ||||
|         inventory = transform.GetComponent<Inventory>(); | ||||
|         inventory.createEmptyInventory(5); | ||||
|         Debug.Log(inventory.addItemAt(0, item1, 8)); | ||||
|         Debug.Log(inventory.addItemAt(0, item1, 1)); | ||||
|         Debug.Log(inventory.addItemAt(3, item2, 15)); | ||||
|         Debug.Log(inventory.addItemAt(0, item2, 15)); | ||||
|         Debug.Log(inventory.getInventory[0].Count); | ||||
|  | ||||
|         uiInventory.setInventory(inventory); | ||||
|     } | ||||
|     void Start() | ||||
|     { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     // Update is called once per frame | ||||
|     void Update() | ||||
|     { | ||||
|  | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/InventoryController.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/InventoryController.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: cff3b5ce3863a7442a86b400700ff131 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										12
									
								
								Assets/Scripts/Inventory/Item.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Assets/Scripts/Inventory/Item.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| [CreateAssetMenu(fileName = "Item", menuName = "Items/Item", order = 1)] | ||||
| public class Item : ScriptableObject | ||||
| { | ||||
|     public new string name; | ||||
|     public int id; | ||||
|     public bool isStackable; | ||||
|     public Sprite sprite; | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/Item.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/Item.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: de55e2e3081d4ee4a9d0737e718a5b3b | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										19
									
								
								Assets/Scripts/Inventory/ItemAtlas.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Assets/Scripts/Inventory/ItemAtlas.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class ItemAtlas : MonoBehaviour | ||||
| { | ||||
|     public static ItemAtlas Instance { get; private set; } | ||||
|  | ||||
|     private void Awake() | ||||
|     { | ||||
|         Instance = this; | ||||
|     } | ||||
|     enum ItemTypes { | ||||
|     STONE,TOMATO | ||||
|      | ||||
|     } | ||||
|     public Sprite stoneSprite; | ||||
|     public Sprite woodSprite; | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/ItemAtlas.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/ItemAtlas.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 362c4fe8229e41c46b489ad07bfa9133 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										8
									
								
								Assets/Scripts/Inventory/Items.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/Scripts/Inventory/Items.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: db274161008cc6a4a8c92d42b6a81832 | ||||
| folderAsset: yes | ||||
| DefaultImporter: | ||||
|   externalObjects: {} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										18
									
								
								Assets/Scripts/Inventory/Items/Stone.asset
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Assets/Scripts/Inventory/Items/Stone.asset
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| %YAML 1.1 | ||||
| %TAG !u! tag:unity3d.com,2011: | ||||
| --- !u!114 &11400000 | ||||
| MonoBehaviour: | ||||
|   m_ObjectHideFlags: 0 | ||||
|   m_CorrespondingSourceObject: {fileID: 0} | ||||
|   m_PrefabInstance: {fileID: 0} | ||||
|   m_PrefabAsset: {fileID: 0} | ||||
|   m_GameObject: {fileID: 0} | ||||
|   m_Enabled: 1 | ||||
|   m_EditorHideFlags: 0 | ||||
|   m_Script: {fileID: 11500000, guid: de55e2e3081d4ee4a9d0737e718a5b3b, type: 3} | ||||
|   m_Name: Stone | ||||
|   m_EditorClassIdentifier:  | ||||
|   name: Stone | ||||
|   id: 0 | ||||
|   isStackable: 1 | ||||
|   sprite: {fileID: 21300000, guid: 475f050511847c0499c1ca4f47fcaefa, type: 3} | ||||
							
								
								
									
										8
									
								
								Assets/Scripts/Inventory/Items/Stone.asset.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/Scripts/Inventory/Items/Stone.asset.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: e04039a6cbed5e5439b605aeee90485f | ||||
| NativeFormatImporter: | ||||
|   externalObjects: {} | ||||
|   mainObjectFileID: 11400000 | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										21
									
								
								Assets/Scripts/Inventory/Items/Tomato.asset
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Assets/Scripts/Inventory/Items/Tomato.asset
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| %YAML 1.1 | ||||
| %TAG !u! tag:unity3d.com,2011: | ||||
| --- !u!114 &11400000 | ||||
| MonoBehaviour: | ||||
|   m_ObjectHideFlags: 0 | ||||
|   m_CorrespondingSourceObject: {fileID: 0} | ||||
|   m_PrefabInstance: {fileID: 0} | ||||
|   m_PrefabAsset: {fileID: 0} | ||||
|   m_GameObject: {fileID: 0} | ||||
|   m_Enabled: 1 | ||||
|   m_EditorHideFlags: 0 | ||||
|   m_Script: {fileID: 11500000, guid: 0f6fecdcda7efd84baa350f4c41432a6, type: 3} | ||||
|   m_Name: Tomato | ||||
|   m_EditorClassIdentifier:  | ||||
|   name: Tomato | ||||
|   id: 1 | ||||
|   isStackable: 1 | ||||
|   sprite: {fileID: 21300000, guid: 5ee3152fa9663e94092921b0bd6b4821, type: 3} | ||||
|   health: 15 | ||||
|   water: 10 | ||||
|   regeneration: 2 | ||||
							
								
								
									
										8
									
								
								Assets/Scripts/Inventory/Items/Tomato.asset.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/Scripts/Inventory/Items/Tomato.asset.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: a29513e23691eb0408e3b0e1a8e99148 | ||||
| NativeFormatImporter: | ||||
|   externalObjects: {} | ||||
|   mainObjectFileID: 11400000 | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										43
									
								
								Assets/Scripts/Inventory/Slot.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								Assets/Scripts/Inventory/Slot.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class Slot | ||||
| { | ||||
|      | ||||
|     Item item; | ||||
|     int maxItems; | ||||
|     int count; | ||||
|     public Slot(int maxItems) | ||||
|     { | ||||
|         item = null; | ||||
|         this.maxItems = maxItems; | ||||
|         count = 0; | ||||
|  | ||||
|     } | ||||
|     public bool removeItem()  | ||||
|     { | ||||
|         if (count > 0) | ||||
|         { | ||||
|             count--; | ||||
|             if (count == 0)  | ||||
|             { | ||||
|                 item = null; | ||||
|             } | ||||
|             return true; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|     public bool addItem(int count = 1) | ||||
|     { | ||||
|         this.count += count; | ||||
|         return true; | ||||
|  | ||||
|     } | ||||
|     public Item ItemType { get => item; set => item = value; } | ||||
|     public int Count { get => count; set => count = value; } | ||||
|     public int MaxItems { get => maxItems; set => maxItems = value; } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/Slot.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/Slot.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 497c0b5449a4c3141b273db4d97546c3 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										46
									
								
								Assets/Scripts/Inventory/UI_Inventory.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								Assets/Scripts/Inventory/UI_Inventory.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.UI; | ||||
|  | ||||
| public class UI_Inventory : MonoBehaviour | ||||
| { | ||||
|     Inventory playerInventory; | ||||
|     public Transform inventoryContainer; | ||||
|     public Transform itemSlotTemplate; | ||||
|     private void Awake() | ||||
|     { | ||||
|         //itemSlotTemplate = inventoryContainer.Find("containerTemplate"); | ||||
|     } | ||||
|     public void setInventory(Inventory inventory)  | ||||
|     { | ||||
|         playerInventory = inventory; | ||||
|         updateInventory(); | ||||
|     } | ||||
|     private void updateInventory() | ||||
|     { | ||||
|         int x = 0; | ||||
|         int y = 0; | ||||
|         float slotSize = 100; | ||||
|         foreach (Slot slot in playerInventory.getInventory)  | ||||
|         { | ||||
|             RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent<RectTransform>(); | ||||
|             itemSlotRectTransform.anchoredPosition = new Vector2(x* slotSize,y * slotSize); | ||||
|             itemSlotRectTransform.gameObject.SetActive(true); | ||||
|              | ||||
|  | ||||
|             if (slot.ItemType != null)  | ||||
|             { | ||||
|                 Transform Item = itemSlotRectTransform.Find("ItemTemplate"); | ||||
|                 Item.gameObject.SetActive(true); | ||||
|                 Item.GetComponent<Image>().sprite = slot.ItemType.sprite; | ||||
|             } | ||||
|             x++; | ||||
|             if (x > 3)  | ||||
|             { | ||||
|                 x = 0; | ||||
|                 y--; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Inventory/UI_Inventory.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Inventory/UI_Inventory.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 2270781166138a84fa19eefc3f92bda6 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user
	 julius
					julius