167 lines
4.7 KiB
C#
167 lines
4.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// InventoryManager.cs
|
|
public class EquipmentManager : MonoBehaviour, IItemManager<Slot>
|
|
{
|
|
public static EquipmentManager Instance;
|
|
public readonly Dictionary<int, Slot> Slots = new();
|
|
|
|
public Slot ActiveSlot { get; set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("EquipmentManager Start");
|
|
Initialize();
|
|
}
|
|
|
|
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 RemoveSlot(int slotId)
|
|
{
|
|
Slots.Remove(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 AddSlot(Slot slot)
|
|
{
|
|
Slots[slot.slotId] = slot;
|
|
}
|
|
|
|
public Slot GetSlot(int slotId)
|
|
{
|
|
return Slots.GetValueOrDefault(slotId, null);
|
|
}
|
|
|
|
|
|
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 SetItemInSlot(int slotId, GameItem item)
|
|
{
|
|
// TODO: check if item is valid for slot
|
|
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];
|
|
}
|
|
|
|
|
|
public void Initialize()
|
|
{
|
|
// create player slots for inventory
|
|
var equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 0;
|
|
equipmentSlot.item = null;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Helmet);
|
|
AddSlot(equipmentSlot);
|
|
|
|
equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 1;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Accessory);
|
|
equipmentSlot.item = null;
|
|
AddSlot(equipmentSlot);
|
|
|
|
// feet
|
|
equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 2;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Armor);
|
|
equipmentSlot.item = null;
|
|
AddSlot(equipmentSlot);
|
|
|
|
|
|
equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 3;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Accessory);
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Ring);
|
|
equipmentSlot.item = null;
|
|
AddSlot(equipmentSlot);
|
|
|
|
equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 4;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Boots);
|
|
equipmentSlot.item = null;
|
|
AddSlot(equipmentSlot);
|
|
|
|
equipmentSlot = ScriptableObject.CreateInstance<Slot>();
|
|
equipmentSlot.slotId = 5;
|
|
equipmentSlot.slotType = Slot.SlotType.Equipment;
|
|
equipmentSlot.allowedItemTypes.Add(GameItem.ItemType.Ring);
|
|
equipmentSlot.item = null;
|
|
AddSlot(equipmentSlot);
|
|
|
|
Debug.Log("Completed Initialized items in EquipmentManager");
|
|
}
|
|
} |