void Update() { Player player = Player.localPlayer; if (player) { panel.SetActive(true); // instantiate/destroy enough slots UIUtils.BalancePrefabs(slotPrefab.gameObject, player.skillbar.Length, content); // refresh all for (int i = 0; i < player.skillbar.Length; ++i) { UISkillbarSlot slot = content.GetChild(i).GetComponent <UISkillbarSlot>(); slot.dragAndDropable.name = i.ToString(); // drag and drop index // hotkey overlay (without 'Alpha' etc.) string pretty = player.skillbar[i].hotKey.ToString().Replace("Alpha", ""); slot.hotkeyText.text = pretty; // skill, inventory item or equipment item? int skillIndex = player.GetSkillIndexByName(player.skillbar[i].reference); int inventoryIndex = player.GetInventoryIndexByName(player.skillbar[i].reference); int equipmentIndex = player.GetEquipmentIndexByName(player.skillbar[i].reference); if (skillIndex != -1) { Skill skill = player.skills[skillIndex]; bool canCast = player.CastCheckSelf(skill); // hotkey pressed and not typing in any input right now? if (Input.GetKeyDown(player.skillbar[i].hotKey) && !UIUtils.AnyInputActive() && canCast) // checks mana, cooldowns, etc.) { // try use the skill or walk closer if needed player.TryUseSkill(skillIndex); } // refresh skill slot slot.button.interactable = canCast; // check mana, cooldowns, etc. slot.button.onClick.SetListener(() => { // try use the skill or walk closer if needed player.TryUseSkill(skillIndex); }); slot.tooltip.enabled = true; slot.tooltip.text = skill.ToolTip(); slot.dragAndDropable.dragable = true; slot.image.color = Color.white; slot.image.sprite = skill.image; float cooldown = skill.CooldownRemaining(); slot.cooldownOverlay.SetActive(cooldown > 0); slot.cooldownText.text = cooldown.ToString("F0"); slot.cooldownCircle.fillAmount = skill.cooldown > 0 ? cooldown / skill.cooldown : 0; slot.amountOverlay.SetActive(false); } else if (inventoryIndex != -1) { ItemSlot itemSlot = player.inventory[inventoryIndex]; // hotkey pressed and not typing in any input right now? if (Input.GetKeyDown(player.skillbar[i].hotKey) && !UIUtils.AnyInputActive()) { player.CmdUseInventoryItem(inventoryIndex); } // refresh inventory slot slot.button.onClick.SetListener(() => { player.CmdUseInventoryItem(inventoryIndex); }); slot.tooltip.enabled = true; slot.tooltip.text = itemSlot.ToolTip(); slot.dragAndDropable.dragable = true; slot.image.color = Color.white; slot.image.sprite = itemSlot.item.Image; slot.cooldownOverlay.SetActive(false); slot.cooldownCircle.fillAmount = 0; slot.amountOverlay.SetActive(itemSlot.amount > 1); slot.amountText.text = itemSlot.amount.ToString(); } else if (equipmentIndex != -1) { ItemSlot itemSlot = player.equipment[equipmentIndex]; // refresh equipment slot slot.button.onClick.RemoveAllListeners(); slot.tooltip.enabled = true; slot.tooltip.text = itemSlot.ToolTip(); slot.dragAndDropable.dragable = true; slot.image.color = Color.white; slot.image.sprite = itemSlot.item.Image; slot.cooldownOverlay.SetActive(false); slot.cooldownCircle.fillAmount = 0; slot.amountOverlay.SetActive(itemSlot.amount > 1); slot.amountText.text = itemSlot.amount.ToString(); } else { // clear the outdated reference player.skillbar[i].reference = ""; // refresh empty slot slot.button.onClick.RemoveAllListeners(); slot.tooltip.enabled = false; slot.dragAndDropable.dragable = false; slot.image.color = Color.clear; slot.image.sprite = null; slot.cooldownOverlay.SetActive(false); slot.cooldownCircle.fillAmount = 0; slot.amountOverlay.SetActive(false); } } } else { panel.SetActive(false); } }
void Update() { Player player = Player.localPlayer; // use collider point(s) to also work with big entities if (player != null && player.Target != null && player.Target is Npc && Utility.Utility.ClosestDistance(player.collider, player.Target.collider) <= player.interactionRange) { Npc npc = (Npc)player.Target; // items for sale UIUtils.BalancePrefabs(slotPrefab.gameObject, npc.saleItems.Length, content); for (int i = 0; i < npc.saleItems.Length; ++i) { UINpcTradingSlot slot = content.GetChild(i).GetComponent <UINpcTradingSlot>(); ScriptableItem itemData = npc.saleItems[i]; // show item in UI int icopy = i; slot.button.onClick.SetListener(() => { buyIndex = icopy; }); slot.image.color = Color.white; slot.image.sprite = itemData.image; slot.tooltip.enabled = true; slot.tooltip.text = new ItemSlot(new Item(itemData)).ToolTip(); // with slot for {AMOUNT} } // buy if (buyIndex != -1 && buyIndex < npc.saleItems.Length) { ScriptableItem itemData = npc.saleItems[buyIndex]; // make valid amount, calculate price int amount = buyAmountInput.text.ToInt(); amount = Mathf.Clamp(amount, 1, itemData.maxStack); long price = amount * itemData.buyPrice; // show buy panel with item in UI buyAmountInput.text = amount.ToString(); buySlot.GetComponent <Image>().color = Color.white; buySlot.GetComponent <Image>().sprite = itemData.image; buySlot.GetComponent <UIShowToolTip>().enabled = true; buySlot.GetComponent <UIShowToolTip>().text = new ItemSlot(new Item(itemData)).ToolTip(); // with slot for {AMOUNT} buySlot.dragable = true; buyCostsText.text = price.ToString(); buyButton.interactable = amount > 0 && price <= player.Money && player.InventoryCanAdd(new Item(itemData), amount); buyButton.onClick.SetListener(() => { player.CmdNpcBuyItem(buyIndex, amount); buyIndex = -1; buyAmountInput.text = "1"; }); } else { // show default buy panel in UI buySlot.GetComponent <Image>().color = Color.clear; buySlot.GetComponent <Image>().sprite = null; buySlot.GetComponent <UIShowToolTip>().enabled = false; buySlot.dragable = false; buyCostsText.text = "0"; buyButton.interactable = false; } // sell if (sellIndex != -1 && sellIndex < player.inventory.Count && player.inventory[sellIndex].amount > 0) { ItemSlot itemSlot = player.inventory[sellIndex]; // make valid amount, calculate price int amount = sellAmountInput.text.ToInt(); amount = Mathf.Clamp(amount, 1, itemSlot.amount); long price = amount * itemSlot.item.SellPrice; // show sell panel with item in UI sellAmountInput.text = amount.ToString(); sellSlot.GetComponent <Image>().color = Color.white; sellSlot.GetComponent <Image>().sprite = itemSlot.item.Image; sellSlot.GetComponent <UIShowToolTip>().enabled = true; sellSlot.GetComponent <UIShowToolTip>().text = itemSlot.ToolTip(); sellSlot.dragable = true; sellCostsText.text = price.ToString(); sellButton.interactable = amount > 0; sellButton.onClick.SetListener(() => { player.CmdNpcSellItem(sellIndex, amount); sellIndex = -1; sellAmountInput.text = "1"; }); } else { // show default sell panel in UI sellSlot.GetComponent <Image>().color = Color.clear; sellSlot.GetComponent <Image>().sprite = null; sellSlot.GetComponent <UIShowToolTip>().enabled = false; sellSlot.dragable = false; sellCostsText.text = "0"; sellButton.interactable = false; } } else { panel.SetActive(false); // hide } }