示例#1
0
 /// <summary>
 /// Swap items between two cells
 /// </summary>
 /// <param name="firstCell"> Cell </param>
 /// <param name="secondCell"> Cell </param>
 public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
 {
     if ((firstCell != null) && (secondCell != null))
     {
         DragAndDropItem firstItem  = firstCell.GetItem();               // Get item from first cell
         DragAndDropItem secondItem = secondCell.GetItem();              // Get item from second cell
                                                                         // Swap items
         if (firstItem != null)
         {
             firstItem.transform.SetParent(secondCell.transform, false);
             firstItem.transform.localPosition = Vector3.zero;
             firstItem.MakeRaycast(true);
         }
         if (secondItem != null)
         {
             secondItem.transform.SetParent(firstCell.transform, false);
             secondItem.transform.localPosition = Vector3.zero;
             secondItem.MakeRaycast(true);
         }
         // Update states
         firstCell.UpdateMyItem();
         secondCell.UpdateMyItem();
         firstCell.UpdateBackgroundState();
         secondCell.UpdateBackgroundState();
     }
 }
示例#2
0
    /// <summary>
    /// Swap items between two cells
    /// </summary>
    /// <param name="firstCell"> Cell </param>
    /// <param name="secondCell"> Cell </param>
    public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
    {
        if ((firstCell != null) && (secondCell != null))
        {
            DragAndDropItem firstItem  = DragAndDropItem.draggedItem;       // Get item from first cell
            DragAndDropItem secondItem = secondCell.GetItem();              // Get item from second cell
                                                                            // Swap items
            if (secondItem != null)
            {
                if (secondItem.transform.parent != null)
                {
                    secondItem.transform.parent.DetachChildren();
                    DragAndDropItem tempItem = Instantiate(secondItem);
                    tempItem.GetComponent <PorteScript>().SetEntree1(false);
                    tempItem.GetComponent <PorteScript>().SetEntree2(false);
                    tempItem.GetComponent <PorteScript>().SetSortie(false);

                    Destroy(secondItem.gameObject);
                    secondItem = null;
                    secondItem = tempItem;
                }
                secondItem.transform.SetParent(firstCell.transform, false);
                secondItem.transform.localPosition = Vector3.zero;
                secondItem.MakeRaycast(true);
            }
            if (firstItem != null)
            {
                firstItem.transform.SetParent(secondCell.transform, false);
                firstItem.transform.localPosition = Vector3.zero;
                firstItem.MakeRaycast(true);
            }

            // Update states
            firstCell.UpdateMyItem();
            secondCell.UpdateMyItem();
            //Debug.Log(secondItem.GetComponent<PorteScript>().IsSortie());
            //firstCell.UpdateBackgroundState();
            //secondCell.UpdateBackgroundState();
        }
    }
示例#3
0
    /// <summary>
    /// Swap items between to cells
    /// </summary>
    /// <param name="firstCell"> Cell </param>
    /// <param name="secondCell"> Cell </param>
    public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
    {
        if ((firstCell != null) && (secondCell != null))
        {
            DragAndDropItem firstItem  = firstCell.GetItem();               // Get item from first cell
            DragAndDropItem secondItem = secondCell.GetItem();              // Get item from second cell

            ItemSlot slot1 = firstCell.GetComponent <ItemSlot>();
            ItemSlot slot2 = secondCell.GetComponent <ItemSlot>();

            Item  item1  = slot1.item;
            Image image1 = slot1.image;
            Text  text1  = slot1.counterText;

            Item  item2  = slot2.item;
            Image image2 = slot2.image;
            Text  text2  = slot2.counterText;

            if (firstItem != null)
            {
                slot1.item        = item2;
                slot1.image       = image2;
                slot1.counterText = text2;
                // Place first item into second cell
                firstItem.transform.SetParent(secondCell.transform, false);
                firstItem.transform.localPosition = Vector3.zero;
                secondCell.SetBackgroundState(true);
            }
            if (secondItem != null)
            {
                slot2.item        = item1;
                slot2.image       = image1;
                slot2.counterText = text1;
                // Place second item into first cell
                secondItem.transform.SetParent(firstCell.transform, false);
                secondItem.transform.localPosition = Vector3.zero;
                firstCell.SetBackgroundState(true);
            }
        }
    }
示例#4
0
 /// <summary>
 /// Swap items between to cells
 /// </summary>
 /// <param name="firstCell"> Cell </param>
 /// <param name="secondCell"> Cell </param>
 public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
 {
     if ((firstCell != null) && (secondCell != null))
     {
         DragAndDropItem firstItem  = firstCell.GetItem();               // Get item from first cell
         DragAndDropItem secondItem = secondCell.GetItem();              // Get item from second cell
         if (firstItem != null)
         {
             // Place first item into second cell
             firstItem.transform.SetParent(secondCell.transform, false);
             firstItem.transform.localPosition = Vector3.zero;
             secondCell.SetBackgroundState(true);
         }
         if (secondItem != null)
         {
             // Place second item into first cell
             secondItem.transform.SetParent(firstCell.transform, false);
             secondItem.transform.localPosition = Vector3.zero;
             firstCell.SetBackgroundState(true);
         }
     }
 }
示例#5
0
    /// <summary>
    /// Swap items between two cells
    /// </summary>
    /// <param name="firstCell"> Cell </param>
    /// <param name="secondCell"> Cell </param>
    public SwapType SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
    {
        var swapType = SwapType.None;

        if ((firstCell != null) && (secondCell != null))
        {
            DragAndDropItem firstDragAndDropItem  = firstCell.GetItem();               // Get item from first cell
            DragAndDropItem secondDragAndDropItem = secondCell.GetItem();              // Get item from second cell

            //try stack
            var isStacked = false;
            if (firstDragAndDropItem != null && secondDragAndDropItem != null)
            {
                var firstItemBehaviour  = firstDragAndDropItem.GetComponent <SlotItemBehaviour>();
                var secondItemBehaviour = secondDragAndDropItem.GetComponent <SlotItemBehaviour>();
                if (firstItemBehaviour != null && secondItemBehaviour != null)
                {
                    if (firstItemBehaviour.Item.Id.Equals(secondItemBehaviour.Item.Id))
                    {
                        if (secondItemBehaviour.Item.Quantity < secondItemBehaviour.Item.MaxStack)
                        {
                            //requirements match for stack we can stack here
                            var stackableAmount = secondItemBehaviour.Item.MaxStack - secondItemBehaviour.Item.Quantity;

                            if (firstItemBehaviour.Item.Quantity <= stackableAmount)
                            {
                                // all of them will be stacked to second cell
                                var inventoryBehaviour = secondItemBehaviour.GetComponentInParent <InventoryBehavior>();
                                var itemToStack        = inventoryBehaviour.ItemDatabase.getItemByID(firstItemBehaviour.Item.Id);
                                itemToStack.Quantity = firstItemBehaviour.Item.Quantity;
                                secondItemBehaviour.Stack(itemToStack);



                                //remove first Item slot
                                var firstItemSlotBehaviour = firstItemBehaviour.GetComponentInParent <SlotBehaviour>();
                                firstItemSlotBehaviour.RemoveItem();

                                swapType = SwapType.FullyStacked;
                            }
                            else
                            {
                                var initialFirstAmount  = firstItemBehaviour.Item.Quantity;
                                var initialSecondAmount = secondItemBehaviour.Item.Quantity;

                                secondItemBehaviour.Item.Quantity = firstItemBehaviour.Item.MaxStack;
                                firstItemBehaviour.Item.Quantity  = initialSecondAmount - (secondItemBehaviour.Item.MaxStack - initialFirstAmount);

                                firstItemBehaviour.SetItemAmount(firstItemBehaviour.Item.Quantity);
                                secondItemBehaviour.SetItemAmount(secondItemBehaviour.Item.Quantity);

                                if (firstItemBehaviour.Item.Quantity == 0)
                                {
                                    var firstItemSlotBehaviour = firstItemBehaviour.GetComponentInParent <SlotBehaviour>();
                                    firstItemSlotBehaviour.RemoveItem();
                                }

                                swapType = SwapType.PartiallyStacked;
                            }


                            isStacked = true;
                            //stacking process is done here
                        }
                    }
                }
            }

            if (!isStacked)
            {
                // Swap items
                if (firstDragAndDropItem != null)
                {
                    firstDragAndDropItem.transform.SetParent(secondCell.transform, false);
                    firstDragAndDropItem.transform.localPosition = Vector3.zero;
                    firstDragAndDropItem.MakeRaycast(true);
                }
                if (secondDragAndDropItem != null)
                {
                    secondDragAndDropItem.transform.SetParent(firstCell.transform, false);
                    secondDragAndDropItem.transform.localPosition = Vector3.zero;
                    secondDragAndDropItem.MakeRaycast(true);
                }

                swapType = SwapType.Swapped;
            }

            firstCell.UpdateMyItem();
            firstCell.UpdateBackgroundState();

            secondCell.UpdateMyItem();
            secondCell.UpdateBackgroundState();
        }
        return(swapType);
    }
 /// <summary>
 /// Swap items between to cells
 /// </summary>
 /// <param name="firstCell"> Cell </param>
 /// <param name="secondCell"> Cell </param>
 public void SwapItems(DragAndDropCell firstCell, DragAndDropCell secondCell)
 {
     if ((firstCell != null) && (secondCell != null))
     {
         DragAndDropItem firstItem = firstCell.GetItem();                // Get item from first cell
         DragAndDropItem secondItem = secondCell.GetItem();              // Get item from second cell
         if (firstItem != null)
         {
             // Place first item into second cell
             firstItem.transform.SetParent(secondCell.transform, false);
             firstItem.transform.localPosition = Vector3.zero;
             secondCell.SetBackgroundState(true);
         }
         if (secondItem != null)
         {
             // Place second item into first cell
             secondItem.transform.SetParent(firstCell.transform, false);
             secondItem.transform.localPosition = Vector3.zero;
             firstCell.SetBackgroundState(true);
         }
     }
 }