From d2ab30e7a69bfe7efda63ae75812207377917bd3 Mon Sep 17 00:00:00 2001 From: miepzerino <o.skotnik@gmail.com> Date: Sun, 30 Mar 2025 18:50:27 +0000 Subject: [PATCH] Merge branch 'Flexalon-UI-Layouts' into develop --- Assets/Scripts/Inventory/Inventory.cs | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 89 insertions(+), 0 deletions(-) diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs new file mode 100644 index 0000000..edb224e --- /dev/null +++ b/Assets/Scripts/Inventory/Inventory.cs @@ -0,0 +1,89 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Inventory : MonoBehaviour +{ + public static Inventory Instance { get; private set; } + public List<InventorySlot> items = new List<InventorySlot>(); + public int maxInventorySize = 20; + + private void Awake() + { + if (Instance == null) + { + Instance = this; + } + else + { + Destroy(gameObject); + } + } + + public bool AddItem(Item item, int quantity = 1) + { + if (items.Count >= maxInventorySize && !item.isStackable) + { + //TODO: Show inventory full message + Debug.Log("Inventory is full"); + return false; + } + + InventorySlot existingSlot = items.Find(slot => slot.item.itemName == item.itemName); + if (existingSlot != null && item.isStackable) + { + //Add to existing stack + if (existingSlot.quantity + quantity <= item.maxStackSize) + { + existingSlot.quantity += quantity; + } + else + { + int remainingQuantity = item.maxStackSize - existingSlot.quantity; + existingSlot.quantity = item.maxStackSize; + //TODO: Show inventory overflow message + Debug.Log("Inventory overflow"); + existingSlot.quantity += remainingQuantity; + } + + } + else + { + //Add new item to inventory + items.Add(new InventorySlot(item, quantity)); + } + GameStateEvents.inventoryChanged?.Invoke(); + return true; + } + + public void RemoveItem(Item item, int quantity = 1) + { + InventorySlot existingSlot = items.Find(slot => slot.item.itemName == item.itemName); + + if (existingSlot != null) + { + if (existingSlot.quantity > quantity) + { + existingSlot.quantity -= quantity; + } + else + { + items.Remove(existingSlot); + } + } + GameStateEvents.inventoryChanged?.Invoke(); + } +} + +[System.Serializable] +public class InventorySlot +{ + public Item item; + public int quantity; + + public InventorySlot(Item item, int quantity) + { + this.item = item; + this.quantity = quantity; + } +} -- Gitblit v1.9.3