/// <summary> /// Swaps the stacks between cells. /// </summary> /// <returns><c>true</c>, if stacks was swaped, <c>false</c> otherwise.</returns> /// <param name="sourceStackCell">Source stack cell.</param> public bool SwapStacks(StackCell sourceStackCell) { print(6); bool res = false; if (sourceStackCell != null) { StackItem myStackItem = GetStackItem(); StackItem theirStackItem = sourceStackCell.GetStackItem(); if (myStackItem != null && theirStackItem != null) { if (SortCell.IsSortAllowed(gameObject, theirStackItem.gameObject) == true && SortCell.IsSortAllowed(sourceStackCell.gameObject, myStackItem.gameObject) == true) { if (cellStackLimit >= theirStackItem.GetStack() && sourceStackCell.cellStackLimit >= myStackItem.GetStack()) { DadCell.SwapItems(gameObject, sourceStackCell.gameObject); res = true; } } } } return(res); }
/// <summary> /// Unites item in stack with this cell. /// </summary> /// <returns>The stack.</returns> /// <param name="stackItem">Stack item.</param> /// <param name="limit">Stack limit.</param> public int UniteStack(StackItem stackItem, int limit) { print(5); int res = 0; if (stackItem != null) { int allowedSpace = GetAllowedSpace(); StackItem myStackItem = GetStackItem(); if (myStackItem == null) // Cell has no item { if (SortCell.IsSortAllowed(gameObject, stackItem.gameObject) == true) { // Create new stack item to put it into this cell StackItem newStackItem = Instantiate(stackItem); newStackItem.name = stackItem.name; DadCell.AddItem(gameObject, newStackItem.gameObject); // Check the correct amout of united item int stackDelta = Mathf.Min(stackItem.GetStack(), allowedSpace, limit); newStackItem.SetStack(stackDelta); stackItem.ReduceStack(stackDelta); res = stackDelta; } } else if (HasSameItem(stackItem) == true) // Cell has same item { int stackDelta = Mathf.Min(stackItem.GetStack(), allowedSpace, limit); myStackItem.AddStack(stackDelta); stackItem.ReduceStack(stackDelta); res = stackDelta; } } return(res); }
/// <summary> /// Unites item in stack with this cell. /// </summary> /// <returns>The stack.</returns> /// <param name="stackItem">Stack item.</param> /// <param name="limit">Stack limit.</param> public int UniteStack(StackItem stackItem, int limit) { int res = 0; if (stackItem != null) { int allowedSpace = GetAllowedSpace(); StackItem myStackItem = GetStackItem(); if (myStackItem == null) // Cell has no item { if (SortCell.IsSortAllowed(gameObject, stackItem.gameObject) == true) // Item type is allowed for this cell { if (stackItem.GetStack() == limit && allowedSpace >= limit) // Cell has anough space for all item's stack { // Totaly place item in new cell DadCell sourceDadCell = Gets.GetComponentInParent <DadCell>(stackItem.transform); if (sourceDadCell != null) { DadCell.SwapItems(gameObject, sourceDadCell.gameObject); } else { DadCell.AddItem(gameObject, stackItem.gameObject); } res = limit; } else // Only part of item stack will be placed into new cell { // Create new stack item to put it into this cell StackItem newStackItem = Instantiate(stackItem); newStackItem.name = stackItem.name; DadCell.AddItem(gameObject, newStackItem.gameObject); // Check the correct amout of united item int stackDelta = Mathf.Min(stackItem.GetStack(), allowedSpace, limit); newStackItem.SetStack(stackDelta); stackItem.ReduceStack(stackDelta); res = stackDelta; } } } else if (HasSameItem(stackItem) == true) // Cell has same item { int stackDelta = Mathf.Min(stackItem.GetStack(), allowedSpace, limit); myStackItem.AddStack(stackDelta); stackItem.ReduceStack(stackDelta); res = stackDelta; } } return(res); }
/// <summary> /// Determines if is item allowed the specified sort. /// </summary> /// <returns><c>true</c> if is sort allowed the specified cell item; otherwise, <c>false</c>.</returns> /// <param name="cell">Cell.</param> /// <param name="item">Item.</param> public static bool IsSortAllowed(GameObject cell, GameObject item) { bool res = false; if (cell != null && item != null) { res = true; SortCell sortCell = cell.GetComponent <SortCell>(); if (sortCell != null) { res = sortCell.IsSortAllowed(item); } } return(res); }
/// <summary> /// Gets the free stack cells. /// </summary> /// <returns>The free stack cells.</returns> /// <param name="stackItem">Stack item.</param> public List <StackCell> GetFreeStackCells(StackItem stackItem) { List <StackCell> res = new List <StackCell>(); if (stackItem != null) { foreach (StackCell stackCell in GetComponentsInChildren <StackCell>()) { if (stackCell.GetStackItem() == null) { if (SortCell.IsSortAllowed(stackCell.gameObject, stackItem.gameObject) == true) { res.Add(stackCell); } } } } return(res); }
/// <summary> /// Gets the similar stack items (with same sort). /// </summary> /// <returns>The similar stack items.</returns> /// <param name="stackItem">Stack item.</param> public List <StackItem> GetSimilarStackItems(StackItem stackItem) { print(7); List <StackItem> res = new List <StackItem>(); if (stackItem != null) { foreach (StackCell stackCell in GetComponentsInChildren <StackCell>()) { StackItem sameStackItem = stackCell.GetStackItem(); if (sameStackItem != null) { if (SortCell.IsSortAllowed(stackCell.gameObject, stackItem.gameObject) == true) { res.Add(sameStackItem); } } } } return(res); }
/// <summary> /// Stack event handler. /// </summary> /// <returns>The handler.</returns> /// <param name="desc">Desc.</param> private IEnumerator EventHandler(DadCell.DadEventDescriptor desc) { StackGroup sourceStackGroup = desc.sourceCell.GetComponentInParent <StackGroup>(); StackGroup destStackGroup = desc.destinationCell.GetComponentInParent <StackGroup>(); StackCell myStackCell = desc.destinationCell.GetComponent <StackCell>(); StackCell theirStackCell = desc.sourceCell.GetComponent <StackCell>(); StackItem myStackItem = myStackCell.GetStackItem(); StackItem theirStackItem = theirStackCell.GetStackItem(); PriceItem priceItem = theirStackItem.GetComponent <PriceItem>(); PriceGroup buyer = desc.destinationCell.GetComponentInParent <PriceGroup>(); PriceGroup seller = desc.sourceCell.GetComponentInParent <PriceGroup>(); 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 ain 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 { audioSource.PlayOneShot(itemSound); } myState = MyState.WaitForRequest; }
/// <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; }