using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; namespace BulletHellTemplate { public class UIShopMenu : MonoBehaviour { public ShopItem[] shopItems; // List of available shop items public ShopBuyPopup buyPopup; // Popup for item purchase confirmation public ShopEntry shopEntryPrefab; // Prefab for shop entries public Transform containerShopItems; // Container to hold shop entries public TextMeshProUGUI errorMessage; public string notEnoughCurrency = "Not enough currency to buy this item."; public NameTranslatedByLanguage[] notEnoughCurrencyTranslated; public UnityEvent OnOpenShop; [Header("On open Menu filter category")] public string category = "character"; private string currentLang; public static UIShopMenu Singleton; // Singleton Singleton of the shop menu private void Awake() { // Ensure singleton Singleton if (Singleton == null) { Singleton = this; } } private void OnEnable() { currentLang = LanguageManager.LanguageManager.Instance.GetCurrentLanguage(); } private void Start() { InitialFilter(); } /// /// Opens the purchase confirmation popup with the selected item's details. /// /// The ID of the item. /// The name of the item. /// The description of the item. /// The icon of the item. public void OpenBuyPopup(string itemId, string itemName, string itemDescription, Sprite itemIcon) { buyPopup.SetPopupInfo(itemId, itemName, itemDescription, itemIcon); buyPopup.gameObject.SetActive(true); } /// /// Confirms the purchase of an item if the player has sufficient currency. /// /// The ID of the item to buy. public void BuyItemConfirm(string itemId) { foreach (ShopItem item in shopItems) { if (item.itemId == itemId) { string purchasedItemCategory = item.category; // Get the category of the purchased item item.Buy(); FilterItemsByCategory(purchasedItemCategory); // Reload shop items for the category of the purchased item return; } } } /// /// Clears all shop items from the container. /// public void ClearShopItems() { foreach (Transform child in containerShopItems) { Destroy(child.gameObject); } } /// /// Loads and displays shop items that have not been purchased. /// public void LoadShopItems() { ClearShopItems(); // Display items that are not purchased foreach (ShopItem item in shopItems) { if (!MonetizationManager.Singleton.IsShopItemPurchased(item.itemId)) { ShopEntry shopEntry = Instantiate(shopEntryPrefab, containerShopItems); string itemTitleTranslated = GetTranslatedString(item.itemTitleTranslated, item.itemTitle, currentLang); shopEntry.shopItemName.text = itemTitleTranslated; shopEntry.price.text = item.price.ToString(); string itemDescriptionTranslated = GetTranslatedString(item.itemDescriptionTranslated, item.itemDescription, currentLang); shopEntry.description.text = itemDescriptionTranslated; shopEntry.shopItemIcon.sprite = item.itemIcon; shopEntry.currency.sprite = MonetizationManager.Singleton.GetCurrencyIcon(item.currency); shopEntry.SetShopItemID(item.itemId); } } } /// /// Filters shop items by a specific category. /// /// The category to filter shop items by. public void FilterItemsByCategory(string filterCategory) { ClearShopItems(); foreach (ShopItem item in shopItems) { if (item.category.Equals(filterCategory, System.StringComparison.OrdinalIgnoreCase) && !MonetizationManager.Singleton.IsShopItemPurchased(item.itemId)) { ShopEntry shopEntry = Instantiate(shopEntryPrefab, containerShopItems); shopEntry.shopItemName.text = item.itemTitle; shopEntry.price.text = item.price.ToString(); shopEntry.description.text = item.itemDescription; shopEntry.shopItemIcon.sprite = item.itemIcon; shopEntry.currency.sprite = MonetizationManager.Singleton.GetCurrencyIcon(item.currency); shopEntry.SetShopItemID(item.itemId); } } } /// /// Filters shop items based on the category specified in the script's category field. /// public void InitialFilter() { FilterItemsByCategory(category); OnOpenShop.Invoke(); } public void ShowErrorMessage() { string translatedError = GetTranslatedString(notEnoughCurrencyTranslated,notEnoughCurrency,currentLang); errorMessage.text = translatedError; StartCoroutine(ClearErrorMessageAfterDelay(2.0f)); } /// /// Coroutine to clear the error message after a delay. /// /// The delay in seconds before clearing the message. /// An IEnumerator for coroutine execution. public IEnumerator ClearErrorMessageAfterDelay(float delay) { yield return new WaitForSeconds(delay); errorMessage.text = ""; } private string GetTranslatedString(NameTranslatedByLanguage[] translations, string fallback, string currentLang) { if (translations != null) { foreach (var trans in translations) { if (!string.IsNullOrEmpty(trans.LanguageId) && trans.LanguageId.Equals(currentLang) && !string.IsNullOrEmpty(trans.Translate)) { return trans.Translate; } } } return fallback; } private string GetTranslatedString(DescriptionTranslatedByLanguage[] translations, string fallback, string currentLang) { if (translations != null) { foreach (var trans in translations) { if (!string.IsNullOrEmpty(trans.LanguageId) && trans.LanguageId.Equals(currentLang) && !string.IsNullOrEmpty(trans.Translate)) { return trans.Translate; } } } return fallback; } } }