예제 #1
0
        private void SellItem(Inventory shopperInventory, Purse shopperPurse, InventoryItem item, float price)
        {
            int slot = FindFirstItemSlot(shopperInventory, item);

            if (slot == -1)
            {
                return;
            }

            AddToTransaction(item, -1);
            shopperInventory.RemoveFromSlot(slot, 1);
            if (!stockSold.ContainsKey(item))
            {
                stockSold[item] = 0;
            }
            stockSold[item]--;
            shopperPurse.UpdateBalance(price);
        }
예제 #2
0
        private void BuyItem(Inventory shopperInventory, Purse shopperPurse, InventoryItem item, float price)
        {
            if (shopperPurse.GetBalance() < price)
            {
                return;
            }

            bool success = shopperInventory.AddToFirstEmptySlot(item, 1);

            if (success)
            {
                AddToTransaction(item, -1);
                if (!stockSold.ContainsKey(item))
                {
                    stockSold[item] = 0;
                }
                stockSold[item]++;
                shopperPurse.UpdateBalance(-price);
            }
        }
예제 #3
0
        public void ConfirmTransaction()
        {
            Inventory shopperInventory = currentShopper.GetComponent <Inventory>();
            Purse     shopperPurse     = currentShopper.GetComponent <Purse>();

            if (shopperInventory == null || shopperPurse == null)
            {
                return;
            }

            // Transfer to or from the inventory
            foreach (ShopItem shopItem in GetAllItems())
            {
                InventoryItem item     = shopItem.GetInventoryItem();
                int           quantity = shopItem.GetQuantityInTransaction();
                float         price    = shopItem.GetPrice();
                for (int i = 0; i < quantity; i++)
                {
                    if (shopperPurse.GetBalance() < price)
                    {
                        break;
                    }

                    bool success = shopperInventory.AddToFirstEmptySlot(item, 1);
                    if (success)
                    {
                        AddToTransaction(item, -1);
                        stock[item]--;
                        shopperPurse.UpdateBalance(-price);
                    }
                }
            }
            // Removal from transaction
            // Debting or Crediting of funds

            if (onChange != null)
            {
                onChange();
            }
        }