我创建了一个库存系统,但我不知道如何进行

问题描述

我已经准备好所有东西,但是在将清单中创建的项目设置为实际插槽时遇到了问题。我这样做是为了更好地概述这些项目。

这是库存的代码

// Amount of Slots in inventory defined here!!!
[Serializefield]
private Slot[] slots = new Slot[10];
public List<Item> invItems = new List<Item>();




public void CheckSlot(Item item)
{
for (int i = 0; i < slots.Length; i++)
{
    if (slots[i].isEmpty == false)
    {
        //call a method.
        if (item.isstackable)
         {
             slots[i].amount = item.amount;
         }
     }

     if (slots[i].isEmpty == true)
     {
            //call another method.
            //run some code.
            
            slots[i] = new Slot(item);
         return;

     }
 }
 }

public void UpdateAmount(Item _item,Slot _slot)
{
    _slot.amount = _item.amount;
}

public void CreateItem(Item item)
{
    Item invItem1 = new Item();
    invItem1 = item.copy();
    invItems.Add(invItem1);
}


public void InventoryAdd(Item item)
{
    //check if the item is already in the inventory
    //if true check if it is stackable and the new amount is less than maxStackable amount
    //if false add the item to the invItems list and set the slot to false            


    

    

     if (invItems.Count != 0)
     {

     for (int i = 0; i < invItems.Count; i++)
        {
           
            if (invItems[i].ItemID == item.ItemID)
            {
                
                if (invItems[i].amount + item.amount <= invItems[i].maxStackable)
                {
                    
                    invItems[i].amount += item.amount;

                    CheckSlot(invItems[i]); //

                    return;
                }
                if (invItems[i].amount < invItems[i].maxStackable)
                {
                    if (invItems[i].amount + item.amount > invItems[i].maxStackable)
                    {

                        
                        int nextAmount = invItems[i].maxStackable - invItems[i].amount;
                        invItems[i].amount += nextAmount;


                        Item invItem1 = new Item();
                        invItem1 = item.copy();
                        invItem1.amount = invItem1.amount - nextAmount;
                        invItems.Add(invItem1);

                        

                        return;
                    }
                }
            }
        }
     }

    Item invItem = new Item();
    invItem = item.copy();
    invItems.Add(invItem);

和项目脚本:

public string name;
public  int ItemID;
public GameObject prefab;
public Sprite icon;
public int amount = 1;

public bool isstackable;
public int maxStackable = 5;

public Item()
{

}

public Item(string _name,int id,GameObject _prefab,int _amount,bool stackable,int maxStack)
{
    name = _name;
    ItemID = id;
    prefab = _prefab;
    amount = _amount;
    isstackable = stackable;
    maxStackable = maxStack;
}

public Item copy()
{
    Item copy = new Item();
    copy.name = this.name;
    copy.ItemID = this.ItemID;
    copy.prefab = this.prefab;
    copy.amount = this.amount;
    copy.isstackable = this.isstackable;
    copy.maxStackable = this.maxStackable;

    return copy;
}

和广告位代码

公共布尔isEmpty = true;

public Slot()
{

}

public Slot(Item _item)
{
    this.name = _item.name;
    this.ItemID = _item.ItemID;
    this.isstackable = _item.isstackable;
    this.prefab = _item.prefab;
    this.icon = _item.icon;
}

问题是我不知道如何将清单中创建的项目设置到插槽中。也许我需要清理代码,然后一次性检查所有这些内容,而不是单独检查?

解决方法

根据您的描述,我了解到您有多个插槽,每个插槽可以容纳一个或多个项目。 因此,与其像这样通过构造函数添加它们:

public Slot(Item _item)
{
    this.name = _item.name;
    this.ItemID = _item.ItemID;
    this.isStackable = _item.isStackable;
    this.prefab = _item.prefab;
    this.icon = _item.icon;
}

维护插槽类中的项目列表,并具有将项目添加到插槽的特殊方法。 像这样:

        public List<Item> ItemsInSlot {get;set;}
        public void AddItems(IEnumerable<Item> items)
        {
            ItemsInSlot.AddRange(items);
        }

我假设您已经有一些机制可以触发向插槽添加项目。就像用户界面中的按钮或将项目拖到广告位一样。