Files
test/Assets/PerfectWorld/Scripts/UI/NPCShopDetailPanel.cs
T
2025-12-08 09:30:59 +07:00

202 lines
5.8 KiB
C#

// Filename : NPCShopDetailPanel.cs
// Creator : Converted from C++ EC_Shop
// Date : 2024
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using PerfectWorld.Scripts.Shop;
using BrewMonster.Scripts.Managers;
using BrewMonster.Network;
using CSNetwork.C2SCommand;
public class NPCShopDetailPanel : MonoBehaviour
{
[Header("UI Components")]
public TextMeshProUGUI itemNameText;
public TextMeshProUGUI itemDescriptionText;
public Image itemIconImage;
public TextMeshProUGUI itemPriceText;
[Header("Action Buttons")]
public Button closeButton;
public Button buyButton;
private GShopItem currentItem;
private NPCShopUIManager shopManager;
void Start()
{
SetupEventListeners();
}
void SetupEventListeners()
{
if (closeButton != null)
closeButton.onClick.AddListener(OnCloseClicked);
if (buyButton != null)
buyButton.onClick.AddListener(OnBuyButtonClicked);
}
public void SetupDetailPanel(GShopItem item, NPCShopUIManager manager)
{
currentItem = item;
shopManager = manager;
UpdateDisplay();
}
void UpdateDisplay()
{
if (currentItem.id == 0)
{
Debug.LogWarning("[NPCShopDetailPanel] Current item ID is 0, skipping display update");
return;
}
// Set item name
if (itemNameText != null)
itemNameText.text = currentItem.name;
// Set item description
if (itemDescriptionText != null)
{
string description = GetItemDescription((int)currentItem.id);
itemDescriptionText.text = description;
}
// Set price (use first available price)
uint price = 0;
if (currentItem.buy != null && currentItem.buy.Length > 0)
{
for (int i = 0; i < currentItem.buy.Length; i++)
{
if (currentItem.buy[i].price > 0)
{
price = currentItem.buy[i].price;
break;
}
}
}
if (itemPriceText != null)
itemPriceText.text = price > 0 ? $"Price: {price}" : "Price: N/A";
// Load icon
if (itemIconImage != null)
{
LoadItemIcon(itemIconImage, (int)currentItem.id);
}
}
void LoadItemIcon(Image iconImage, int itemId)
{
if (itemId <= 0 || iconImage == null)
return;
// Ensure the Image component is enabled and properly configured
iconImage.enabled = true;
iconImage.preserveAspect = true;
iconImage.type = Image.Type.Simple;
// Use the existing icon loading system from EC_IvtrItemUtils
Sprite iconSprite = EC_IvtrItemUtils.Instance.ResolveItemIconSprite(itemId);
if (iconSprite != null)
{
iconImage.sprite = iconSprite;
iconImage.color = Color.white;
}
else
{
iconImage.sprite = null;
}
}
string GetItemDescription(int itemId)
{
// First check if description is already in the item data
if (!string.IsNullOrEmpty(currentItem.desc))
{
return currentItem.desc;
}
// Otherwise, build description using the same central pipeline as inventory:
try
{
EC_IvtrItem item = EC_IvtrItem.CreateItem(itemId, 0, 1);
if (item != null)
{
// For NPC shop, we typically have static data only, so load from local DB
item.GetDetailDataFromLocal();
string description = item.GetDesc();
if (!string.IsNullOrEmpty(description))
{
return description.Replace("\\r", "\n");
}
}
}
catch (System.Exception ex)
{
Debug.LogWarning($"[NPCShopDetailPanel] Failed to get description for item {itemId}: {ex.Message}");
}
return "No description available.";
}
void OnCloseClicked()
{
gameObject.SetActive(false);
}
void OnBuyButtonClicked()
{
if (currentItem.id == 0)
{
Debug.LogWarning("[NPCShopDetailPanel] Cannot buy item with ID 0");
return;
}
// Get price from item
uint price = 0;
if (currentItem.buy != null && currentItem.buy.Length > 0)
{
for (int i = 0; i < currentItem.buy.Length; i++)
{
if (currentItem.buy[i].price > 0)
{
price = currentItem.buy[i].price;
break;
}
}
}
// Create npc_trade_item array for buying from NPC
// The tid is the item template ID, index is shop item index (0 for now), count is quantity to buy
npc_trade_item[] items = new npc_trade_item[1];
items[0] = new npc_trade_item
{
tid = (int)currentItem.id,
index = 0, // Shop item index - may need to be determined from shop item position
count = 1 // Quantity to buy
};
// Send the buy command
UnityGameSession.c2s_CmdNPCSevBuy(1, items);
Debug.Log($"[NPCShopDetailPanel] Sent buy command for item {currentItem.id}, price {price}");
}
void OnDestroy()
{
// Clean up event listeners
if (closeButton != null)
closeButton.onClick.RemoveListener(OnCloseClicked);
if (buyButton != null)
buyButton.onClick.RemoveListener(OnBuyButtonClicked);
}
}