using System.Linq;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[CreateAssetMenu(menuName = "Major Project/Inventory", order = 150)]
|
|
public class Inventory : ScriptableObject
|
|
{
|
|
|
|
public string test;
|
|
|
|
public List<Data> BagItems { get { if (_bagItems == null) _bagItems = new List<Data>(); return _bagItems; } }
|
|
|
|
[SerializeField]
|
|
protected List<Data> _bagItems = new List<Data>();
|
|
|
|
public System.Action OnItemsUpdated;
|
|
|
|
public void Add(LogicBlock element, int count = 1, bool isInfinit = false)
|
|
{
|
|
|
|
Data data = new Data
|
|
{
|
|
element = element,
|
|
Count = count,
|
|
isInfinit = isInfinit
|
|
};
|
|
|
|
Add(data);
|
|
}
|
|
|
|
public void Add(Data data)
|
|
{
|
|
|
|
if (_bagItems.Exists(p => p.element.isSameType(data.element)))
|
|
{
|
|
Data bagData = BagItems.Find(p => p.element.isSameType(data.element));
|
|
|
|
bagData.Count += data.Count;
|
|
bagData.isInfinit = (data.isInfinit || bagData.isInfinit);
|
|
}
|
|
else
|
|
_bagItems.Add(data);
|
|
|
|
OnItemsUpdated?.Invoke();
|
|
}
|
|
|
|
public void Remove(LogicBlock element)
|
|
{
|
|
|
|
|
|
if (!_bagItems.Exists(p => p.element.isSameType(element)))
|
|
{
|
|
return;
|
|
}
|
|
|
|
|
|
Data data = _bagItems.First(p => p.element.isSameType(element));
|
|
|
|
if (!data.isInfinit)
|
|
{
|
|
if (data.Count > data.minCount)
|
|
data.Count--;
|
|
|
|
|
|
|
|
if (data.Count <= 0)
|
|
_bagItems.Remove(data);
|
|
}
|
|
|
|
|
|
OnItemsUpdated?.Invoke();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_bagItems = new List<Data>();
|
|
OnItemsUpdated = null;
|
|
}
|
|
|
|
public void SetItems(Data[] bagItems)
|
|
{
|
|
_bagItems = new List<Data>(bagItems);
|
|
OnItemsUpdated?.Invoke();
|
|
}
|
|
|
|
public static Inventory Clone(Inventory copy)
|
|
{
|
|
Inventory retVal = ScriptableObject.CreateInstance<Inventory>();
|
|
retVal._bagItems = new List<Data>();
|
|
|
|
foreach (Data data in copy.BagItems)
|
|
{
|
|
Data dataCopy = new Data(data.element, data.Count, data.isInfinit,data.minCount);
|
|
retVal._bagItems.Add(dataCopy);
|
|
}
|
|
|
|
return retVal;
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class Data
|
|
{
|
|
public LogicBlock element;
|
|
public int Count;
|
|
public int minCount;
|
|
public bool isInfinit = false;
|
|
|
|
|
|
public Data() { }
|
|
|
|
public Data(LogicBlock element, int Count, bool isInfinit, int minCount = 0)
|
|
{
|
|
this.element = element;
|
|
this.Count = Count;
|
|
this.isInfinit = isInfinit;
|
|
this.minCount = minCount;
|
|
}
|
|
}
|
|
}
|