150 lines
4.4 KiB
C#
150 lines
4.4 KiB
C#
using System.Collections.Generic;
|
|
using FishNet.Object;
|
|
using Types;
|
|
using Types.Items;
|
|
using Types.UI;
|
|
using UnityEngine;
|
|
|
|
namespace Managers.Item
|
|
{
|
|
public class EquipmentManager : NetworkBehaviour, IItemManager<GameSlot>
|
|
{
|
|
// public readonly SyncDictionary<int, Slot> Slots = new();
|
|
|
|
public PlayerManager playerManager;
|
|
public readonly Dictionary<int, GameSlot> Slots = new();
|
|
|
|
public GameSlot ActiveGameSlot { get; set; }
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("EquipmentManager 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 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 AddSlotServerRpc(GameSlot gameSlot)
|
|
{
|
|
Slots[gameSlot.Position] = gameSlot;
|
|
}
|
|
|
|
public GameSlot GetSlot(int slotId)
|
|
{
|
|
return Slots.GetValueOrDefault(slotId, null);
|
|
}
|
|
|
|
public void AddItemToOpenSlot(GameItem item)
|
|
{
|
|
foreach (var slot in 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);
|
|
}
|
|
|
|
// [ServerRpc]
|
|
public void AddItemToSlot(GameItem item, int slotId)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.Item = item;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}
|
|
|
|
/*[ServerRpc]
|
|
public void AddItemToOpenSlot(SyncGameItem syncGameItem)
|
|
{
|
|
var item = syncGameItem.GameItem;
|
|
|
|
foreach (var slot in 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(SyncGameItem item, int slotId)
|
|
{
|
|
if (Slots.TryGetValue(slotId, out var slot))
|
|
slot.item = item;
|
|
else
|
|
Debug.LogWarning("Slot not found: " + slotId);
|
|
}*/
|
|
|
|
|
|
/*public void RemoveItemFromSlot(SyncGameItem 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)
|
|
{
|
|
// 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)
|
|
{
|
|
ActiveGameSlot = Slots[slotId];
|
|
}
|
|
|
|
|
|
public void Initialize(GamePlayer playerData)
|
|
{
|
|
Debug.Log("Initializing EquipmentManager");
|
|
LoadSlots(playerData.EquipmentSlots);
|
|
Debug.Log("Completed Initialized items in EquipmentManager");
|
|
}
|
|
|
|
public void LoadSlots(Dictionary<int, GameSlot> slots)
|
|
{
|
|
// clear existing slots
|
|
Slots.Clear();
|
|
ActiveGameSlot = null;
|
|
foreach (var slot in slots) AddSlotServerRpc(slot.Value);
|
|
}
|
|
}
|
|
} |