public Item FillItem(Item itemToFill)
        {
            if (itemToFill == null)
            {
                Debug.LogError("null인 아이템은 컨테이너에 넣을 수 없습니다");
                return(itemToFill);
            }

            if (itemToFill.IsStackEmpty)
            {
                Debug.LogWarning($"비어있는 아이템은 컨테이너에 넣을 수 없습니다, {nameof(itemToFill.Data.ItemName)}: {itemToFill.Data.ItemName}");
                return(itemToFill);
            }

            if (HasItem(itemToFill.Data.ItemName))
            {
                foreach (var targetItem in container.Where(e => e.Data.ItemName == itemToFill.Data.ItemName && e.IsStackFull == false))
                {
                    itemToFill = targetItem.MergeStack(itemToFill);
                    if (itemToFill.IsStackEmpty)
                    {
                        break;
                    }
                }
            }
            else
            {
                container.Add(itemToFill);
                itemToFill = Item.EmptyItem;
            }
            container.Sort();

            OnContainerUpdate?.Invoke(this, EventArgs.Empty);
            return(itemToFill);
        }
        /// <summary>
        /// 컨테이너의 크기를 늘이거나 줄이고 넘친 아이템들은 반환
        /// </summary>
        /// <param name="newCapacity">새로운 컨테이너의 크기</param>
        /// <returns>컨테이너 크기를 변경한 후 넘친 아이템</returns>
        public List <Item> ResizeCapacity(int newCapacity)
        {
            var overflowItems = new List <Item>();

            if (newCapacity < 0)
            {
                Debug.LogError($"{nameof(ItemContainer)}의 크기가 0보다 작을 수 없습니다, {nameof(newCapacity)}: {newCapacity}");
                newCapacity = Mathf.Clamp(newCapacity, 0, int.MaxValue);
            }

            var newContainer = new List <Item>(newCapacity);

            for (int index = 0; index < Count && index < Capacity; index++)
            {
                if (index < newCapacity)
                {
                    newContainer.Add(container[index]);
                }
                else
                {
                    overflowItems.Add(container[index]);
                }
            }

            container = newContainer;
            OnContainerResize?.Invoke(this, EventArgs.Empty);
            OnContainerUpdate?.Invoke(this, EventArgs.Empty);
            return(overflowItems);
        }
        /// <summary>
        /// 매개변수로 받은 슬롯과 스택 크기만큼 아이템을 컨테이너에서 빼고 반환
        /// </summary>
        /// <param name="slot">컨테이너의 슬롯 인덱스</param>
        /// <param name="stack">아이템을 뺄 스택 크기</param>
        /// <returns>컨테이너에서 뺀 아이템</returns>
        public Item SubtrackItemsAtSlot(int slot, int stack)
        {
            if (slot < 0 || container.Capacity <= slot)
            {
                Debug.LogError($"컨테이너의 범위를 초과한 슬롯 인덱스 입니다, slot: {slot}");
                return(Item.EmptyItem);
            }

            if (slot >= container.Count)
            {
                return(Item.EmptyItem);
            }

            Item slotItem = container[slot];

            stack = Mathf.Clamp(stack, 0, slotItem.CurrentStack);

            Item splitedItem = slotItem.SplitStack(stack);

            if (slotItem.IsStackEmpty)
            {
                container.RemoveAt(slot);
            }

            OnContainerUpdate?.Invoke(this, EventArgs.Empty);

            return(splitedItem);
        }
示例#4
0
        public void AddItem(InventoryItem item, bool fromSave = false, float timeDecayPause = 0)
        {
            if (IsFull)
            {
                return;
            }

            var eatableEntity = new EatableEntities();

            eatableEntity.Initialize(item.item, false);
            eatableEntity.PauseDecay();
            if (fromSave)
            {
                eatableEntity.TimeDecayPause = timeDecayPause;
            }
            FridgeItems.Add(eatableEntity);
            OnContainerUpdate?.Invoke(NumberOfItems, _itemLimit);
            Destroy(item.item.gameObject);
        }
示例#5
0
        public Pickupable RemoveItemFromContainer(TechType techType, int amount)
        {
            EatableEntities match = FindMatch(techType, EatableType.Any);

            if (match == null)
            {
                return(null);
            }

            var go      = GameObject.Instantiate(CraftData.GetPrefabForTechType(techType));
            var eatable = go.GetComponent <Eatable>();
            var pickup  = go.GetComponent <Pickupable>();

            match.UnpauseDecay();
            eatable.timeDecayStart = match.TimeDecayStart;
            FridgeItems.Remove(match);
            OnContainerUpdate?.Invoke(NumberOfItems, _itemLimit);

            return(pickup);
        }
示例#6
0
        public void RemoveItem(TechType techType, EatableType eatableType)
        {
            var pickupable = techType.ToPickupable();

            if (Inventory.main.HasRoomFor(pickupable))
            {
                EatableEntities match = FindMatch(techType, eatableType);

                if (match != null)
                {
                    var go      = GameObject.Instantiate(CraftData.GetPrefabForTechType(techType));
                    var eatable = go.GetComponent <Eatable>();
                    var pickup  = go.GetComponent <Pickupable>();

                    match.UnpauseDecay();
                    eatable.timeDecayStart = match.TimeDecayStart;

                    if (Inventory.main.Pickup(pickup))
                    {
                        QuickLogger.Debug($"Removed Match Before || Fridge Count {FridgeItems.Count}");
                        FridgeItems.Remove(match);
                        QuickLogger.Debug($"Removed Match || Fridge Count {FridgeItems.Count}");
                    }
                    else
                    {
                        QuickLogger.Message(LanguageHelpers.GetLanguage("InventoryFull"), true);
                    }
                    GameObject.Destroy(pickupable);
                    OnContainerUpdate?.Invoke(NumberOfItems, _itemLimit);
                }
            }
            else
            {
                Destroy(pickupable);
            }
        }