evershade/Assets/Scripts/Managers/Item/BoxManager.cs

192 lines
6.1 KiB
C#

using System.Collections.Generic;
using Data.ScriptableObjects;
using Data.ScriptableObjects.World;
using FishNet.Object;
using Types.Items;
using Types.UI;
using UnityEngine;
// InventoryManager.cs
namespace Managers.Item
{
public class BoxManager : NetworkBehaviour, IItemManager<GameSlot>
{
public delegate void ActiveBoxChangedHandler(BoxInteractable boxInteractable);
public static BoxManager Instance;
public readonly Dictionary<int, BoxInteractable> Boxes = new();
public BoxInteractable ActiveBoxInteractable { get; set; }
private void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(gameObject);
}
private void Start()
{
Debug.Log("InventoryManager Start");
Initialize();
}
public void RemoveSlot(int slotId)
{
ActiveBoxInteractable.Slots.Remove(slotId);
}
public GameItem GetItemFromSlot(int slotId)
{
return ActiveBoxInteractable.Slots.TryGetValue(slotId, out var slot) ? slot.Item : null;
}
public void RemoveItemFromSlot(int slotId)
{
if (ActiveBoxInteractable.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 ActiveBoxInteractable.Slots.Values)
if (slot.IsEmpty())
{
slot.item = item.GameItem;
Debug.Log("Added item to slot: " + slot.position + " / " + item.GameItem.itemName + " / " +
item.GameItem.quantity);
return;
}
Debug.LogWarning("No empty slots available to add item: " + item.GameItem.itemName);*/
}
public void AddItemToSlot(GameItem item, int slotId)
{
/*if (ActiveBoxInteractable.Slots.TryGetValue(slotId, out var slot))
slot.item = item.GameItem;
else
Debug.LogWarning("Slot not found: " + slotId);*/
}
public GameSlot GetSlot(int slotId)
{
return ActiveBoxInteractable.Slots.GetValueOrDefault(slotId, null);
}
public void AddSlotServerRpc(GameSlot gameSlot)
{
ActiveBoxInteractable.AddSlot(gameSlot);
}
public void SwapItems(int slotId1, int slotId2)
{
if (ActiveBoxInteractable.Slots.TryGetValue(slotId1, out var slot1) &&
ActiveBoxInteractable.Slots.TryGetValue(slotId2, out var slot2))
(slot1.Item, slot2.Item) = (slot2.Item, slot1.Item);
}
// public void AddItemToOpenSlot(SyncGameItem syncGameItem)
// {
// var item = syncGameItem.GameItem;
// foreach (var slot in ActiveBoxInteractable.Slots.Values)
// if (slot.IsEmpty())
// {
// slot.item = item;
// Debug.Log("Added item to slot: " + slot.position + " / " + item.itemName + " / " + item.quantity);
// return;
// }
//
// Debug.LogWarning("No empty slots available to add item: " + item.itemName);
// }
/*public void AddItemToSlot(GameItem item, int slotId)
{
if (ActiveBoxInteractable.Slots.TryGetValue(slotId, out var slot))
slot.item = item;
else
Debug.LogWarning("Slot not found: " + slotId);
}*/
public event ActiveBoxChangedHandler OnActiveBoxChanged;
public void RemoveItemFromSlot(GameItem item)
{
foreach (var slot in ActiveBoxInteractable.Slots.Values)
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 void RemoveItemInSlot(int slotId)
{
if (ActiveBoxInteractable.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<BoxInteractable>();
box.id = 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<SlotSo>();
// need to redo this to use slot prefab and then add slot data to prefab?
var slot = new GameSlot();
Debug.LogWarning("Added slots to box: " + j + " / 30 slots added");
slot.Position = j;
slot.SlotType = SlotType.Box;
slot.Item = null;
box.AddSlot(slot);
}
// SetActiveBox(i);
// add random inventory to box
for (var j = 0; j < 10; j++)
{
var item = ItemDatabase.Instance.AllItems[
Random.Range(0, ItemDatabase.Instance.AllItems.Count - 1)];
box.AddItemToOpenSlot(item);
}
}
Debug.Log("Completed Initialized items in BoxManager");
}
public void CreateBox(BoxInteractable boxInteractable)
{
Boxes[boxInteractable.id] = boxInteractable;
}
public void SetActiveBox(int boxId)
{
Debug.Log("Active box set to: " + boxId);
ActiveBoxInteractable = Boxes.GetValueOrDefault(boxId);
// Trigger the event
OnActiveBoxChanged?.Invoke(ActiveBoxInteractable);
}
}
}