コード例 #1
0
ファイル: b_Inventory.cs プロジェクト: ThatBirkl/Bhrachan
    /*
     * Remove a specified amount of a specified consumable from the inventory
     */
    public bool RemoveConsumable(Ressources.Consumables type, int amount)
    {
        foreach (var entry in inventory)
        {
            try
            {
                Item.Consumable ic = (Item.Consumable)entry.Key;
                if (ic.GetName() == type)
                {
                    if (entry.Value - amount == 0)
                    {
                        inventory.Remove(ic);
                        return(true);
                    }
                    else if (entry.Value - amount < 0)
                    {
                        return(false);
                    }
                    else
                    {
                        inventory[ic] = entry.Value - amount;
                        Debug.Log(inventory[ic]);
                        return(true);
                    }
                }
            }
            catch (System.InvalidCastException e) { }
        }

        return(false);
    }
コード例 #2
0
ファイル: b_Inventory.cs プロジェクト: ThatBirkl/Bhrachan
    /*
     * Adds one or more items of a type to the inventory
     *
     * @param item The item that should be added
     * @param amount The number of these items that should be added
     *
     * @return Returns if the item was added or not
     */
    public bool Add(Item item, int amount)
    {
        if (weight + (item.GetWeight() * amount) > capacity)
        {
            return(false);
        }

        if (item.GetItemType() == Type.ItemType.weapon ||
            item.GetItemType() == Type.ItemType.armor)
        {
            inventory.Add(item, 1);
            weight += item.GetWeight();
        }
        else
        {
            bool added = false;
            if (item.GetItemType() == Type.ItemType.consumable)
            {
                Item.Consumable c = (Item.Consumable)item;

                foreach (var entry in inventory)
                {
                    try
                    {
                        Item.Consumable ic = (Item.Consumable)entry.Key;
                        if (ic.GetName() == c.GetName())
                        {
                            inventory[ic] = entry.Value + amount;
                            added         = true;
                            return(true);
                        }
                    }
                    catch (System.InvalidCastException e) { Debug.Log(e.ToString()); }
                }
            }
            else
            {
                Item.Ressource r = (Item.Ressource)item;

                foreach (var entry in inventory)
                {
                    try
                    {
                        Item.Ressource ir = (Item.Ressource)entry.Key;
                        if (ir.GetName() == r.GetName())
                        {
                            inventory[ir] = entry.Value + amount;
                            added         = true;
                            return(true);
                        }
                    }
                    catch (System.InvalidCastException e) { }
                }
            }

            if (!added)
            {
                inventory.Add(item, amount);
            }
        }

        return(false);
    }