- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- [CreateAssetMenu(menuName = "Major Project/Inventory", order = 150)]
- public class Inventory : ScriptableObject
- {
-
- 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.Contains(data))
- {
- Data bagData = BagItems.Find(p => p.Equals(data));
-
- 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 == element))
- return;
-
- Data data = _bagItems.First(p => p.element == element);
-
- if (!data.isInfinit)
- data.Count--;
-
- 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>(copy._bagItems);
-
- return retVal;
- }
-
-
- [System.Serializable]
- public class Data
- {
- public LogicBlock element;
- public int Count;
- public bool isInfinit = false;
- }
- }
|