bool tryBuy(UIItem uitem, bool stack) { PraeItem pitem = uitem._item; int amount = (stack) ? System.Math.Min(pitem.amount, pitem.stackSize) : 1; float itemWeight = pitem.weightSingle * amount; // weight constraint if (_buyer.weight + itemWeight > _buyer.maxWeight) { Debug.Log("Cannot add item: inventory weight constraint violated!"); return false; } // money constraint if (!_buyer.money.CanPay(uitem._item.value)) { Debug.Log("Insufficient money!"); return false; } // it can fit based on size int rest = _buyer.AddItem(new PraeItem(pitem, amount), true, false); Debug.Assert(rest == 0, "Adding item to inventory was unsuccessful!"); uitem._item.amount -= (amount - rest); uitem.Set(uitem._item); // update labels if (uitem._item.amount <= 0) { selectedUIItem.ResetColor(); selectedUIItem = null; // deselect item // NOTE: this is by far not the most efficient way of doing this - but whats the point in optimising shops anyway if (userMode == EShopMode.buy) _seller.RemoveItem(page * 10 + uitem.id); else if (userMode == EShopMode.rebuy) _seller.RemoveBoughtItem(page * 10 + uitem.id); else Debug.LogError("Cannot buy item in wrong mode: " + userMode); calcPagedItems(); // refresh PagedItems if (page > maxPage) page = maxPage; SetUpPage(page); // update UI } // pay _buyer.money.Pay(uitem._item.value); _seller.money.Add(uitem._item.value); UIbuyerMoney.Set(_buyer.G, _buyer.K, _buyer.T); return true; }
bool trySell(UIItem uitem, bool stack) { PraeItem pitem = uitem._item; int amount = (stack) ? System.Math.Min(pitem.amount, pitem.stackSize) : 1; float itemWeight = pitem.weightSingle * amount; // money constraint of shop if (!_seller.money.CanPay(uitem._item.value)) { Debug.Log("Shop has insufficient money!"); return false; } _seller.AddBoughtItem(new PraeItem(pitem, amount)); uitem._item.amount -= amount; uitem.Set(uitem._item); // update labels if (uitem._item.amount <= 0) { selectedUIItem.ResetColor(); selectedUIItem = null; // deselect item // NOTE: this is by far not the most efficient way of doing this - but whats the point in optimising shops anyway _buyer.RemoveItem(page * 10 + uitem.id); calcPagedItems(); if (page > maxPage) page = maxPage; SetUpPage(page); } // gain _seller.money.Pay(uitem._item.value); _buyer.money.Add(uitem._item.value); UIbuyerMoney.Set(_buyer.G, _buyer.K, _buyer.T); return true; }
void LateUpdate() { // handle mouse over animation // remove visual changes for buttons (except items) if (selectedModeButton != null) selectedModeButton.butImage.color = selectedModeButton.selectedColor; if (mouseOverModeButton != null && mouseOverModeButton != selectedModeButton) mouseOverModeButton.butImage.color = mouseOverModeButton.origColor; mouseOverModeButton = null; // remove visual changes for items if (selectedUIItem != null) selectedUIItem.Set(mouseSelectColor); if (mouseoverUIItem != null && mouseoverUIItem != selectedUIItem) mouseoverUIItem.ResetColor(); mouseoverUIItem = null; if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)gameObject.transform, Input.mousePosition)) // ShopUI rect { if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)panelItems.transform, Input.mousePosition)) // PanelItems rect { for (int i = 0; i < UIItems.Length; ++i) { if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)UIItems[i]._itemElem.transform, Input.mousePosition)) // items { // mouse is within item rect if (Input.GetMouseButton(0)) { if (UIItems[i]._hasItem && UIItems[i]._item.amount > 0) { /* item was clicked on with left mouse button => selected */ if (selectedUIItem != UIItems[i] && selectedUIItem != null) selectedUIItem.ResetColor(); // a different item was selected previously selectedUIItem = UIItems[i]; selectedUIItem.Set(mouseSelectColor); } } else { /* mouse is only hovering over the item */ // restore old visual for the previously highlighted item if (mouseoverUIItem != null && mouseoverUIItem != UIItems[i] && mouseoverUIItem != selectedUIItem) // mouse hovers over a new item mouseoverUIItem.ResetColor(); else if (selectedUIItem != null && mouseoverUIItem == selectedUIItem) // the previous item could still be selected selectedUIItem.Set(mouseSelectColor); // set colors for new hover item mouseoverUIItem = UIItems[i]; UIItems[i].Set((selectedUIItem == mouseoverUIItem) ? mouseSelectOverColor : mouseOverColor); break; } } } } else if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)panelTop.transform, Input.mousePosition)) // top bar rect { UIButton oldSelectButton = selectedModeButton; if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)buttonClose.button.transform, Input.mousePosition)) { if (Input.GetMouseButton(0)) currendManager.EndInteraction(); // end of shopping else if (selectedModeButton == buttonClose) selectedModeButton.butImage.color = selectedModeButton.selectedOverColor; else mouseOverModeButton = buttonClose; } else if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)buttonSelectBuy.button.transform, Input.mousePosition)) { if (Input.GetMouseButton(0)) ChangeModeTo(EShopMode.buy); else if (selectedModeButton == buttonSelectBuy) selectedModeButton.butImage.color = selectedModeButton.selectedOverColor; else mouseOverModeButton = buttonSelectBuy; } else if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)buttonSell.button.transform, Input.mousePosition)) { if (Input.GetMouseButton(0)) ChangeModeTo(EShopMode.sell); else if (selectedModeButton == buttonSell) selectedModeButton.butImage.color = selectedModeButton.selectedOverColor; else mouseOverModeButton = buttonSell; } else if (RectTransformUtility.RectangleContainsScreenPoint((RectTransform)buttonRebuy.button.transform, Input.mousePosition)) { if (Input.GetMouseButton(0)) ChangeModeTo(EShopMode.rebuy); else if (selectedModeButton == buttonRebuy) selectedModeButton.butImage.color = selectedModeButton.selectedOverColor; else mouseOverModeButton = buttonRebuy; } if (mouseOverModeButton != null) mouseOverModeButton.butImage.color = mouseOverModeButton.mouseOvercolor; // apply visual changes if (selectedModeButton != oldSelectButton ) { oldSelectButton.butImage.color = selectedModeButton.origColor; selectedModeButton.butImage.color = selectedModeButton.selectedOverColor; } } } }