161 lines
4.3 KiB
C#
161 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// InventoryManager.cs
|
|
public class BoxManager : MonoBehaviour, IItemManager<Slot>
|
|
{
|
|
public delegate void ActiveBoxChangedHandler(Box box);
|
|
|
|
public static BoxManager Instance;
|
|
|
|
public readonly Dictionary<int, Box> Boxes = new();
|
|
public Box ActiveBox { get; set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("InventoryManager Start");
|
|
Initialize();
|
|
}
|
|
|
|
public void AddItemToSlot(GameItem item, int slotId)
|
|
{
|
|
if (ActiveBox.Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = item;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void RemoveSlot(int slotId)
|
|
{
|
|
ActiveBox.Slots.Remove(slotId);
|
|
}
|
|
|
|
public GameItem GetItemFromSlot(int slotId)
|
|
{
|
|
return ActiveBox.Slots.TryGetValue(slotId, out var slot) ? slot.item : null;
|
|
}
|
|
|
|
public void AddItemToOpenSlot(GameItem item)
|
|
{
|
|
foreach (var slot in ActiveBox.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 RemoveItemFromSlot(int slotId)
|
|
{
|
|
if (ActiveBox.Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = null;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public Slot GetSlot(int slotId)
|
|
{
|
|
return ActiveBox.Slots.GetValueOrDefault(slotId, null);
|
|
}
|
|
|
|
public void AddSlot(Slot slot)
|
|
{
|
|
ActiveBox.AddSlot(slot);
|
|
}
|
|
|
|
public void SwapItems(int slotId1, int slotId2)
|
|
{
|
|
if (ActiveBox.Slots.TryGetValue(slotId1, out var slot1) &&
|
|
ActiveBox.Slots.TryGetValue(slotId2, out var slot2))
|
|
(slot1.item, slot2.item) = (slot2.item, slot1.item);
|
|
}
|
|
|
|
public event ActiveBoxChangedHandler OnActiveBoxChanged;
|
|
|
|
public void RemoveItemFromSlot(GameItem item)
|
|
{
|
|
foreach (var slot in ActiveBox.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 (ActiveBox.Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = null;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
// var testBoxCount = 5;
|
|
//
|
|
// for (var i = 0; i < testBoxCount; i++)
|
|
// {
|
|
// var box = ScriptableObject.CreateInstance<Box>();
|
|
// box.boxId = i;
|
|
// Boxes[i] = box;
|
|
//
|
|
// // box created
|
|
// Debug.Log("Created box: " + i);
|
|
//
|
|
// // add 30 slots to each box
|
|
// for (var j = 0; j < 30; j++)
|
|
// {
|
|
// var slot = ScriptableObject.CreateInstance<Slot>();
|
|
// Debug.LogWarning("Added slots to box: " + j + " / 30 slots added");
|
|
// slot.slotId = j;
|
|
// slot.slotType = Slot.SlotType.Box;
|
|
// slot.item = null;
|
|
// box.AddSlot(slot);
|
|
// }
|
|
//
|
|
//
|
|
// SetActiveBox(i);
|
|
// // add random inventory to box
|
|
// for (var j = 0; j < 10; j++)
|
|
// {
|
|
// var item = GameItemManager.Instance.gameItems[
|
|
// Random.Range(0, GameItemManager.Instance.gameItems.Count - 1)];
|
|
//
|
|
//
|
|
// box.AddItemToOpenSlot(item);
|
|
// }
|
|
// }
|
|
|
|
Debug.Log("Completed Initialized items in BoxManager");
|
|
}
|
|
|
|
|
|
public void SetActiveBox(int boxId)
|
|
{
|
|
Debug.Log("Active box set to: " + boxId);
|
|
ActiveBox = Boxes.GetValueOrDefault(boxId);
|
|
|
|
// Trigger the event
|
|
OnActiveBoxChanged?.Invoke(ActiveBox);
|
|
}
|
|
} |