You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Linq;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [CreateAssetMenu(menuName = "Major Project/Inventory", order = 150)]
  6. public class Inventory : ScriptableObject
  7. {
  8. public List<Data> BagItems { get { if (_bagItems == null) _bagItems = new List<Data>(); return _bagItems; } }
  9. [SerializeField]
  10. protected List<Data> _bagItems = new List<Data>();
  11. public System.Action OnItemsUpdated;
  12. public void Add(LogicBlock element,int count = 1, bool isInfinit = false)
  13. {
  14. Data data = new Data {
  15. element = element,
  16. Count = count,
  17. isInfinit = isInfinit };
  18. Add(data);
  19. }
  20. public void Add(Data data)
  21. {
  22. if (_bagItems.Contains(data))
  23. {
  24. Data bagData = BagItems.Find(p => p.Equals(data));
  25. bagData.Count += data.Count;
  26. bagData.isInfinit = (data.isInfinit || bagData.isInfinit);
  27. }
  28. else
  29. _bagItems.Add(data);
  30. OnItemsUpdated?.Invoke();
  31. }
  32. public void Remove(LogicBlock element) {
  33. if (!_bagItems.Exists(p => p.element == element))
  34. return;
  35. Data data = _bagItems.First(p => p.element == element);
  36. if (!data.isInfinit)
  37. data.Count--;
  38. OnItemsUpdated?.Invoke();
  39. }
  40. public void Reset()
  41. {
  42. _bagItems = new List<Data>();
  43. OnItemsUpdated = null;
  44. }
  45. public void SetItems(Data[] bagItems)
  46. {
  47. _bagItems = new List<Data>(bagItems);
  48. OnItemsUpdated?.Invoke();
  49. }
  50. public static Inventory Clone(Inventory copy)
  51. {
  52. Inventory retVal = ScriptableObject.CreateInstance<Inventory>();
  53. retVal._bagItems = new List<Data>(copy._bagItems);
  54. return retVal;
  55. }
  56. [System.Serializable]
  57. public class Data
  58. {
  59. public LogicBlock element;
  60. public int Count;
  61. public bool isInfinit = false;
  62. }
  63. }