Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. /// <summary>
  5. /// A storage unit for items.
  6. /// </summary>
  7. public class ItemContainer : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private float capacity = 250;
  11. [SerializeField]
  12. private float usedCapacity;
  13. [SerializeField]
  14. private List<Item> _contents = new List<Item>();
  15.  
  16. public bool destroyWhenEmpty = false;
  17.  
  18. void Awake()
  19. {
  20.  
  21. }
  22.  
  23. void Update()
  24. {
  25. AdjustCapacity();
  26. if (destroyWhenEmpty && usedCapacity == 0)
  27. {
  28. Destroy(transform.root.gameObject);
  29. }
  30. }
  31.  
  32. // Remove requested amount of items from the container, or the remaining value
  33. public Item TakeItem(Item itemDescription, float capacity)
  34. {
  35. if (itemDescription == null){return null;}
  36.  
  37. Item _storedItem = _contents.Find(v => v.id == itemDescription.id);
  38. if (_storedItem == null)
  39. {
  40. return null;
  41. }
  42. Item _takenItem = _storedItem.Clone();
  43.  
  44. // Calculate maximum allowable volume
  45. int maxUnits = Mathf.FloorToInt(capacity / itemDescription.volume);
  46.  
  47. int _amount = Mathf.Min(maxUnits, itemDescription.amount);
  48. //Debug.Log("Capacity is " + capacity + " and I want to give " + _amount * _takenItem.volume);
  49. if (_storedItem.amount - _amount <= 0)
  50. {
  51. _takenItem.amount = _storedItem.amount;
  52. _contents.Remove(_storedItem);
  53. return _takenItem;
  54. }
  55. else
  56. {
  57. _storedItem.amount -= _amount;
  58. _takenItem.amount = _amount;
  59. return _takenItem;
  60. }
  61. }
  62.  
  63. // Add item to the container, return items that did not fit inside.
  64. public void GiveItem(int itemId, int amount)
  65. {
  66. // If the item already exists, add the fitAmount
  67. if (_contents.Exists(v => v.id == itemId))
  68. {
  69. Item _myItem = _contents.Find(v => v.id == itemId);
  70. _myItem.amount += amount;
  71. }
  72.  
  73. // If it does not exist, create a clone.
  74. else
  75. {
  76. _contents.Add(ItemDefinitions.CloneItem(itemId, amount));
  77. }
  78. }
  79.  
  80. public bool DestroyItem(int Id, int amount)
  81. {
  82. if (HaveInCargo(Id) && CountInCargo(Id) > 0)
  83. {
  84. Item item = _contents.Find(i => i.id == Id);
  85. item.amount -= amount;
  86. return true;
  87. }
  88. else
  89. {
  90. _contents.Remove(_contents.Find(i => i.id == Id));
  91. return false;
  92. }
  93. }
  94.  
  95. private int Fitremaining(Item item)
  96. {
  97. if (item.amount * item.volume <= AvailableCapacity())
  98. {
  99. return item.amount;
  100. }
  101. else
  102. {
  103. return Mathf.Min(Mathf.FloorToInt(AvailableCapacity() / item.volume), item.amount);
  104. }
  105. }
  106.  
  107. private void AdjustCapacity()
  108. {
  109. float newCapacity = 0;
  110. foreach (Item item in _contents)
  111. {
  112. newCapacity += item.amount*item.volume;
  113. }
  114.  
  115. usedCapacity = newCapacity;
  116. }
  117.  
  118. public float AvailableCapacity()
  119. {
  120. return Mathf.Round((capacity - usedCapacity) * 100f) / 100f;
  121. }
  122.  
  123. public bool HaveInCargo(int Id)
  124. {
  125. return _contents.Exists(i => i.id == Id);
  126. }
  127.  
  128. public int CountInCargo(int Id)
  129. {
  130. if (HaveInCargo(Id))
  131. {
  132. return _contents.Find(i => i.id == Id).amount;
  133. }
  134. else
  135. {
  136. return 0;
  137. }
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement