miepzerino
2025-04-01 f5e15fa93d84acbae6a26b86fddf20add38bb485
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
using System;
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);
        }
        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;
            }
            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;
    }
}