Ejemplo n.º 1
0
    /// <summary>
    /// Stack event handler.
    /// </summary>
    /// <returns>The handler.</returns>
    /// <param name="desc">Desc.</param>
    private IEnumerator EventHandler(DadCell.DadEventDescriptor desc)
    {
        StackGroup sourceStackGroup = Gets.GetComponentInParent <StackGroup>(desc.sourceCell.transform);
        StackGroup destStackGroup   = Gets.GetComponentInParent <StackGroup>(desc.destinationCell.transform);

        if (sourceStackGroup == null || destStackGroup == null)
        {
            desc.groupPermission = false;
            // Send stack event notification
            SendNotification(sourceStackGroup != null ? sourceStackGroup.gameObject : null, destStackGroup != null ? destStackGroup.gameObject : null);
            myState = MyState.WaitForRequest;
            yield break;
        }

        StackCell myStackCell    = desc.destinationCell.GetComponent <StackCell>();
        StackCell theirStackCell = desc.sourceCell.GetComponent <StackCell>();

        if (myStackCell == null || theirStackCell == null)
        {
            desc.groupPermission = false;
            // Send stack event notification
            SendNotification(sourceStackGroup.gameObject, destStackGroup.gameObject);
            myState = MyState.WaitForRequest;
            yield break;
        }

        StackItem myStackItem    = myStackCell.GetStackItem();
        StackItem theirStackItem = theirStackCell.GetStackItem();

        PriceItem  priceItem = theirStackItem.GetComponent <PriceItem>();
        PriceGroup buyer     = Gets.GetComponentInParent <PriceGroup>(desc.destinationCell.transform);
        PriceGroup seller    = Gets.GetComponentInParent <PriceGroup>(desc.sourceCell.transform);

        AudioClip itemSound = theirStackItem.sound;                                                                             // Item's SFX

        int amount = theirStackItem.GetStack();                                                                                 // Item's stack amount

        if ((globalSplit == true) ||
            (sourceStackGroup != destStackGroup && (sourceStackGroup.splitOuter == true || destStackGroup.splitOuter == true)))
        {
            // Need to use split interface
            if (splitInterface != null)
            {
                if (priceItem != null && buyer != null && seller != null && buyer != seller)
                {
                    // Split with prices
                    splitInterface.ShowSplitter(theirStackItem, priceItem);
                }
                else
                {
                    // Split without prices
                    splitInterface.ShowSplitter(theirStackItem, null);
                }
                // Show split interface and wait while it is active
                while (splitInterface.gameObject.activeSelf == true)
                {
                    yield return(new WaitForEndOfFrame());
                }
                // Get splitted stack amount
                amount = splitInterface.GetRightAmount();
            }
        }

        if (amount > 0)
        {
            if (sourceStackGroup != destStackGroup &&
                (destStackGroup.arrangeMode == true || sourceStackGroup.arrangeMode == true))
            {
                // Split in arrange mode between different stack groups
                if (priceItem != null && buyer != null && seller != null && buyer != seller)
                {
                    // Different price groups
                    if (buyer.GetCash() > priceItem.GetPrice() * amount)
                    {
                        // Has anough cash
                        int distributed = DistributeAnywhere(theirStackItem, amount, null);
                        if (distributed > 0)
                        {
                            int totalPrice = priceItem.GetPrice() * distributed;
                            seller.AddCash(totalPrice);
                            buyer.SpendCash(totalPrice);

                            buyer.UpdatePrices();
                        }
                    }
                }
                else
                {
                    // Same price group
                    DistributeAnywhere(theirStackItem, amount, null);
                }
            }
            else
            {
                // Inside same stack group transactions disabled in arrange mode
                if (arrangeMode == false)
                {
                    if (myStackItem != null)
                    {
                        // Check if items allowed for cells
                        if (SortCell.IsSortAllowed(myStackCell.gameObject, theirStackItem.gameObject) == true &&
                            SortCell.IsSortAllowed(theirStackCell.gameObject, myStackItem.gameObject) == true)
                        {
                            // Destination cell already has item
                            if (myStackCell.HasSameItem(theirStackItem) == true)
                            {
                                // Same item
                                myStackCell.UniteStack(theirStackItem, amount);
                            }
                            else
                            {
                                // Different items. Try to swap items between cells
                                if (myStackCell.SwapStacks(theirStackCell) == true)
                                {
                                    // Swap successful
                                    theirStackItem = theirStackCell.GetStackItem();
                                    if (theirStackItem != null)
                                    {
                                        // Distribute item after swap
                                        DistributeInItems(theirStackItem, theirStackItem.GetStack(), theirStackCell);
                                    }
                                }
                                else
                                {
                                    // Swap unsuccessful.
                                    // Try to distribute item between other cells to make cell free
                                    DistributeAnywhere(myStackItem, myStackItem.GetStack(), myStackCell);
                                    myStackItem = myStackCell.GetStackItem();
                                    if (myStackItem != null)
                                    {
                                        // Item still in cell. Try to place item in other group's cells
                                        sourceStackGroup.DistributeAnywhere(myStackItem, myStackItem.GetStack(), null);
                                        myStackItem = myStackCell.GetStackItem();
                                        if (myStackItem == null)
                                        {
                                            // Item was placed into other cell and now this cell is free
                                            // Place item into destination cell
                                            myStackCell.UniteStack(theirStackItem, amount);
                                        }
                                    }
                                    else
                                    {
                                        // Item was placed into other cell and now this cell is free
                                        // Place item into destination cell
                                        myStackCell.UniteStack(theirStackItem, amount);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Destination cell has no item
                        // Place item into destination cell
                        myStackCell.UniteStack(theirStackItem, amount);
                    }
                }
            }
        }
        // Send stack event notification
        SendNotification(sourceStackGroup.gameObject, destStackGroup.gameObject);
        if (trashBinMode == true)
        {
            // In trash bin mode just destroy item
            desc.destinationCell.RemoveItem();
            PlaySound(trashBinSound);
        }
        else
        {
            PlaySound(itemSound);
        }
        myState = MyState.WaitForRequest;
    }