/// <summary> /// Updates stack's condition. /// </summary> private void UpdateCondition() { int stack = GetStack(); if (stack > 1) { ShowStack(); } else if (stack == 1) { // Hide stack text if stack == 0 HideStack(); } else { // Stack <= 0 DadCell dadCell = Gets.GetComponentInParent <DadCell>(transform); if (dadCell != null) { dadCell.RemoveItem(); } else { Destroy(gameObject); } } }
/// <summary> /// Raises the item click event. /// </summary> /// <param name="item">Item.</param> public void OnItemUse(GameObject item) { if (item != null) { StackItem stackItem = item.GetComponent <StackItem>(); if (stackItem != null) { StackGroup sourceGroup = Gets.GetComponentInParent <StackGroup>(item.transform); if (sourceGroup != null && (sourceGroup == firstGroup || sourceGroup == secondGroup)) { StackGroup destinationGroup = sourceGroup == firstGroup ? secondGroup : firstGroup; // Try to place item into free space of specified stack group if (destinationGroup.AddItem(stackItem, stackItem.GetStack()) <= 0) { // If group have no free space for item // Get similar items in that group List <StackItem> similarItems = destinationGroup.GetSimilarStackItems(stackItem); if (similarItems.Count > 0) { // Try to replace with first similar item destinationGroup.ReplaceItems(similarItems[0], stackItem, sourceGroup); } } } } } }
/// <summary> /// Remove quick item. /// </summary> public void Remove() { DadCell dadCell = Gets.GetComponentInParent <DadCell>(transform); if (dadCell != null) { dadCell.RemoveItem(); } else { Destroy(gameObject); } }
/// <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> /// Adds the item. /// </summary> /// <returns>The item.</returns> /// <param name="stackItem">Stack item.</param> /// <param name="limit">Limit.</param> public int AddItem(StackItem stackItem, int limit) { int res = 0; if (stackItem != null) { StackGroup sourceStackGroup = Gets.GetComponentInParent <StackGroup>(stackItem.transform); // Try to distribute item inside group's items and cells res += DistributeAnywhere(stackItem, limit, null); // Send stack event notification SendNotification(sourceStackGroup != null ? sourceStackGroup.gameObject : null, gameObject); if (res > 0) { PlaySound(stackItem.sound); } } return(res); }
/// <summary> /// Raises the DaD group event. /// </summary> /// <param name="desc">Desc.</param> private void OnDadGroupEvent(DadCell.DadEventDescriptor desc) { switch (desc.triggerType) { case DadCell.TriggerType.DragGroupRequest: case DadCell.TriggerType.DropGroupRequest: if (myState == MyState.WaitForRequest) { // Disable standard DaD logic desc.groupPermission = false; myState = MyState.WaitForEvent; } break; case DadCell.TriggerType.DragEnd: if (myState == MyState.WaitForEvent) { StackGroup sourceStackControl = Gets.GetComponentInParent <StackGroup>(desc.sourceCell.transform); StackGroup destStackControl = Gets.GetComponentInParent <StackGroup>(desc.destinationCell.transform); if (sourceStackControl != destStackControl) { // If this group is source group - do nothing myState = MyState.WaitForRequest; } } break; case DadCell.TriggerType.DropEnd: if (myState == MyState.WaitForEvent) { // If this group is destination group myState = MyState.Busy; // Operate item's drop StartCoroutine(EventHandler(desc)); } break; } }
/// <summary> /// Gets the stack cell of this item. /// </summary> /// <returns>The stack cell.</returns> public StackCell GetStackCell() { return(Gets.GetComponentInParent <StackCell>(transform)); }
/// <summary> /// Gets DaD cell which contains this item. /// </summary> /// <returns>The cell.</returns> public DadCell GetCell() { return(Gets.GetComponentInParent <DadCell>(transform)); }
/// <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; }