Пример #1
0
    /// <summary>
    /// Adds existing item.
    /// </summary>
    /// <returns>The item.</returns>
    /// <param name="stackItem">Stack item.</param>
    /// <param name="limit">Limit.</param>
    public int AddExistingItem(StackItem stackItem, int limit)
    {
        int res = 0;

        if (stackItem != null)
        {
            // Init internal state (just in case it was not initted before)
            stackItem.Init();

            StackGroup sourceStackGroup = AccessUtility.GetComponentInParent <StackGroup>(stackItem.transform);
            StackCell  sourceStackCell  = stackItem.GetStackCell();

            StackGroupEventDescriptor desc = new StackGroupEventDescriptor();
            // Try to distribute item inside group's items and cells
            DistributeResults distributeResults = DistributeAnywhere(stackItem, limit, null);
            res += distributeResults.amount;
            // Send stack event notification
            if (res > 0)
            {
                PlaySound(stackItem.sound);

                desc.sourceGroup      = sourceStackGroup;
                desc.destinationGroup = this;
                desc.sourceCell       = sourceStackCell;
                desc.destinationCells = distributeResults.cells;
                SendNotification(desc);
            }
        }
        return(res);
    }
Пример #2
0
    /// <summary>
    /// Distribute destination item in source group than place source item in just vacated cell.
    /// </summary>
    /// <returns><c>true</c>, if item was replaced, <c>false</c> otherwise.</returns>
    /// <param name="sourceStackItem">Source stack item.</param>
    /// <param name="destinationStackItem">Destination stack item.</param>
    public bool ReplaceItems(StackItem sourceStackItem, StackItem destinationStackItem)
    {
        bool       res                  = false;
        StackCell  sourceStackCell      = sourceStackItem.GetStackCell();
        StackCell  destinationStackCell = destinationStackItem.GetStackCell();
        StackGroup sourceStackGroup     = AccessUtility.GetComponentInParent <StackGroup>(sourceStackItem.transform);

        if (sourceStackItem != null && destinationStackItem != null && sourceStackCell != null && destinationStackCell != null && sourceStackGroup != null)
        {
            StackGroupEventDescriptor distributeDesc = new StackGroupEventDescriptor();
            StackGroupEventDescriptor swapDesc       = new StackGroupEventDescriptor();

            // Try distribute item from destination cell into source group
            DistributeResults distributeResults = sourceStackGroup.DistributeAnywhere(destinationStackItem, destinationStackItem.GetStack(), null);

            if (distributeResults.amount > 0)
            {
                distributeDesc.sourceGroup      = this;
                distributeDesc.destinationGroup = sourceStackGroup;
                distributeDesc.sourceCell       = destinationStackCell;
                distributeDesc.destinationCells = distributeResults.cells;

                // If destination cell is empty now
                if (destinationStackCell.GetStackItem() == null)
                {
                    // Place source item into it
                    if (destinationStackCell.UniteStack(sourceStackItem, sourceStackItem.GetStack()) > 0)
                    {
                        PlaySound(sourceStackItem.sound);

                        swapDesc.sourceGroup      = sourceStackGroup;
                        swapDesc.destinationGroup = this;
                        swapDesc.sourceCell       = sourceStackCell;
                        swapDesc.destinationCells.Add(destinationStackCell);

                        res = true;
                    }
                }
            }

            if (distributeResults.amount > 0)
            {
                // Send distribute stack event notification
                SendNotification(distributeDesc);
                if (res == true)
                {
                    // Send swap stack item event notification
                    SendNotification(swapDesc);
                }
            }
        }
        return(res);
    }
Пример #3
0
    /// <summary>
    /// Removes the item.
    /// </summary>
    /// <param name="stackItem">Stack item.</param>
    /// <param name="limit">Limit.</param>
    public void RemoveItem(StackItem stackItem, int limit)
    {
        if (stackItem != null)
        {
            StackCell stackCell = stackItem.GetStackCell();

            PlaySound(trashBinSound);
            stackItem.ReduceStack(limit);

            StackGroupEventDescriptor desc = new StackGroupEventDescriptor();
            desc.sourceGroup      = this;
            desc.destinationGroup = this;
            desc.sourceCell       = stackCell;
            desc.destinationCells.Add(stackCell);
            SendNotification(desc);
        }
    }
Пример #4
0
    /// <summary>
    /// Create item from prefab and distribute it insede group.
    /// </summary>
    /// <returns>Distributed items amount.</returns>
    /// <param name="stackItemPrefab">Stack item prefab.</param>
    /// <param name="stackAmount">Stack amount for created item.</param>
    public int AddItemFromPrefab(StackItem stackItemPrefab, int stackAmount)
    {
        int res = 0;

        Debug.Log(stackItemPrefab);
        if (stackItemPrefab != null)
        {
            StackGroupEventDescriptor desc = new StackGroupEventDescriptor();
            // Create item
            StackItem stackItem = Instantiate(stackItemPrefab);
            stackItem.name = stackItemPrefab.name;
            // Set stack amount
            stackAmount = Mathf.Min(stackAmount, stackItem.maxStack);
            stackItem.SetStack(stackAmount);
            // Init inernal state
            stackItem.Init();
            // Try to distribute item inside group's items and cells
            DistributeResults distributeResults = DistributeAnywhere(stackItem, stackAmount, null);
            Debug.Log(distributeResults);
            res += distributeResults.amount;
            if (stackItem.GetStackCell() == null)
            {
                // Destroy item if it has no cell (was not distributed completed)
                Destroy(stackItem.gameObject);
            }
            // Send stack event notification
            if (res > 0)
            {
                PlaySound(stackItem.sound);

                desc.sourceGroup      = this;
                desc.destinationGroup = this;
                desc.sourceCell       = distributeResults.cells[0];
                desc.destinationCells = distributeResults.cells;
                SendNotification(desc);
            }
        }
        return(res);
    }
Пример #5
0
 /// <summary>
 /// Sends the stack event notification.
 /// </summary>
 /// <param name="desc">Event descriptor.</param>
 private void SendNotification(StackGroupEventDescriptor desc)
 {
     if (desc.sourceGroup != null)
     {
         // Send notification to source GO
         AccessUtility.SendMessageUpwards(desc.sourceGroup.transform, "OnStackGroupEvent", desc);
         foreach (GameObject receiver in desc.sourceGroup.eventAdditionalReceivers)
         {
             // Send notification to additionaly specified GOs
             AccessUtility.SendMessage(receiver.transform, "OnStackGroupEvent", desc);
         }
     }
     if (desc.destinationGroup != null && desc.sourceGroup != desc.destinationGroup && desc.destinationGroup.trashBinMode == false)
     {
         // Send notification to destination GO
         AccessUtility.SendMessageUpwards(desc.destinationGroup.transform, "OnStackGroupEvent", desc);
         foreach (GameObject receiver in desc.destinationGroup.eventAdditionalReceivers)
         {
             // Send notification to additionaly specified GOs
             AccessUtility.SendMessage(receiver.transform, "OnStackGroupEvent", desc);
         }
     }
 }
Пример #6
0
    /// <summary>
    /// Stack event handler.
    /// </summary>
    /// <returns>The handler.</returns>
    /// <param name="desc">Desc.</param>
    private IEnumerator EventHandler(DadCell.DadEventDescriptor dadDesc)
    {
        StackGroup sourceStackGroup = AccessUtility.GetComponentInParent <StackGroup>(dadDesc.sourceCell.transform);
        StackGroup destStackGroup   = AccessUtility.GetComponentInParent <StackGroup>(dadDesc.destinationCell.transform);

        if (sourceStackGroup == null || destStackGroup == null)
        {
            dadDesc.groupPermission = false;
            myState = MyState.WaitForRequest;
            yield break;
        }

        StackCell destStackCell   = dadDesc.destinationCell.GetComponent <StackCell>();
        StackCell sourceStackCell = dadDesc.sourceCell.GetComponent <StackCell>();

        if (destStackCell == null || sourceStackCell == null)
        {
            dadDesc.groupPermission = false;
            myState = MyState.WaitForRequest;
            yield break;
        }

        StackItem destStackItem   = destStackCell.GetStackItem();
        StackItem sourceStackItem = sourceStackCell.GetStackItem();

        StackGroupEventDescriptor stackDescPrimary = new StackGroupEventDescriptor();   // Stack event info

        stackDescPrimary.sourceGroup      = sourceStackGroup;
        stackDescPrimary.destinationGroup = destStackGroup;
        stackDescPrimary.sourceCell       = sourceStackCell;

        StackGroupEventDescriptor stackDescSecondary = new StackGroupEventDescriptor(); // One more stack event info in case if destination cell is not empty and items were swapped

        stackDescSecondary.sourceGroup      = destStackGroup;
        stackDescSecondary.destinationGroup = sourceStackGroup;
        stackDescSecondary.sourceCell       = destStackCell;

        DistributeResults distributeResults = new DistributeResults();                  // Info with results of stack item distribution in stack group

        PriceItem  priceItem = sourceStackItem.GetComponent <PriceItem>();
        PriceGroup buyer     = AccessUtility.GetComponentInParent <PriceGroup>(dadDesc.destinationCell.transform);
        PriceGroup seller    = AccessUtility.GetComponentInParent <PriceGroup>(dadDesc.sourceCell.transform);

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

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

        if (amount > 1)
        {
            // If item's stack > 1 try to use split interface
            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(sourceStackItem, priceItem);
                    }
                    else
                    {
                        // Split without prices
                        splitInterface.ShowSplitter(sourceStackItem, 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 ((long)buyer.GetCash() >= (long)priceItem.GetPrice() * amount)
                    {
                        // Has anough cash
                        distributeResults = DistributeAnywhere(sourceStackItem, amount, null);
                        if (distributeResults.amount > 0)
                        {
                            stackDescPrimary.destinationCells = distributeResults.cells;

                            int totalPrice = priceItem.GetPrice() * distributeResults.amount;
                            seller.AddCash(totalPrice);
                            buyer.SpendCash(totalPrice);

                            buyer.UpdatePrices();
                        }
                    }
                }
                else
                {
                    // Same price group
                    distributeResults = DistributeAnywhere(sourceStackItem, amount, null);
                    if (distributeResults.amount > 0)
                    {
                        stackDescPrimary.destinationCells = distributeResults.cells;
                    }
                }
            }
            else
            {
                // Inside same stack group transactions disabled in arrange mode
                if (arrangeMode == false)
                {
                    if (destStackItem != null)
                    {
                        // Check if items allowed for destination cell
                        if (SortCell.IsSortAllowed(destStackCell.gameObject, sourceStackItem.gameObject) == true)

                        {
                            // Destination cell already has same item
                            if (destStackCell.HasSameItem(sourceStackItem) == true)
                            {
                                if (destStackCell.UniteStack(sourceStackItem, amount) > 0)
                                {
                                    stackDescPrimary.destinationCells.Add(destStackCell);
                                }
                            }
                            // Check if items allowed for source cell
                            else if (SortCell.IsSortAllowed(sourceStackCell.gameObject, destStackItem.gameObject) == true)
                            {
                                // Different items. Try to swap items between cells
                                if (destStackCell.SwapStacks(sourceStackCell) == true)
                                {
                                    // Swap successful
                                    stackDescSecondary.destinationCells.Add(sourceStackCell);
                                    sourceStackItem = sourceStackCell.GetStackItem();
                                    if (sourceStackItem != null)
                                    {
                                        // Distribute item after swap
                                        distributeResults = DistributeInItems(sourceStackItem, sourceStackItem.GetStack(), destStackCell);
                                        if (distributeResults.amount > 0)
                                        {
                                            stackDescPrimary.destinationCells = distributeResults.cells;
                                        }
                                        if (destStackCell.GetStackItem() != null)
                                        {
                                            // If stack item (or part of it) in destination cell
                                            stackDescPrimary.destinationCells.Add(destStackCell);
                                        }
                                    }
                                }
                                else
                                {
                                    // Swap unsuccessful.
                                    // Try to distribute item between other cells to make cell free
                                    distributeResults = DistributeAnywhere(destStackItem, destStackItem.GetStack(), destStackCell);
                                    if (distributeResults.amount > 0)
                                    {
                                        stackDescSecondary.destinationCells = distributeResults.cells;
                                    }
                                    destStackItem = destStackCell.GetStackItem();
                                    if (destStackItem != null)
                                    {
                                        // Item still in cell. Try to place item in other group's cells
                                        distributeResults = sourceStackGroup.DistributeAnywhere(destStackItem, destStackItem.GetStack(), null);
                                        if (distributeResults.amount > 0)
                                        {
                                            stackDescSecondary.destinationCells.AddRange(distributeResults.cells);
                                        }
                                        destStackItem = destStackCell.GetStackItem();
                                        if (destStackItem == null)
                                        {
                                            // Item was placed into other cell and now this cell is free
                                            // Place item into destination cell
                                            if (destStackCell.UniteStack(sourceStackItem, amount) > 0)
                                            {
                                                stackDescPrimary.destinationCells.Add(destStackCell);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // Item was placed into other cell and now this cell is free
                                        // Place item into destination cell
                                        if (destStackCell.UniteStack(sourceStackItem, amount) > 0)
                                        {
                                            stackDescPrimary.destinationCells.Add(destStackCell);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Destination cell has no item
                        // Place item into destination cell
                        if (destStackCell.UniteStack(sourceStackItem, amount) > 0)
                        {
                            stackDescPrimary.destinationCells.Add(destStackCell);
                        }
                    }
                }
            }
        }

        // Send stack event notifications
        if (stackDescSecondary.destinationCells.Count > 0)
        {
            SendNotification(stackDescSecondary);
        }
        if (stackDescPrimary.destinationCells.Count > 0)
        {
            SendNotification(stackDescPrimary);
            if (trashBinMode == true)
            {
                // In trash bin mode just destroy item
                dadDesc.destinationCell.RemoveItem();
                PlaySound(trashBinSound);
            }
            else
            {
                PlaySound(itemSound);
            }
        }

        myState = MyState.WaitForRequest;
        eventHandkerCoroutine = null;
    }