146 lines
4.0 KiB
C#
146 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using Managers;
|
|
using UnityEngine;
|
|
|
|
// InventoryManager.cs
|
|
public class InventoryManager : MonoBehaviour, IItemManager<Slot>
|
|
{
|
|
public static InventoryManager Instance;
|
|
public readonly Dictionary<int, Slot> Slots = new();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("InventoryManager Start");
|
|
InitializeInventory(PlayerManager.Instance.playerData);
|
|
}
|
|
|
|
/*public override InventorySlot GetSlot(int slotId)
|
|
{
|
|
return InventorySlots.GetValueOrDefault(slotId, null);
|
|
}*/
|
|
public Slot GetSlot(int slotId)
|
|
{
|
|
return Slots.GetValueOrDefault(slotId, null);
|
|
}
|
|
|
|
public GameItem GetItemFromSlot(int slotId)
|
|
{
|
|
return Slots.TryGetValue(slotId, out var slot) ? slot.item : null;
|
|
}
|
|
|
|
public void RemoveItemFromSlot(int slotId)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = null;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void AddItemToOpenSlot(GameItem item)
|
|
{
|
|
foreach (var slot in Slots.Values)
|
|
if (slot.IsEmpty())
|
|
{
|
|
slot.item = item;
|
|
Debug.Log("Added item to slot: " + slot.slotId + " / " + item.name);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("No empty slots available to add item: " + item.name);
|
|
}
|
|
|
|
public void AddItemToSlot(GameItem item, int slotId)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = item;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void SwapItems(int slotId1, int slotId2)
|
|
{
|
|
if (Slots.TryGetValue(slotId1, out var slot1) &&
|
|
Slots.TryGetValue(slotId2, out var slot2))
|
|
(slot1.item, slot2.item) = (slot2.item, slot1.item);
|
|
}
|
|
|
|
public void RemoveSlot(int slotId)
|
|
{
|
|
Slots.Remove(slotId);
|
|
}
|
|
|
|
public void AddSlot(Slot slot)
|
|
{
|
|
Slots[slot.slotId] = slot;
|
|
}
|
|
|
|
public void RemoveItemFromSlot(GameItem item)
|
|
{
|
|
foreach (var slot in Slots.Values)
|
|
if (slot.item == item)
|
|
{
|
|
slot.item = null;
|
|
Debug.Log("Removed item from slot: " + item.name);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("Item not found in any slot: " + item.name);
|
|
}
|
|
|
|
public void RemoveItemInSlot(int slotId)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = null;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void InitializeInventory(PlayerData playerData)
|
|
{
|
|
// create player slots for inventory
|
|
for (var i = 0; i < playerData.inventorySize; i++)
|
|
{
|
|
var slot = ScriptableObject.CreateInstance<Slot>();
|
|
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]);
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[4]);
|
|
AddItemToOpenSlot(GameItemManager.Instance.gameItems[5]);*/
|
|
|
|
// add up to ten items or stop on error
|
|
for (var i = 0; i < 10; i++)
|
|
try
|
|
{
|
|
var item = GameItemManager.Instance.gameItems[i];
|
|
if (item == null)
|
|
break;
|
|
AddItemToOpenSlot(item);
|
|
}
|
|
catch
|
|
{
|
|
Debug.Log("Error adding items to inventory slots. Stopped at: " + i + " items.");
|
|
}
|
|
|
|
|
|
Debug.Log("Completed Initialized items in InventoryManager");
|
|
}
|
|
} |