Scriptum/Assets/Scripts/REFACTORING/Application/Shared/Manager/UI/UIWarehouseManager.cs

120 lines
3.7 KiB
C#
Raw Normal View History

2022-11-06 21:34:17 +01:00
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
2022-11-24 03:03:30 +01:00
public abstract class UIWarehouseManager : UISlotPanelManager<IndexValuePair<int, EquippableItem>>
2022-11-06 21:34:17 +01:00
{
2022-11-24 03:03:30 +01:00
public override int SLOTS_NUMBER => 48;
2022-11-06 21:34:17 +01:00
public static new UIWarehouseManager Instance { get; protected set; }
2022-11-06 21:34:17 +01:00
/*
* not sure why but childrens of this class dont interfere on parents Awake
*/
/// <summary>
/// Function to find item in warehouser by its id, returns list of all slots where item occured
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
2022-11-24 03:03:30 +01:00
public override List<IndexValuePair<int, EquippableItem>> FindItemInWarehouse(int itemId)
2022-11-06 21:34:17 +01:00
{
return Elements.Where(item => item.Value.Id == itemId).ToList();
}
/// <summary>
/// Function to find item in warehouser by its name, returns list of all slots where item occured
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
2022-11-24 03:03:30 +01:00
public override List<IndexValuePair<int, EquippableItem>> FindItemInWarehouseByName(string itemName)
2022-11-06 21:34:17 +01:00
{
return Elements.Where(item => item.Value.name == itemName).ToList();
}
/// <summary>
/// Function (SetItemOnPosition) to add slot with its number and item to list (which will be mapped with panel content)
/// </summary>
/// <param name="itemOnSlot"></param>
2022-11-24 03:03:30 +01:00
public virtual void Add(IndexValuePair<int, EquippableItem> itemOnSlot)
2022-11-06 21:34:17 +01:00
{
if (!CheckIfSlotIsInRange(itemOnSlot.Key))
throw new System.Exception($"Slot number: {itemOnSlot.Key} is out of range, avaiable: {SLOTS_NUMBER} slots");
if (CheckIfSlotExists(itemOnSlot.Key) && !CheckIfPositionIsEmpty(itemOnSlot.Key))
RemoveByPosition(itemOnSlot.Key);
if(itemOnSlot.Value != null)
{
base.Add(itemOnSlot);
}
}
/// <summary>
/// Function to add item on first empty slot to list (which will be mapped with panel content)
/// </summary>
/// <param name="item"></param>
2022-11-24 03:03:30 +01:00
public virtual void Add(EquippableItem item)
2022-11-06 21:34:17 +01:00
{
if(IsFull())
throw new System.Exception($"Warehouse is full!!!");
// find first empty position / slot
2022-11-27 21:28:55 +01:00
var max = 0;
if(Elements.Count() > 0)
{
for (int i = 0; i < SLOTS_NUMBER; i++)
{
if (Elements.Where(el => el.Key == i && el.Value != null).Count() != 0)
continue;
max = i;
break;
}
}
2022-11-06 21:34:17 +01:00
2022-11-24 03:03:30 +01:00
base.Add(new IndexValuePair<int, EquippableItem>(max, item));
2022-11-06 21:34:17 +01:00
}
2022-11-24 03:03:30 +01:00
public override void RemoveByPosition(int keyPosition)
2022-11-06 21:34:17 +01:00
{
if (!CheckIfSlotExists(keyPosition))
return; // throw new System.Exception($"Slot with number: {keyPosition} don't exist");
Debug.Log($"Remove from position: {keyPosition}");
Elements.RemoveAll(itemSlot => itemSlot.Key == keyPosition);
}
2022-11-24 03:03:30 +01:00
public override int RemoveByItemId(int itemId)
2022-11-06 21:34:17 +01:00
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Id == itemId);
}
2022-11-24 03:03:30 +01:00
public override int RemoveByItemName(string itemName)
2022-11-06 21:34:17 +01:00
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Name == itemName);
}
2022-11-24 03:03:30 +01:00
protected override bool CheckIfSlotExists(int slotNumber)
2022-11-06 21:34:17 +01:00
{
return Elements.Any(itemSlot => itemSlot.Key == slotNumber);
}
/// <summary>
/// Check if slot is empty in local list
/// </summary>
/// <returns></returns>
2022-11-24 03:03:30 +01:00
protected override bool CheckIfPositionIsEmpty(int slotNumber)
2022-11-06 21:34:17 +01:00
{
if(CheckIfSlotExists(slotNumber)) {
return Elements.Where(itemSlot => itemSlot.Key == slotNumber).First().Value == null;
}
return true;
}
}