Scriptum/Assets/Scripts/REFACTORING/Application/Shared/Manager/UI/UIWarehouseManager.cs
2022-11-07 23:06:06 +01:00

137 lines
3.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public abstract class UIWarehouseManager : UIBaseManager<KeyValuePair<int, Item>>
{
public virtual int SLOTS_NUMBER => 0;
public static new UIWarehouseManager Instance { get; private set; }
/*
* not sure why but childrens of this class dont interfere on parents Awake
*/
public void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
/// <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>
public List<KeyValuePair<int, Item>> FindItemInWarehouse(int itemId)
{
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>
public List<KeyValuePair<int, Item>> FindItemInWarehouseByName(string itemName)
{
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>
public void Add(KeyValuePair<int, Item> itemOnSlot)
{
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)
{
Debug.Log($" Added {itemOnSlot.Value} on position {itemOnSlot.Key}");
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>
public void Add(Item item)
{
if(IsFull())
throw new System.Exception($"Warehouse is full!!!");
// find first empty position / slot
var max = Elements.Max(itemSlot => itemSlot.Key);
base.Add(new KeyValuePair<int, Item>(max, item));
}
public void RemoveByPosition(int keyPosition)
{
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);
}
public int RemoveByItemId(int itemId)
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Id == itemId);
}
public int RemoveByItemName(string itemName)
{
return Elements.RemoveAll(itemSlot => itemSlot.Value.Name == itemName);
}
public bool IsFull()
{
return Elements.Count() == SLOTS_NUMBER;
}
private bool CheckIfSlotIsInRange(int slotNumber)
{
return slotNumber < SLOTS_NUMBER;
}
private bool CheckIfSlotExists(int slotNumber)
{
return Elements.Any(itemSlot => itemSlot.Key == slotNumber);
}
/// <summary>
/// Check if slot is empty in local list
/// </summary>
/// <returns></returns>
private bool CheckIfPositionIsEmpty(int slotNumber)
{
if(CheckIfSlotExists(slotNumber)) {
return Elements.Where(itemSlot => itemSlot.Key == slotNumber).First().Value == null;
}
return true;
}
}