|
@@ -0,0 +1,230 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.UI;
|
|
|
+using TMPro;
|
|
|
+using Sirenix.OdinInspector;
|
|
|
+using KairoEngine.SFX;
|
|
|
+using KairoEngine.Core;
|
|
|
+
|
|
|
+namespace KairoEngine.Inventory
|
|
|
+{
|
|
|
+ public class ItemSlotUi : MonoBehaviour
|
|
|
+ {
|
|
|
+ public ItemRef itemRef;
|
|
|
+ public Image iconUi;
|
|
|
+ public TextMeshProUGUI quantityUi;
|
|
|
+ public TextMeshProUGUI priceUi;
|
|
|
+ public TextMeshProUGUI valueUI;
|
|
|
+ public RectTransform curtainTransform;
|
|
|
+ public bool draggable = true;
|
|
|
+ public bool dropable = true;
|
|
|
+ public bool forSaleItem = false;
|
|
|
+
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonNormal;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonInteraction;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonNavigation;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonSelected;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonDisabled;
|
|
|
+
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonSuccess;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonSuccessInteraction;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonSuccessNavigation;
|
|
|
+ [ShowIf("@forSaleItem")] public Sprite buttonSuccessSelected;
|
|
|
+
|
|
|
+ [ReadOnly] public ItemContainer parentItemContainer;
|
|
|
+ [ReadOnly] public ItemContainerUi parentItemContainerUi;
|
|
|
+
|
|
|
+ [ReadOnly] public int quantityToBuy = 0;
|
|
|
+
|
|
|
+ private BarterControllerUi baterCtrl;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void Setup(ItemRef newItemRef, ItemContainer parentItemContainer, ItemContainerUi itemContainerUi, bool showQuantity = true, bool showPrice = false, string value = "")
|
|
|
+ {
|
|
|
+ this.parentItemContainerUi = itemContainerUi;
|
|
|
+ this.parentItemContainer = parentItemContainer;
|
|
|
+ itemRef = newItemRef;
|
|
|
+ if(forSaleItem) ResetButtonUi();
|
|
|
+ if (itemRef != null)
|
|
|
+ {
|
|
|
+ iconUi.sprite = itemRef.item.icon;
|
|
|
+ iconUi.enabled = true;
|
|
|
+ CooldownCurtain();
|
|
|
+ if (itemRef.item.stackble && showQuantity)
|
|
|
+ {
|
|
|
+ quantityUi.gameObject.SetActive(true);
|
|
|
+ quantityUi.enabled = true;
|
|
|
+ quantityUi.text = itemRef.quantity.ToString();
|
|
|
+ }
|
|
|
+ if(showPrice)
|
|
|
+ {
|
|
|
+ priceUi.gameObject.SetActive(true);
|
|
|
+ priceUi.enabled = true;
|
|
|
+ priceUi.text = "$" + itemRef.item.value.ToString();
|
|
|
+ }
|
|
|
+ if(forSaleItem)
|
|
|
+ {
|
|
|
+ quantityToBuy = 0;
|
|
|
+ SetupBuy();
|
|
|
+ }
|
|
|
+ else gameObject.GetComponent<Button>().interactable = true;
|
|
|
+ } else
|
|
|
+ {
|
|
|
+ iconUi.sprite = null;
|
|
|
+ iconUi.enabled = false;
|
|
|
+ quantityUi.gameObject.SetActive(false);
|
|
|
+ quantityUi.enabled = false;
|
|
|
+ priceUi.gameObject.SetActive(false);
|
|
|
+ priceUi.enabled = false;
|
|
|
+ curtainTransform.sizeDelta = new Vector2 (75, 0);
|
|
|
+ }
|
|
|
+ if(value != "")
|
|
|
+ {
|
|
|
+ valueUI.gameObject.SetActive(true);
|
|
|
+ valueUI.enabled = true;
|
|
|
+ valueUI.text = value;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ valueUI.gameObject.SetActive(false);
|
|
|
+ valueUI.enabled = false;
|
|
|
+ }
|
|
|
+ baterCtrl = BarterControllerUi.instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ void Update()
|
|
|
+ {
|
|
|
+ CooldownCurtain();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CooldownCurtain()
|
|
|
+ {
|
|
|
+ if(itemRef != null)
|
|
|
+ {
|
|
|
+ if(itemRef.item == null) return;
|
|
|
+ if(itemRef.item.category == ItemType.instantEffect)
|
|
|
+ {
|
|
|
+ ItemBaseInstantEffect item = (ItemBaseInstantEffect)itemRef.item;
|
|
|
+ if(item.hasCooldown)
|
|
|
+ {
|
|
|
+ if(itemRef.lastUsed < item.cooldownTime)
|
|
|
+ {
|
|
|
+ float height = (75/item.cooldownTime) * (item.cooldownTime - itemRef.lastUsed);
|
|
|
+ curtainTransform.sizeDelta = new Vector2 (75, height);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ curtainTransform.sizeDelta = new Vector2 (75, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SetupBuy()
|
|
|
+ {
|
|
|
+ ItemContainer playerInventory = EventManager.request.GetItemContainer("PlayerItemContainer").value;
|
|
|
+ if(playerInventory == null) Debug.LogError("Missing playerInventory");
|
|
|
+ bool hasMoney = playerInventory.HasItem("Old Dollars", itemRef.item.value);
|
|
|
+ Button button = gameObject.GetComponent<Button>();
|
|
|
+ if(hasMoney == false)
|
|
|
+ {
|
|
|
+ if(button != null) button.interactable = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(button != null) button.interactable = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void AddToCart()
|
|
|
+ {
|
|
|
+ if(itemRef == null) return;
|
|
|
+ bool x10 = false;
|
|
|
+ if(Input.GetKey(KeyCode.LeftControl)) x10 = true;
|
|
|
+ ItemContainer playerInventory = EventManager.request.GetItemContainer("PlayerItemContainer").value;
|
|
|
+ if(playerInventory == null) Debug.LogError("Missing playerInventory");
|
|
|
+ int total = 0;
|
|
|
+ foreach (var itemSlot in parentItemContainerUi.itemSlots)
|
|
|
+ {
|
|
|
+ if(itemSlot.itemRef != null)
|
|
|
+ {
|
|
|
+ total += itemSlot.itemRef.item.value * itemSlot.quantityToBuy;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var x = x10 && itemRef.quantity >= quantityToBuy + 10 ? 10 : itemRef.quantity - quantityToBuy;
|
|
|
+ total += x10 ? itemRef.item.value * x : itemRef.item.value;
|
|
|
+ bool hasMoney = playerInventory.HasItem("Old Dollars", total);
|
|
|
+ if(hasMoney == false)
|
|
|
+ {
|
|
|
+ SoundController.EmmitSound(baterCtrl.sfxNegative, this.transform.position);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(itemRef.quantity > quantityToBuy)
|
|
|
+ {
|
|
|
+ SoundController.EmmitSound(baterCtrl.sfxAddItem, this.transform.position);
|
|
|
+ quantityToBuy += x10 ? x : 1;
|
|
|
+
|
|
|
+ }
|
|
|
+ if(quantityToBuy > 1)
|
|
|
+ {
|
|
|
+ valueUI.gameObject.SetActive(true);
|
|
|
+ valueUI.enabled = true;
|
|
|
+ valueUI.text = quantityToBuy.ToString() + "x";
|
|
|
+ }
|
|
|
+ quantityUi.text = (itemRef.quantity - quantityToBuy).ToString();
|
|
|
+ Image image = gameObject.GetComponent<Image>();
|
|
|
+ if(image != null)
|
|
|
+ {
|
|
|
+ image.sprite = buttonSuccess;
|
|
|
+ }
|
|
|
+ Button button = gameObject.GetComponent<Button>();
|
|
|
+ if(button != null)
|
|
|
+ {
|
|
|
+ SpriteState state = new SpriteState();
|
|
|
+ state.highlightedSprite = buttonSuccessSelected;
|
|
|
+ state.pressedSprite = buttonSuccessInteraction;
|
|
|
+ state.selectedSprite = buttonSuccessNavigation;
|
|
|
+ button.spriteState = state;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void RemoveFromCart()
|
|
|
+ {
|
|
|
+ if(itemRef == null) return;
|
|
|
+ bool x10 = false;
|
|
|
+ if(Input.GetKey(KeyCode.LeftControl)) x10 = true;
|
|
|
+ SoundController.EmmitSound(baterCtrl.sfxRemoveItem, this.transform.position);
|
|
|
+ if(x10 && quantityToBuy > 10) quantityToBuy -= 10;
|
|
|
+ else if(x10 && quantityToBuy <= 10) quantityToBuy = 0;
|
|
|
+ else if(!x10 && quantityToBuy > 0) quantityToBuy -= 1;
|
|
|
+ if(quantityToBuy < 2)
|
|
|
+ {
|
|
|
+ valueUI.gameObject.SetActive(false);
|
|
|
+ valueUI.enabled = false;
|
|
|
+ }
|
|
|
+ valueUI.text = quantityToBuy.ToString() + "x";
|
|
|
+ quantityUi.text = (itemRef.quantity - quantityToBuy).ToString();
|
|
|
+ if(quantityToBuy == 0) ResetButtonUi();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ResetButtonUi()
|
|
|
+ {
|
|
|
+ Image image = gameObject.GetComponent<Image>();
|
|
|
+ if(image != null)
|
|
|
+ {
|
|
|
+ image.sprite = buttonNormal;
|
|
|
+ }
|
|
|
+ Button button = gameObject.GetComponent<Button>();
|
|
|
+ if(button != null)
|
|
|
+ {
|
|
|
+ SpriteState state = new SpriteState();
|
|
|
+ state.highlightedSprite = buttonSelected;
|
|
|
+ state.pressedSprite = buttonInteraction;
|
|
|
+ state.selectedSprite = buttonNavigation;
|
|
|
+ state.disabledSprite = buttonDisabled;
|
|
|
+ button.spriteState = state;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|