131 lines
3.7 KiB
C#
131 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using Managers;
|
|
using UnityEngine;
|
|
|
|
|
|
// InventoryManager.cs
|
|
public class InventoryManager : MonoBehaviour
|
|
{
|
|
public List<InventorySlot> inventorySlots = new();
|
|
public PlayerManager playerManager;
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("InventoryManager Start");
|
|
InitializeInventory();
|
|
}
|
|
|
|
private void AddSlot(InventorySlot slot)
|
|
{
|
|
inventorySlots.Add(slot);
|
|
}
|
|
|
|
public InventorySlot GetSlot(int slotId)
|
|
{
|
|
foreach (var slot in inventorySlots)
|
|
if (slot.slotId == slotId)
|
|
{
|
|
Debug.Log("Got slot: " + slotId);
|
|
return slot;
|
|
}
|
|
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
return null;
|
|
}
|
|
|
|
private void RemoveSlot(InventorySlot slot)
|
|
{
|
|
inventorySlots.Remove(slot);
|
|
}
|
|
|
|
public void RemoveItemFromSlot(GameItem item)
|
|
{
|
|
foreach (var slot in inventorySlots)
|
|
if (slot.item == item)
|
|
{
|
|
slot.item = null;
|
|
Debug.Log("Removed item from slot: " + item.itemName);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("Item not found in any slot: " + item.itemName);
|
|
}
|
|
|
|
public GameItem GetItemFromSlot(int slotId)
|
|
{
|
|
Debug.Log("Getting item from slot: " + slotId);
|
|
foreach (var slot in inventorySlots)
|
|
if (slot.slotId == slotId && slot.item != null)
|
|
{
|
|
Debug.Log("Got item from slot: " + slot.item.itemName);
|
|
return slot.item;
|
|
}
|
|
|
|
Debug.LogWarning("Item not found in slot: " + slotId);
|
|
return null;
|
|
}
|
|
|
|
public void SetItemInSlot(int slotId, GameItem item)
|
|
{
|
|
Debug.Log("Setting item in slot: " + slotId + " / " + item?.itemName ?? "null");
|
|
foreach (var slot in inventorySlots)
|
|
if (slot.slotId == slotId)
|
|
{
|
|
if (item == null)
|
|
{
|
|
Debug.Log("Item is null, removing item from slot: " + slotId);
|
|
slot.item = null;
|
|
return;
|
|
}
|
|
|
|
slot.item = item;
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("Item not found in slot: " + slotId);
|
|
}
|
|
|
|
public void SwapItems(int slotId1, int slotId2)
|
|
{
|
|
var item1 = GetItemFromSlot(slotId1);
|
|
var item2 = GetItemFromSlot(slotId2);
|
|
|
|
SetItemInSlot(slotId1, item2);
|
|
SetItemInSlot(slotId2, item1);
|
|
}
|
|
|
|
public void AddItemToOpenSlot(GameItem item)
|
|
{
|
|
foreach (var slot in inventorySlots)
|
|
if (slot.IsEmpty())
|
|
{
|
|
slot.slotId = inventorySlots.IndexOf(slot);
|
|
slot.item = item;
|
|
Debug.Log("Added item to slot: " + slot.slotId + " / " + item.itemName);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("No empty slots available to add item: " + item.itemName);
|
|
}
|
|
|
|
public void InitializeInventory()
|
|
{
|
|
// create player slots for inventory
|
|
for (var i = 0; i < playerManager.playerData.inventorySize; i++)
|
|
{
|
|
var slot = ScriptableObject.CreateInstance<InventorySlot>();
|
|
slot.slotId = i;
|
|
slot.item = null;
|
|
AddSlot(slot);
|
|
}
|
|
|
|
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[0]);
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[1]);
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[2]);
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[3]);
|
|
|
|
|
|
Debug.Log("Completed Initialized items in InventoryManager");
|
|
}
|
|
} |