public void CompleteOnFilledFromPartial()
        {
            var storageSlot = new ItemStorageSlot()
            {
                MaxStackSize = 10
            };

            var template = new ProceduralItemTemplate();

            var itemToAddA = new StackableItem(template, 5);
            var itemToAddB = new StackableItem(template, 5);

            storageSlot.AddItem(itemToAddA);
            var result = storageSlot.AddItem(itemToAddB);

            Assert.That(result.Status, Is.EqualTo(TransactionStatus.Complete));
            Assert.That(result.Items, Has.Count.EqualTo(1));
            Assert.That(result.Items[0], Is.EqualTo(new ItemTransaction()
            {
                Quantity      = 5,
                Item          = itemToAddA,
                FromInventory = null,
                ToInventory   = storageSlot
            }));
        }
        public void StoreStackableItem()
        {
            var storageSlot = new ItemStorageSlot();

            var itemToAdd = new StackableItem(new ProceduralItemTemplate(), 10);

            var result = storageSlot.AddItem(itemToAdd);

            Assert.That(result.Status, Is.EqualTo(TransactionStatus.Complete));
            Assert.That(result.Items, Has.Count.EqualTo(1));
            Assert.That(result.Items[0], Is.EqualTo(new ItemTransaction()
            {
                Quantity      = 10,
                Item          = itemToAdd,
                FromInventory = null,
                ToInventory   = storageSlot
            }));

            Assert.AreEqual(TransactionStatus.Complete, result.Status);
            Assert.AreEqual(itemToAdd, storageSlot.CurrentItem);

            var firstTransaction = result.Items[0];

            Assert.AreEqual(null, firstTransaction.FromInventory);
            Assert.AreEqual(storageSlot, firstTransaction.ToInventory);
        }
        public void LimitedSlotCapacityForSingleStackableItem()
        {
            var storageSlot = new ItemStorageSlot()
            {
                MaxStackSize = 10
            };

            var itemToAdd = new StackableItem(new ProceduralItemTemplate(), 15);

            Assert.AreEqual(15, itemToAdd.Quantity);

            var result = storageSlot.AddItem(itemToAdd);

            Assert.That(result.Status, Is.EqualTo(TransactionStatus.Partial));
            Assert.That(result.Items, Has.Count.EqualTo(1));
            Assert.That(result.Items[0], Is.EqualTo(new ItemTransaction()
            {
                Quantity      = 10,
                Item          = storageSlot.CurrentItem,
                FromInventory = null,
                ToInventory   = storageSlot
            }));

            Assert.That(itemToAdd.Quantity, Is.EqualTo(5));
            Assert.That(((StackableItem)result.Items[0].Item).Quantity, Is.EqualTo(10));
        }
        public void LimitedSlotCapacityForMultipleStackableItems()
        {
            var storageSlot = new ItemStorageSlot()
            {
                MaxStackSize = 10
            };

            var template = new ProceduralItemTemplate();

            var itemToAddA = new StackableItem(template, 5);
            var itemToAddB = new StackableItem(template, 10);

            storageSlot.AddItem(itemToAddA);
            var result = storageSlot.AddItem(itemToAddB);

            Assert.That(result.Status, Is.EqualTo(TransactionStatus.Partial));
            Assert.That(result.Items, Has.Count.EqualTo(1));
            Assert.That(result.Items[0], Is.EqualTo(new ItemTransaction()
            {
                Quantity      = 5,
                Item          = itemToAddA,
                FromInventory = null,
                ToInventory   = storageSlot
            }));

            Assert.That(itemToAddB.Quantity, Is.EqualTo(5));
            Assert.That(((StackableItem)result.Items[0].Item).Quantity, Is.EqualTo(10));
        }
Example #5
0
        public void DragStackableToEmpty()
        {
            var fromItem = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            fromSlot.AddItem(fromItem);

            var result = fromSlot.DragInto(toSlot);

            var expected = new InventoryTransactionBuilder()
            {
                new ItemTransaction()
                {
                    FromInventory = null,
                    ToInventory   = toSlot,
                    Item          = fromItem,
                    Quantity      = 5
                }
            }.Build(TransactionStatus.Complete);

            Assert.That(result, Is.EqualTo(expected));
            Assert.That(fromSlot.CurrentItem, Is.Null);
            Assert.That(toSlot.CurrentItem, Is.EqualTo(fromItem));
        }
Example #6
0
        public void DragStackableToStackable()
        {
            var template = new ProceduralItemTemplate();

            var fromItem = new StackableItem(template, 5);
            var toItem   = new StackableItem(template, 10);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            fromSlot.AddItem(fromItem);
            toSlot.AddItem(toItem);

            var result = fromSlot.DragInto(toSlot);

            var expected = new InventoryTransactionBuilder()
            {
                new ItemTransaction()
                {
                    FromInventory = fromSlot,
                    ToInventory   = toSlot,
                    Item          = fromItem,
                    Quantity      = 15
                }
            }.Build(TransactionStatus.Complete);

            // Assert.That (result, Is.EqualTo (expected));
            Assert.That(fromItem.Quantity, Is.EqualTo(0));
            Assert.That(toItem.Quantity, Is.EqualTo(15));
        }
Example #7
0
        public void LimitCapacityInFullItemStorageSlot()
        {
            var storageSlot = new ItemStorageSlot
                              (
                new IInventoryConstraint[]
            {
                new WeightedInventoryConstraint(3)
            }
                              );

            var template = new ProceduralItemTemplate()
            {
                Weight = 1
            };

            var itemToAddA = new StackableItem(template, 3);
            var itemToAddB = new StackableItem(template, 5);

            storageSlot.AddItem(itemToAddA);
            var result = storageSlot.AddItem(itemToAddB);

            Assert.AreEqual(0, result.Quantity);
            Assert.AreEqual(InventoryResult.OperationStatus.None, result.Status);
            Assert.AreEqual(null, result.ItemAdded);
        }
Example #8
0
    private void AddStackItem(StackableItem pickedUpItem)
    {
        foreach (Item item in inventory)
        {
            if (item != null)
            {
                if (item.getItemType() == Item.ItemType.Stackable)
                {
                    if (item.itemName.Equals(pickedUpItem.itemName))
                    {
                        StackableItem tempStackableItem = (StackableItem)item;

                        if (tempStackableItem.AddStack())
                        {
                            pickedUpItem.Destroy();
                            inventorySlots[inventory.IndexOf(tempStackableItem)].GetComponentInChildren <Text>().text = tempStackableItem.getStackStatus();

                            return;
                        }
                    }
                }
            }
        }

        if (AddToInventory(pickedUpItem))
        {
            inventorySlots[inventory.IndexOf(pickedUpItem)].transform.GetChild(1).GetComponent <Image>().enabled = true;
            inventorySlots[inventory.IndexOf(pickedUpItem)].GetComponentInChildren <Text>().text = pickedUpItem.getStackStatus();
        }
    }
Example #9
0
        public static void Configure(ModComponent modComponent)
        {
            ModStackableComponent modStackableComponent = ModComponentUtils.ComponentUtils.GetComponent <ModStackableComponent>(modComponent);

            if (modStackableComponent is null)
            {
                return;
            }

            StackableItem stackableItem = ModComponentUtils.ComponentUtils.GetOrCreateComponent <StackableItem>(modStackableComponent);

            stackableItem.m_LocalizedMultipleUnitText = new LocalizedString {
                m_LocalizationID = modStackableComponent.MultipleUnitTextID
            };

            if (string.IsNullOrWhiteSpace(modStackableComponent.SingleUnitTextID))
            {
                stackableItem.m_LocalizedSingleUnitText = new LocalizedString {
                    m_LocalizationID = modComponent.DisplayNameLocalizationId
                };
            }
            else
            {
                stackableItem.m_LocalizedSingleUnitText = new LocalizedString {
                    m_LocalizationID = modStackableComponent.SingleUnitTextID
                };
            }

            stackableItem.m_StackSpriteName = modStackableComponent.StackSprite;

            stackableItem.m_ShareStackWithGear = new StackableItem[0];
            stackableItem.m_Units        = modStackableComponent.UnitsPerItem;
            stackableItem.m_UnitsPerItem = modStackableComponent.UnitsPerItem;
        }
Example #10
0
        public void AllowWeightlessItems()
        {
            var storageSlot = new ItemStorageSlot
                              (
                constraints: new IInventoryConstraint[]
            {
                new WeightedInventoryConstraint(10)
            }
                              );

            var template = new ProceduralItemTemplate()
            {
                Weight = 0
            };

            var itemToAddA = new StackableItem(template, 250)
            {
                MaxStackSize = 1000
            };

            var result = storageSlot.AddItem(itemToAddA);

            var firstTransaction = result.Items[0];

            Assert.AreEqual(250, ((StackableItem)firstTransaction.Item).Quantity);
        }
        public void LimitCapacityInPartiallyFilledItemStorageSlot()
        {
            var storageSlot = new ItemStorageSlot
                              (
                new IInventoryConstraint[]
            {
                new WeightedInventoryConstraint(3)
            }
                              );

            var template = new ProceduralItemTemplate()
            {
                Weight = 1
            };

            var itemToAddA = new StackableItem(template, 2);
            var itemToAddB = new StackableItem(template, 2);

            storageSlot.AddItem(itemToAddA);
            var result = storageSlot.AddItem(itemToAddB);

            var firstTransaction = result.Items[0];

            Assert.AreEqual(3, ((StackableItem)firstTransaction.Item).Quantity);
        }
Example #12
0
        public void TakeItemFromStack()
        {
            var sourceItem = new StackableItem(new ProceduralItemTemplate(), 12);

            var resultItem = sourceItem.Take(4);

            Assert.AreEqual(4, resultItem.Quantity);
            Assert.AreEqual(8, sourceItem.Quantity);
        }
Example #13
0
        public void ThrowWhenTakeWholeStack()
        {
            Assert.Throws <InvalidOperationException> (new TestDelegate(() =>
            {
                var sourceItem = new StackableItem(new ProceduralItemTemplate(), 8);

                var resultItem = sourceItem.Take(8);
            }));
        }
Example #14
0
 /// <summary>
 /// Triggered when we use stackable on different item
 /// </summary>
 /// <param name="item">item we use stackable on</param>
 protected virtual void onUseWithSecondItem(ItemObject item)
 {
     if (areItemsStackable(item))
     {
         StackableItem it = (StackableItem)item;
         it.quantity   += 1;
         this.quantity -= 1;
     }
 }
Example #15
0
        public void StoreStackableItem()
        {
            var storageSlot = new ItemStorageSlot();

            var itemToAdd = new StackableItem(new ProceduralItemTemplate(), 10);

            storageSlot.AddItem(itemToAdd);

            Assert.AreEqual(itemToAdd, storageSlot.CurrentItem);
        }
Example #16
0
        public static void Write(this ByteArrayStreamWriter writer, Item item)
        {
            writer.Write(item.Metadata.TibiaId);

            StackableItem stackable = item as StackableItem;

            if (stackable != null)
            {
                writer.Write((byte)stackable.Count);
            }
        }
Example #17
0
 /// <summary>
 /// Gets the selectable item (UI element) using stackableItem search.
 /// </summary>
 /// <param name="itemToSearch">The stackable item to search in the list.</param>
 /// <returns>Returns the selectableItem if found, null if not.</returns>
 public SelectableItem GetSelectableItem(StackableItem itemToSearch)
 {
     foreach (SelectableItem selectable in selectableItems)
     {
         if (selectable.itemToSell.item == itemToSearch.item)
         {
             return(selectable);
         }
     }
     return(null);
 }
Example #18
0
    public void AddItem(Item item)
    {
        if (item == null)
        {
            Debug.LogError("Attempting to add a null item to the inventory!");
            return;
        }

        int itemCount    = item.ItemType == Constants.ItemTypes.KeyItem ? KeyItems.Count + 1 : Items.Count + 1;
        int itemCapacity = item.ItemType == Constants.ItemTypes.KeyItem ? KeyItems.Capacity : Items.Capacity;

        if (itemCount > itemCapacity)
        {
            Debug.LogWarning("The item cannot be added because the inventory is full!");
            return;
        }

        //Add key items into the key item inventory
        if (item.ItemType == Constants.ItemTypes.KeyItem)
        {
            KeyItems.Add(item);
        }
        else
        {
            switch (item.ItemType)
            {
            //If the item is stackable, try to find the item in the inventory and add its uses count to the existing one
            case Constants.ItemTypes.Stackable:
                StackableItem stackableItem    = (StackableItem)item;
                StackableItem stackableInvItem = FindItem(stackableItem.Name, stackableItem.ItemType) as StackableItem;

                //The item is found in the inventory as a StackableItem, so add its uses
                if (stackableInvItem != null)
                {
                    stackableInvItem.Quantity += stackableItem.Quantity;
                }
                //The item wasn't found, so add the item to a new slot in the inventory
                else
                {
                    goto default;
                }
                break;

            default:
                Items.Add(item);
                break;
            }
        }

        if (ItemAddedEvent != null)
        {
            ItemAddedEvent(item);
        }
    }
Example #19
0
    public override void OnInspectorGUI()
    {
        StackableItem stackableItem = (StackableItem)target;

        stackableItem.itemName = EditorGUILayout.TextField("Item Name", stackableItem.itemName);
        stackableItem.hasLimit = EditorGUILayout.Toggle("Has Limit", stackableItem.hasLimit);

        if (stackableItem.hasLimit)
        {
            stackableItem.stackLimit = EditorGUILayout.IntField("Stack Limit", stackableItem.stackLimit);
        }
    }
        public void NoneOnTryAddUniqueItemAddedToOccupiedSlot()
        {
            var storageSlot = new ItemStorageSlot();

            var oldItem = new StackableItem(new ProceduralItemTemplate(), 15);
            var newItem = new UniqueItem(new ProceduralItemTemplate());

            storageSlot.AddItem(oldItem);

            var result = storageSlot.AddItem(newItem);

            Assert.AreEqual(TransactionStatus.None, result.Status);
        }
        public void NoneOnTryAddUniqueItemAddedToOccupiedSlot()
        {
            var storageSlot = new ItemStorageSlot();

            var oldItem = new StackableItem(new ProceduralItemTemplate(), 15);
            var newItem = new UniqueItem(new ProceduralItemTemplate());

            storageSlot.AddItem(oldItem);

            var result = storageSlot.AddItem(newItem);

            Assert.That(result, Is.EqualTo(InventoryTransaction.None));
        }
Example #22
0
        public void FullSelectToEmptySelect()
        {
            var fromItem = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemSelectSlot();
            var toSlot   = new ItemSelectSlot();

            fromSlot.AddItem(fromItem);

            fromSlot.SwapInto(toSlot);

            Assert.AreEqual(null, fromSlot.CurrentItem);
            Assert.AreEqual(fromItem, toSlot.CurrentItem);
        }
Example #23
0
        public void MoveStackableToEmptySlot()
        {
            var fromItem = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            fromSlot.AddItem(fromItem);

            fromSlot.MoveInto(toSlot);

            Assert.AreEqual(null, fromSlot.CurrentItem);
            Assert.AreEqual(fromItem, toSlot.CurrentItem);
        }
        public void DragEmptyToStackable()
        {
            var toItem = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            toSlot.AddItem(toItem);

            fromSlot.DragInto(toSlot);

            Assert.AreEqual(null, fromSlot.CurrentItem);
            Assert.AreEqual(toItem, toSlot.CurrentItem);
        }
Example #25
0
        public void NoneOnTryAddUniqueItemAddedToOccupiedSlot()
        {
            var storageSlot = new ItemStorageSlot();

            var oldItem = new StackableItem(new ProceduralItemTemplate(), 15);
            var newItem = new UniqueItem(new ProceduralItemTemplate());

            storageSlot.AddItem(oldItem);

            var result = storageSlot.AddItem(newItem);

            Assert.AreEqual(0, result.Quantity);
            Assert.AreEqual(InventoryResult.OperationStatus.None, result.Status);
            Assert.AreEqual(null, result.ItemAdded);
        }
Example #26
0
        public void DragEmptyToStackable()
        {
            var toItem = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            toSlot.AddItem(toItem);

            var result = fromSlot.DragInto(toSlot);

            Assert.That(result, Is.EqualTo(InventoryTransaction.None));
            Assert.That(fromSlot.CurrentItem, Is.Null);
            Assert.That(toSlot.CurrentItem, Is.EqualTo(toItem));
        }
        private static void MakeStackable(string prefabName)
        {
            GameObject gameObject = Resources.Load(prefabName) as GameObject;

            StackableItem stackableItem = gameObject.GetComponent <StackableItem>();

            if (stackableItem == null)
            {
                stackableItem = gameObject.AddComponent <StackableItem>();
                stackableItem.m_ShareStackWithGear = new StackableItem[0];
                stackableItem.m_StackSpriteName    = string.Empty;
                stackableItem.m_Units        = 1;
                stackableItem.m_UnitsPerItem = 1;
            }
        }
        public void DragStackableToStackableOfDifferentType()
        {
            var fromItem = new StackableItem(new ProceduralItemTemplate(), 5);
            var toItem   = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            fromSlot.AddItem(fromItem);
            toSlot.AddItem(toItem);

            fromSlot.DragInto(toSlot);

            Assert.AreEqual(toItem, fromSlot.CurrentItem);
            Assert.AreEqual(fromItem, toSlot.CurrentItem);
        }
        public void StoreStackableItem()
        {
            var storageSlot = new ItemStorageSlot();

            var itemToAdd = new StackableItem(new ProceduralItemTemplate(), 10);

            var result = storageSlot.AddItem(itemToAdd);

            var firstTransaction = result.Items[0];

            Assert.AreEqual(TransactionStatus.Complete, result.Status);
            Assert.AreEqual(itemToAdd, storageSlot.CurrentItem);

            Assert.AreEqual(null, firstTransaction.FromInventory);
            Assert.AreEqual(storageSlot, firstTransaction.ToInventory);
        }
Example #30
0
        public void FullStorageToFullStorage()
        {
            var fromItem = new StackableItem(new ProceduralItemTemplate(), 5);
            var toItem   = new StackableItem(new ProceduralItemTemplate(), 5);

            var fromSlot = new ItemStorageSlot();
            var toSlot   = new ItemStorageSlot();

            fromSlot.AddItem(fromItem);
            toSlot.AddItem(toItem);

            fromSlot.SwapInto(toSlot);

            Assert.AreEqual(toItem, fromSlot.CurrentItem);
            Assert.AreEqual(fromItem, toSlot.CurrentItem);
        }