148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// InventoryManager.cs
|
|
public class ToolbarManager : MonoBehaviour, IItemManager<Slot>
|
|
{
|
|
public delegate void ActiveSlotChangedHandler();
|
|
|
|
public static ToolbarManager Instance;
|
|
|
|
// private static ToolbarManager Instance;
|
|
public readonly Dictionary<int, Slot> Slots = new();
|
|
private readonly Slot.SlotType slotType = Slot.SlotType.Toolbar;
|
|
|
|
public Slot ActiveSlot { get; set; }
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("ToolbarManager Start");
|
|
}
|
|
|
|
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.itemName + " / " + item.quantity);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("No empty slots available to add item: " + item.itemName);
|
|
}
|
|
|
|
|
|
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 AddSlot(Slot slot)
|
|
{
|
|
slot.slotType = slotType;
|
|
Slots[slot.slotId] = slot;
|
|
}
|
|
|
|
public void RemoveSlot(int slotId)
|
|
{
|
|
Slots.Remove(slotId);
|
|
}
|
|
|
|
public Slot GetSlot(int slotId)
|
|
{
|
|
return Slots[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 event ActiveSlotChangedHandler OnActiveSlotChanged;
|
|
|
|
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.itemName);
|
|
return;
|
|
}
|
|
|
|
Debug.LogWarning("Item not found in any slot: " + item.itemName);
|
|
}
|
|
|
|
public void SetItemInSlot(int slotId, GameItem item)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = item;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
public void SetActiveSlot(int slotId)
|
|
{
|
|
ActiveSlot = Slots[slotId];
|
|
OnActiveSlotChanged?.Invoke();
|
|
}
|
|
|
|
|
|
public void Initialize(PlayerData playerData)
|
|
{
|
|
// add toolbar hotkeys 1 to 0 on keyboard
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 1", KeyCode.Alpha1, () => SetActiveSlot(0));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 2", KeyCode.Alpha2, () => SetActiveSlot(1));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 3", KeyCode.Alpha3, () => SetActiveSlot(2));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 4", KeyCode.Alpha4, () => SetActiveSlot(3));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 5", KeyCode.Alpha5, () => SetActiveSlot(4));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 6", KeyCode.Alpha6, () => SetActiveSlot(5));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 7", KeyCode.Alpha7, () => SetActiveSlot(6));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 8", KeyCode.Alpha8, () => SetActiveSlot(7));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 9", KeyCode.Alpha9, () => SetActiveSlot(8));
|
|
HotkeyManager.Instance.RegisterHotkey("Activate Toolbar Slot 10", KeyCode.Alpha0, () => SetActiveSlot(9));
|
|
|
|
// create player slots for inventory
|
|
for (var i = 0; i < playerData.toolbarSize; i++) AddSlot(playerData.toolbarSlots[i]);
|
|
|
|
Debug.Log("Completed Initialized hotkeys in ToolbarManager");
|
|
}
|
|
|
|
public Slot GetActiveToolbarSlot()
|
|
{
|
|
return ActiveSlot;
|
|
}
|
|
} |