From 1f46626c1d98f37b1fdb65abf6f4dea271494689 Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Tue, 01 Apr 2025 15:53:40 +0000
Subject: [PATCH] fixed ruletileset

---
 Assets/Scripts/Inventory/Inventory.cs |   48 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs
index a8a59b5..f622519 100644
--- a/Assets/Scripts/Inventory/Inventory.cs
+++ b/Assets/Scripts/Inventory/Inventory.cs
@@ -1,3 +1,4 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
@@ -18,38 +19,66 @@
         {
             Destroy(gameObject);
         }
+        if (SaveSystem.isGameLoaded)
+        {
+            LoadInventory();
+        }
+    }
+
+    private void LoadInventory()
+    {
+        SaveDataPlayer save = SaveSystem.LoadPlayer();
+        if (save != null)
+        {
+            foreach (SaveDataInventorySlot item in save.inventoryItems)
+            {
+                Item loadedItem = ItemDatabase.Instance.GetItem(item.itemId);
+                if (loadedItem != null)
+                {
+                    items.Add(new InventorySlot(loadedItem, item.quantity));
+                }
+                else
+                {
+                    Debug.LogError($"Failed to load item with ID: {item.itemId}");
+                }
+            }
+        }
     }
 
     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;
-                return true;
             }
             else
             {
                 int remainingQuantity = item.maxStackSize - existingSlot.quantity;
                 existingSlot.quantity = item.maxStackSize;
-                return AddItem(item, quantity - remainingQuantity);
+                //TODO: Show inventory overflow message
+                Debug.Log("Inventory overflow");
+                existingSlot.quantity += remainingQuantity;
             }
+
         }
         else
         {
-            if (items.Count < maxInventorySize)
-            {
-                items.Add(new InventorySlot(item, quantity));
-                return true;
-            }
-            return false;
+            //Add new item to inventory
+            items.Add(new InventorySlot(item, quantity));
         }
+        GameStateEvents.inventoryChanged?.Invoke();
+        return true;
     }
 
     public void RemoveItem(Item item, int quantity = 1)
@@ -67,6 +96,7 @@
                 items.Remove(existingSlot);
             }
         }
+        GameStateEvents.inventoryChanged?.Invoke();
     }
 }
 

--
Gitblit v1.9.3