private void Populate(List <Item> assets)
        {
            for (int i = 0; i < assets.Count; i++)
            {
                StackableItem stackable1 = new StackableItem
                {
                    item   = assets[i],
                    Amount = UnityEngine.Random.Range(1, assets[i].stack)
                };

                StackableItem stackable2 = new StackableItem
                {
                    item   = assets[i],
                    Amount = UnityEngine.Random.Range(1, assets[i].stack)
                };

                int i1 = UnityEngine.Random.Range(0, 8);
                int i2 = UnityEngine.Random.Range(0, 8);

                holders[i1].SetItem(stackable1);
                holders[i2].SetItem(stackable2);
            }

            UpdateUI();
        }
Beispiel #2
0
        public StackableItem Split()
        {
            int total = Amount;

            Amount = (int)(Amount / 2);

            StackableItem splitted = new StackableItem
            {
                item   = item,
                Amount = total - Amount
            };

            return(splitted);
        }
        private void DropNormal(GameObject target)
        {
            // no target no rockenroll
            if (target == null)
            {
                return;
            }

            Slot targetSlot = target.GetComponentInParent <Slot>();

            // same slot no rockenroll
            if (targetSlot == slot)
            {
                return;
            }

            ItemHolder    targetHolder = target.GetComponentInParent <ItemHolder>();
            StackableItem targetItem   = targetHolder.GetItem();

            // if target item is same type (add)
            if (targetItem != null && item.item.name == targetItem.item.name)
            {
                // Add amount
                int remaining = targetItem.AddAmount(item.Amount);

                // Remaining back
                item.Amount = remaining;

                targetHolder.SetItem(targetItem);

                if (remaining > 0)
                {
                    holder.SetItem(item);
                }
                else
                {
                    // nothing left
                    item = null;
                    holder.SetItem(null);
                }
            }
            else
            {
                // swap entire items
                holder.SetItem(targetItem);
                targetHolder.SetItem(item);
            }
        }
        private void Split()
        {
            // dont split if amount under 2, drag as normal
            if (item.Amount < 2)
            {
                return;
            }

            // split stack to half
            StackableItem splitted = item.Split();

            // set placeholder (stay put)
            placeHolder = Instantiate(gameObject, transform.parent);
            placeHolder.GetComponent <ItemHolder>().SetItem(splitted);

            isSplitMode = true;
        }
 private void UpdateItem()
 {
     item = holder.GetItem();
 }
        private void DropSplit(GameObject target)
        {
            if (target == null)
            {
                // invalid middle drop, revert amount back
                StackableItem holdItem = placeHolder.GetComponent <ItemHolder>().GetItem();
                item.Amount += holdItem.Amount;
                return;
            }

            Slot targetSlot = target.GetComponentInParent <Slot>();

            if (targetSlot == slot)
            {
                // middle drop back to position, revert amount back
                StackableItem holdItem = placeHolder.GetComponent <ItemHolder>().GetItem();
                item.Amount += holdItem.Amount;
                return;
            }

            ItemHolder    targetHolder = target.GetComponentInParent <ItemHolder>();
            StackableItem targetItem   = targetHolder.GetItem();

            // if target item is same type (add)
            if (targetItem != null && item.item.name == targetItem.item.name)
            {
                // Add amount
                int remaining = targetItem.AddAmount(item.Amount);

                // Remaining add to placeholder (stand still on place)
                StackableItem holdItem = placeHolder.GetComponent <ItemHolder>().GetItem();
                holdItem.Amount += remaining;

                targetHolder.SetItem(targetItem);

                if (holdItem.Amount > 0)
                {
                    holder.SetItem(holdItem);
                }
                else
                {
                    // nothing left
                    item = null;
                    holder.SetItem(null);
                }
            }
            else
            {
                if (targetItem == null)
                {
                    holder.SetItem(placeHolder.GetComponent <ItemHolder>().GetItem());
                    targetHolder.SetItem(item);
                }
                else
                {
                    // drop is invalid, revert amount back
                    StackableItem holdItem = placeHolder.GetComponent <ItemHolder>().GetItem();
                    item.Amount += holdItem.Amount;
                    holder.SetItem(item);
                }
            }
        }
Beispiel #7
0
 public void SetItem(StackableItem newItem)
 {
     item = newItem;
     UpdateUI();
 }