ItemContainer
user_3183455
csharp
a year ago
11 kB
24
Indexable
using System;
using UnityEngine;
namespace CuongMono.Item
{
[Serializable]
public abstract class ItemContainer<T> : IItemContainer<T> where T: Item
{
public IItemSlot<T>[] itemSlots;
public Action UpdateInventory = delegate { };
public ItemContainer(int slots) => this.itemSlots = new IItemSlot<T>[slots];
public void ResetItemSlots()
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
this.itemSlots[i].ResetSlot();
}
this.UpdateInventory.Invoke();
}
public IItemSlot<T> AddItem(IItemSlot<T> slot)
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item != slot.Item) continue;
if (this.itemSlots[i].DuranceLeft != slot.DuranceLeft) continue;
if (this.itemSlots[i].GetRemainingSlot() <= 0) continue;
if(slot.Quanity <= this.itemSlots[i].GetRemainingSlot())
{
this.itemSlots[i].Quanity += slot.Quanity;
slot.Quanity = 0;
this.UpdateInventory.Invoke();
return slot;
}
else
{
slot.Quanity -= this.itemSlots[i].GetRemainingSlot();
this.itemSlots[i].Quanity = this.itemSlots[i].Item.MaxStack;
}
}
for (int i = 0;i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item != null) continue;
this.itemSlots[i].Item = slot.Item;
this.itemSlots[i].DuranceLeft = slot.DuranceLeft;
if (slot.Quanity <= slot.Item.MaxStack)
{
this.itemSlots[i].Quanity = slot.Quanity;
slot.Quanity = 0;
this.UpdateInventory.Invoke();
return slot;
}
else
{
this.itemSlots[i].Quanity = this.itemSlots[i].Item.MaxStack;
slot.Quanity -= slot.Item.MaxStack;
}
}
this.UpdateInventory.Invoke();
return slot;
}
/// <summary>
/// (Make quite much garbage)
/// </summary>
public void AddSlot(int quanity)
{
if (quanity <= 0) { Debug.LogError("Invalid quanity"); return; }
int newLength = this.itemSlots.Length + quanity;
IItemSlot<T>[] newItemSlots = new IItemSlot<T>[newLength];
Array.Copy(this.itemSlots, newItemSlots, this.itemSlots.Length);
this.itemSlots = newItemSlots;
this.UpdateInventory.Invoke();
}
public IItemSlot<T> GetItemByIndex(int index)
{
return this.itemSlots[index];
}
/// <summary>
/// (Make garbage)
/// </summary>
public int GetTotalItem(IItemSlot<T> slot)
{
int total = 0;
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item != slot.Item) continue;
total += this.itemSlots[i].Quanity;
}
return total;
}
public bool HasItem(IItemSlot<T> slot)
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item != slot.Item) continue;
return true;
}
return false;
}
public bool HasItemType(ItemType type)
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item.Type != type) continue;
return true;
}
return false;
}
public bool HasRarity(Rarity rarity)
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item.Rarity != rarity) continue;
return true;
}
return false;
}
/// <summary>
/// (Make garbage)
/// </summary>
public void RemoveItem(IItemSlot<T> slot)
{
for (int i = 0; i < this.itemSlots.Length; i++)
{
if (this.itemSlots[i].Item != slot.Item) continue;
if (slot.Quanity <= itemSlots[i].Quanity)
{
this.itemSlots[i].Quanity -= slot.Quanity;
slot.Quanity = 0;
this.UpdateInventory.Invoke();
return;
}
else
{
slot.Quanity -= this.itemSlots[i].Quanity;
this.itemSlots[i].ResetSlot();
}
}
this.UpdateInventory.Invoke();
}
public void RemoveItemAtIndex(int index)
{
if (index < 0 || index >= this.itemSlots.Length) return;
this.itemSlots[index].ResetSlot();
this.UpdateInventory.Invoke();
}
/// <summary>
/// (Make garbage)
/// </summary>
public void SwapItem(int indexOne, int indexTwo)
{
//if (indexOne < 0 || indexOne >= this.itemSlots.Length) return;
//if (indexTwo < 0 || indexTwo >= this.itemSlots.Length) return;
IItemSlot<T> firstItem = this.itemSlots[indexOne];
IItemSlot<T> secondItem = this.itemSlots[indexTwo];
if (firstItem.Equals(secondItem))
{
if (secondItem.GetRemainingSlot() <= 0) return;
int secondSlotQuanityRemaining = secondItem.GetRemainingSlot();
if (firstItem.Quanity <= secondSlotQuanityRemaining)
{
this.itemSlots[indexTwo].Quanity += firstItem.Quanity;
this.itemSlots[indexOne].ResetSlot();
this.UpdateInventory.Invoke();
return;
}
else
{
this.itemSlots[indexOne].Quanity -= secondSlotQuanityRemaining;
this.itemSlots[indexTwo].Quanity = this.itemSlots[indexTwo].Item.MaxStack;
this.UpdateInventory.Invoke();
return;
}
}
this.itemSlots[indexOne] = secondItem;
this.itemSlots[indexTwo] = firstItem;
this.UpdateInventory.Invoke();
}
/// <summary>
/// (Make garbage)
/// </summary>
public IItemSlot<T> SwapItemLeftClick(IItemSlot<T> cursorItem, int indexItem)
{
//Try not to make garbage
IItemSlot<T> cursorItemClone = cursorItem.Clone();
IItemSlot<T> inventoryItemClone = this.itemSlots[indexItem].Clone();
if (cursorItemClone.Equals(inventoryItemClone))
if (cursorItemClone.Item == null && inventoryItemClone.Item == null)
return cursorItemClone;
if (cursorItemClone.Item == null || inventoryItemClone.Item == null)
return this.SimplySwapItem(cursorItemClone, inventoryItemClone, indexItem);
if (!cursorItemClone.Item.Equals(inventoryItemClone.Item) ||
cursorItemClone.DuranceLeft != inventoryItemClone.DuranceLeft)
return this.SimplySwapItem(cursorItemClone, inventoryItemClone, indexItem);
if (inventoryItemClone.GetRemainingSlot() <= 0) return cursorItemClone;
if (cursorItemClone.Quanity <= inventoryItemClone.GetRemainingSlot())
{
this.itemSlots[indexItem].Quanity += cursorItemClone.Quanity;
cursorItemClone.ResetSlot();
return cursorItemClone;
}
else
{
cursorItemClone.Quanity -= inventoryItemClone.GetRemainingSlot();
this.itemSlots[indexItem].Quanity = inventoryItemClone.Item.MaxStack;
return cursorItemClone;
}
}
public IItemSlot<T> SimplySwapItem(IItemSlot<T> cursorItem, IItemSlot<T> item, int indexItem)
{
this.itemSlots[indexItem] = cursorItem;
cursorItem = item;
return cursorItem;
}
/// <summary>
/// (Make garbage)
/// </summary>
public IItemSlot<T> SwapItemRightClick(IItemSlot<T> cursorItem, int indexItem)
{
if (this.itemSlots[indexItem].Item == null && cursorItem.Item == null) return cursorItem;
IItemSlot<T> cursorItemClone = cursorItem.Clone();
IItemSlot<T> itemClone = this.itemSlots[indexItem].Clone();
if (itemClone.Item != null && cursorItemClone.Item == null)
{
if (itemClone.Quanity <= 1)
return this.SimplySwapItem(cursorItemClone, itemClone, indexItem);
this.itemSlots[indexItem].Quanity = itemClone.Quanity / 2;
cursorItemClone = itemClone;
cursorItemClone.Quanity = itemClone.Quanity / 2;
if (itemClone.Quanity % 2 == 1) cursorItemClone.Quanity += 1;
return cursorItemClone;
}
else if (itemClone.Item != null && cursorItemClone.Item != null)
{
if (!cursorItemClone.Item.Equals(itemClone.Item) ||
cursorItemClone.DuranceLeft != itemClone.DuranceLeft)
return this.SimplySwapItem(cursorItemClone, itemClone, indexItem);
if (itemClone.Quanity >= itemClone.Item.MaxStack) return cursorItemClone;
cursorItemClone.Quanity -= 1;
this.itemSlots[indexItem].Quanity += 1;
return cursorItemClone;
}
else
{
if (cursorItemClone.Quanity <= 1)
return this.SimplySwapItem(cursorItemClone, itemClone, indexItem);
cursorItemClone.Quanity -= 1;
this.itemSlots[indexItem] = cursorItemClone;
this.itemSlots[indexItem].Quanity = 1;
return cursorItemClone;
}
}
}
}Editor is loading...
Leave a Comment