Example #1
0
 /*组合背包物品的方法*/
 public void MergeItem(int _form_item_index, int _to_item_index)
 {
     if (PlayerKnapsack[_form_item_index].inside_item_data.item_id == PlayerKnapsack[_to_item_index].inside_item_data.item_id)
     {
         //如果两个交换的物品是同一个ID,那么就处理数量
         int TotalQuantity = PlayerKnapsack[_form_item_index].inside_item_quantity + PlayerKnapsack[_to_item_index].inside_item_quantity;
         if (TotalQuantity > PlayerKnapsack[_form_item_index].inside_item_data.item_stackable_limit)
         {
             PlayerKnapsack[_to_item_index].inside_item_quantity   = PlayerKnapsack[_form_item_index].inside_item_data.item_stackable_limit;
             PlayerKnapsack[_form_item_index].inside_item_quantity = TotalQuantity - PlayerKnapsack[_form_item_index].inside_item_data.item_stackable_limit;
         }
         else
         {
             PlayerKnapsack[_to_item_index].inside_item_quantity = TotalQuantity;
             PlayerKnapsack.RemoveAt(_form_item_index);
         }
     }
     else
     {
         //否则就交换它们的位置
         ItemInKnapsack t_item = PlayerKnapsack[_form_item_index];
         PlayerKnapsack[_form_item_index] = PlayerKnapsack[_to_item_index];
         PlayerKnapsack[_to_item_index]   = t_item;
     }
 }
Example #2
0
        };                                                                          //物品ID 在背包中的索引

        /*添加物品到背包的方法 需要传进物品数据和物品数量*/
        public void AddItem(ItemBaseClass _data, int _quantity = 1) //_quantity=时没有传入参数,就按照1继续进行
        {
            if (KnapsackIndex.ContainsKey(_data.item_id) != true)   //当背包中没有该物品时,新建一个空的用来装
            {
                ItemInKnapsack t_new_null_item = new ItemInKnapsack
                {
                    inside_item_data     = _data,
                    inside_item_quantity = 0
                };
                PlayerKnapsack.Add(t_new_null_item);
                KnapsackIndex[_data.item_id] = PlayerKnapsack.Count - 1;
            }

            while (true)
            {
                int number_can_add = _data.item_stackable_limit - PlayerKnapsack[KnapsackIndex[_data.item_id]].inside_item_quantity;
                PlayerKnapsack[KnapsackIndex[_data.item_id]].inside_item_quantity += Math.Min(_quantity, number_can_add);
                _quantity -= number_can_add;

                if (_quantity > 0)
                {
                    ItemInKnapsack t_new_null_item = new ItemInKnapsack
                    {
                        inside_item_data     = _data,
                        inside_item_quantity = 0
                    };
                    PlayerKnapsack.Add(t_new_null_item);
                    KnapsackIndex[_data.item_id] = PlayerKnapsack.Count - 1;
                }
                else
                {
                    break;
                }
            }
        }
Example #3
0
 /*拆分背包物品的方法*/
 public void SplitItem(int _item_index_in_list, int _split_number)
 {
     if (_split_number < PlayerKnapsack[_item_index_in_list].inside_item_quantity)
     {
         //先把原本的数量减去拆分的数量
         PlayerKnapsack[_item_index_in_list].inside_item_quantity -= _split_number;
         //然后新建一个拆分数量的物品丢到背包里
         ItemInKnapsack t_new_item = new ItemInKnapsack
         {
             inside_item_data     = PlayerKnapsack[_item_index_in_list].inside_item_data,
             inside_item_quantity = _split_number
         };
         PlayerKnapsack.Add(t_new_item); //将其添加至背包中
     }
     else
     {
         return;
     }
 }