void UpdatePanel() { if (gameManager == null) { gameManager = GameManager.instance; } int length = gameManager.saleFood.Count; UIUtils.BalancePrefabs(slotPrefab.gameObject, length, content); for (int i = 0; i < length; i++) { UIMarketSlot slot = content.GetChild(i).GetComponent <UIMarketSlot>(); FoodItemAndAmount foodItemAndAmount = gameManager.saleFood[i]; // add select id int icopy = i; slot.button.onClick.SetListener(() => OnFoodClicked(icopy)); // show item in UI slot.image.color = Color.white; slot.image.sprite = foodItemAndAmount.item.image; slot.nameText.text = ""; slot.amountText.text = foodItemAndAmount.amount.ToString(); } }
/// <param name="index"> index of saleFood </param> public void BuyFood(int index, int amount) { if (gameManager == null) { print("nn"); } FoodItem foodItem = gameManager.saleFood[index].item; if (1 <= amount && amount <= foodItem.maxStack) { long price = foodItem.price * amount; // enough gold and enough space in inventory? if (money >= price) { // pay for it, add to food money -= price; // do we already have this food? int i = TryGetFood(foodItem); if (i != -1) { // increase its amount FoodItemAndAmount t = food[i]; t.amount += amount; food[i] = t; } else { food.Add(new FoodItemAndAmount(foodItem, amount)); } } } }
public void UpdatePanel() { // instantiate/destroy enough slots UIUtils.BalancePrefabs(slotPrefab.gameObject, player.food.Count, content); // refresh all items for (int i = 0; i < player.food.Count; ++i) { UIInventorySlot slot = content.GetChild(i).GetComponent <UIInventorySlot>(); FoodItemAndAmount food = player.food[i]; if (food.amount > 0) { int icopy = i; // needed for lambdas, otherwise i is Count slot.button.onClick.SetListener(() => OnFoodSlotClicked(icopy)); // show images and amount text slot.image.color = Color.white; slot.image.sprite = food.item.image; slot.amountText.text = food.amount.ToString(); } else { // refresh invalid item slot.button.onClick.RemoveAllListeners(); slot.image.color = Color.clear; slot.image.sprite = null; slot.cooldownCircle.fillAmount = 0; slot.amountOverlay.SetActive(false); } } }
public override void Use(Player player, int inventoryIndex) { // always call base function too base.Use(player, inventoryIndex); // increase health/mana/etc. player.hp += hp; player.mp += mp; // decrease amount in inventory FoodItemAndAmount food = player.food[inventoryIndex]; food.amount--; player.food[inventoryIndex] = food; }