Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// A storage unit for items.
- /// </summary>
- public class ItemContainer : MonoBehaviour
- {
- [SerializeField]
- private float capacity = 250;
- [SerializeField]
- private float usedCapacity;
- [SerializeField]
- private List<Item> _contents = new List<Item>();
- public bool destroyWhenEmpty = false;
- void Awake()
- {
- }
- void Update()
- {
- AdjustCapacity();
- if (destroyWhenEmpty && usedCapacity == 0)
- {
- Destroy(transform.root.gameObject);
- }
- }
- // Remove requested amount of items from the container, or the remaining value
- public Item TakeItem(Item itemDescription, float capacity)
- {
- if (itemDescription == null){return null;}
- Item _storedItem = _contents.Find(v => v.id == itemDescription.id);
- if (_storedItem == null)
- {
- return null;
- }
- Item _takenItem = _storedItem.Clone();
- // Calculate maximum allowable volume
- int maxUnits = Mathf.FloorToInt(capacity / itemDescription.volume);
- int _amount = Mathf.Min(maxUnits, itemDescription.amount);
- //Debug.Log("Capacity is " + capacity + " and I want to give " + _amount * _takenItem.volume);
- if (_storedItem.amount - _amount <= 0)
- {
- _takenItem.amount = _storedItem.amount;
- _contents.Remove(_storedItem);
- return _takenItem;
- }
- else
- {
- _storedItem.amount -= _amount;
- _takenItem.amount = _amount;
- return _takenItem;
- }
- }
- // Add item to the container, return items that did not fit inside.
- public void GiveItem(int itemId, int amount)
- {
- // If the item already exists, add the fitAmount
- if (_contents.Exists(v => v.id == itemId))
- {
- Item _myItem = _contents.Find(v => v.id == itemId);
- _myItem.amount += amount;
- }
- // If it does not exist, create a clone.
- else
- {
- _contents.Add(ItemDefinitions.CloneItem(itemId, amount));
- }
- }
- public bool DestroyItem(int Id, int amount)
- {
- if (HaveInCargo(Id) && CountInCargo(Id) > 0)
- {
- Item item = _contents.Find(i => i.id == Id);
- item.amount -= amount;
- return true;
- }
- else
- {
- _contents.Remove(_contents.Find(i => i.id == Id));
- return false;
- }
- }
- private int Fitremaining(Item item)
- {
- if (item.amount * item.volume <= AvailableCapacity())
- {
- return item.amount;
- }
- else
- {
- return Mathf.Min(Mathf.FloorToInt(AvailableCapacity() / item.volume), item.amount);
- }
- }
- private void AdjustCapacity()
- {
- float newCapacity = 0;
- foreach (Item item in _contents)
- {
- newCapacity += item.amount*item.volume;
- }
- usedCapacity = newCapacity;
- }
- public float AvailableCapacity()
- {
- return Mathf.Round((capacity - usedCapacity) * 100f) / 100f;
- }
- public bool HaveInCargo(int Id)
- {
- return _contents.Exists(i => i.id == Id);
- }
- public int CountInCargo(int Id)
- {
- if (HaveInCargo(Id))
- {
- return _contents.Find(i => i.id == Id).amount;
- }
- else
- {
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement