Exemplo n.º 1
0
 /// <summary>
 /// 窥探性获取而不真正获取
 /// </summary>
 /// <param name="model">道具原型</param>
 /// <param name="amount">数量</param>
 /// <param name="simulLoseItems">同时失去的道具</param>
 /// <returns>实际获取数量</returns>
 public int PeekGet(ItemBase model, int amount, params ItemWithAmount[] simulLoseItems)
 {
     if (!model || amount <= 0)
     {
         return(0);
     }
     return(PeekGet(model.CreateData(false), amount, simulLoseItems));
 }
Exemplo n.º 2
0
 /// <summary>
 /// 窥探性获取而不是真正获取
 /// </summary>
 /// <param name="model">道具原型</param>
 /// <param name="amount">数量</param>
 /// <param name="errorType">错误类型</param>
 /// <param name="simulLoseItems">同时失去的道具</param>
 /// <returns>是否可以获取</returns>
 public bool PeekGet(ItemBase model, int amount, out InventoryError errorType, params ItemWithAmount[] simulLoseItems)
 {
     if (!model || amount <= 0)
     {
         errorType = InventoryError.Invalid;
         return(false);
     }
     return(PeekGet(model.CreateData(false), amount, out errorType, simulLoseItems));
 }
Exemplo n.º 3
0
    /// <summary>
    /// 按原型放入道具
    /// </summary>
    /// <param name="model">道具原型</param>
    /// <param name="amount">数量</param>
    /// <param name="simulLoseItems">同时失去的道具</param>
    public void Get(ItemBase model, int amount, params ItemWithAmount[] simulLoseItems)
    {
        if (customGetAction != null && customGetAction(new ItemData(model, false), amount))
        {
            return;
        }

        if (!model || amount < 1)
        {
            return;
        }
        WeightCost += model.Weight * amount;
        if (simulLoseItems != null && simulLoseItems.Length > 0)
        {
            Lose(simulLoseItems);
        }
        ItemData item      = null;
        int      oldAmount = 0;

        if (keyedSlots.TryGetValue(model.ID, out var find))
        {
            ItemWithAmount iwa = null;
            foreach (var slot in find)
            {
                oldAmount += slot.amount;
                if (!item)
                {
                    item = slot.item;
                    if (item)
                    {
                        items.TryGetValue(item.ID, out iwa);
                    }
                }
                int take = slot.Put(amount);
                if (iwa)
                {
                    iwa.amount += take;
                }
                amount -= take;
            }
        }
        else
        {
            find = new List <ItemSlotData>();
            keyedSlots.Add(model.ID, find);
        }
        CostEmptySlot(find);
        OnItemAmountChanged?.Invoke(model, oldAmount, oldAmount + amount);

        void CostEmptySlot(List <ItemSlotData> collection)
        {
            if (amount <= 0 || collection == null)
            {
                return;
            }
            if (model.StackAble && item == null)
            {
                item = model.CreateData();                                 //如果物品可叠加但没有在背包中找到,则这个是新物品
            }
            ItemSlotData temp;

            for (int i = 0; i < amount / model.StackNum; i++)
            {
                temp = PutIntoEmptySlot(model.StackAble ? item : model.CreateData(), model.StackNum);
                if (temp)
                {
                    collection.Add(temp);
                }
            }
            if (model.StackAble)
            {
                temp = PutIntoEmptySlot(item, amount % model.StackNum);
                if (temp)
                {
                    collection.Add(temp);
                }
            }
        }
    }