Ejemplo n.º 1
0
        /**
         *  Add an object to the inventory
         *  Will add the object to the first open
         *  location found.
         *
         *  If this is a stackable item it will attempt
         *  to stack it in each slot that is a stackable item.
         *  If there is a remainder of items, it will place them
         *  at the first emtpy slot
         *
         *  @param obj The GameObject
         **/
        public void AddObject(GameObject obj)
        {
            StackableInventoryItem stackableItem   = obj.GetComponent <StackableInventoryItem>();
            InventoryPosition      defaultPosition = null;

            for (int x = 0; x < inventory.GetLength(0); x++)
            {
                for (int y = 0; y < inventory.GetLength(1); y++)
                {
                    if (inventory[x, y] == null)
                    {
                        if (stackableItem == null)
                        {
                            AddObject(new InventoryPosition(x, y), obj);
                            return;
                        }
                        else if (defaultPosition == null)
                        {
                            defaultPosition = new InventoryPosition(x, y);
                        }
                    }
                    else
                    {
                        if (stackableItem != null)
                        {
                            StackableInventoryItem inventoryItem = inventory[x, y].GetComponent <StackableInventoryItem>();
                            if (inventoryItem != null)
                            {
                                StackableInventoryItem remainder = inventoryItem.AddStackableInventoryItems(stackableItem);
                                if (remainder == null)
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            if (defaultPosition != null)
            {
                AddObject(defaultPosition, obj);
            }
        }
Ejemplo n.º 2
0
        /**
         *  Add a StackableInventoryItem to this item
         *  Will move all children up to the max count allowed
         *  including the parent item
         *
         *  @param StackableInventoryItem items The item stack to add
         *  @returns StackableInventoryItem the remaining items, or null
         *
         **/
        public StackableInventoryItem AddStackableInventoryItems(StackableInventoryItem items)
        {
            if (count >= maxCount || items.itemName != this.itemName)
            {
                return(items);
            }

            //	iterating through transforms as
            //	`GetComponentsInChildren` does return the
            //	top level component, which we don't want
            //	so we'll create a transform list so we can
            //	mutate the transforms in items.transform
            //	and not break the loop

            List <Transform> tmpTransforms = new List <Transform>();

            foreach (Transform t in items.transform)
            {
                tmpTransforms.Add(t);
            }

            foreach (Transform t in tmpTransforms)
            {
                StackableInventoryItem child = t.GetComponent <StackableInventoryItem>();
                AddStackableInventoryItem(child);
                items.DecrementCount();
                if (count >= maxCount)
                {
                    return(items);
                }
            }

            //	add the top level stackable item
            AddStackableInventoryItem(items);
            return(null);
        }
Ejemplo n.º 3
0
 private void AddStackableInventoryItem(StackableInventoryItem item)
 {
     item.transform.parent = this.transform;
     IncrementCount();
     item.DecrementCount();
 }