TownShopUI.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. public class TownShopUI : MonoBehaviour
  5. {
  6. [Header("References")]
  7. public UIDocument uiDocument;
  8. [Header("Shop Settings")]
  9. public TownShop currentShop;
  10. public TeamCharacter selectedCustomer;
  11. // UI Elements
  12. private VisualElement root;
  13. private VisualElement shopContainer;
  14. private Label shopNameLabel;
  15. private Label shopkeeperLabel;
  16. private VisualElement itemList;
  17. private VisualElement customerPanel;
  18. private DropdownField customerDropdown;
  19. private Label moneyLabel;
  20. private VisualElement tabContainer;
  21. private Button buyTab;
  22. private Button sellTab;
  23. private DropdownField categoryFilter;
  24. private TextField searchField;
  25. private Button closeButton;
  26. // State
  27. private bool isSellMode = false;
  28. private void Start()
  29. {
  30. InitializeUI();
  31. SetupEventHandlers();
  32. // Try to load team from GameStateManager
  33. if (GameStateManager.Instance?.savedTeam != null)
  34. {
  35. foreach (var character in GameStateManager.Instance.savedTeam)
  36. {
  37. if (character != null)
  38. {
  39. selectedCustomer = character;
  40. UpdateCustomerDisplay();
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. public void SetShop(TownShop shop)
  47. {
  48. Debug.Log($"=== TownShopUI.SetShop DEBUG ===");
  49. Debug.Log($"Received shop: {shop?.buildingName ?? "NULL"} ({shop?.shopType})");
  50. Debug.Log($"Shop keeper: {shop?.shopkeeperName ?? "NULL"}");
  51. Debug.Log($"Current shop before: {currentShop?.buildingName ?? "NULL"}");
  52. currentShop = shop;
  53. Debug.Log($"Current shop after: {currentShop?.buildingName ?? "NULL"}");
  54. if (root != null)
  55. {
  56. UpdateShopDisplay();
  57. RefreshItemList();
  58. }
  59. }
  60. public void SetCustomer(TeamCharacter customer)
  61. {
  62. selectedCustomer = customer;
  63. UpdateCustomerDisplay();
  64. RefreshItemList();
  65. }
  66. private void InitializeUI()
  67. {
  68. if (uiDocument == null)
  69. {
  70. uiDocument = GetComponent<UIDocument>();
  71. }
  72. if (uiDocument?.rootVisualElement == null)
  73. {
  74. Debug.LogError("UI Document or root visual element is null!");
  75. return;
  76. }
  77. // Force refresh the UI Document to ensure proper initialization
  78. uiDocument.enabled = false;
  79. uiDocument.enabled = true;
  80. root = uiDocument.rootVisualElement;
  81. Debug.Log($"Root element found: {root != null}");
  82. // Ensure root element is visible and properly configured
  83. if (root != null)
  84. {
  85. root.style.position = Position.Absolute;
  86. root.style.left = 0;
  87. root.style.top = 0;
  88. root.style.right = 0;
  89. root.style.bottom = 0;
  90. root.style.visibility = Visibility.Visible;
  91. root.style.display = DisplayStyle.Flex;
  92. Debug.Log("Root element visibility and position configured");
  93. }
  94. // Get UI elements
  95. shopContainer = root.Q<VisualElement>("shop-container");
  96. shopNameLabel = root.Q<Label>("shop-name");
  97. shopkeeperLabel = root.Q<Label>("shopkeeper-name");
  98. itemList = root.Q<VisualElement>("item-list");
  99. customerPanel = root.Q<VisualElement>("customer-panel");
  100. customerDropdown = root.Q<DropdownField>("customer-dropdown");
  101. moneyLabel = root.Q<Label>("money-label");
  102. tabContainer = root.Q<VisualElement>("tab-container");
  103. buyTab = root.Q<Button>("buy-tab");
  104. sellTab = root.Q<Button>("sell-tab");
  105. categoryFilter = root.Q<DropdownField>("category-filter");
  106. searchField = root.Q<TextField>("search-field");
  107. closeButton = root.Q<Button>("close-button");
  108. Debug.Log($"Key UI elements found - shopContainer: {shopContainer != null}, shopNameLabel: {shopNameLabel != null}, buyTab: {buyTab != null}");
  109. // Set initial visibility
  110. if (shopContainer != null)
  111. {
  112. shopContainer.style.display = DisplayStyle.None;
  113. Debug.Log("Set shopContainer to hidden initially");
  114. }
  115. else
  116. {
  117. Debug.LogError("shopContainer not found! Check UXML element names.");
  118. }
  119. // Setup category filter options
  120. if (categoryFilter != null)
  121. {
  122. categoryFilter.choices = new List<string> { "All", "Weapons", "Armor", "Potions", "Misc" };
  123. categoryFilter.value = "All";
  124. }
  125. Debug.Log("TownShopUI initialized");
  126. }
  127. private void SetupEventHandlers()
  128. {
  129. if (buyTab != null)
  130. {
  131. buyTab.clicked += SwitchToBuyMode;
  132. }
  133. if (sellTab != null)
  134. {
  135. sellTab.clicked += SwitchToSellMode;
  136. }
  137. if (closeButton != null)
  138. {
  139. closeButton.clicked += CloseShop;
  140. }
  141. if (customerDropdown != null)
  142. {
  143. customerDropdown.RegisterValueChangedCallback(OnCustomerChanged);
  144. }
  145. if (categoryFilter != null)
  146. {
  147. categoryFilter.RegisterValueChangedCallback(evt => RefreshItemList());
  148. }
  149. if (searchField != null)
  150. {
  151. searchField.RegisterValueChangedCallback(evt => RefreshItemList());
  152. }
  153. }
  154. public void OpenShop()
  155. {
  156. Debug.Log($"=== OpenShop called ===");
  157. Debug.Log($"shopContainer: {shopContainer != null}");
  158. Debug.Log($"currentShop: {currentShop?.buildingName ?? "NULL"}");
  159. if (shopContainer != null)
  160. {
  161. shopContainer.style.display = DisplayStyle.Flex;
  162. Debug.Log("Set shopContainer to visible");
  163. }
  164. else
  165. {
  166. Debug.LogError("Cannot open shop - shopContainer is null! Creating fallback UI...");
  167. CreateFallbackUI();
  168. return;
  169. }
  170. PopulateCustomerDropdown();
  171. UpdateShopDisplay();
  172. UpdateCustomerDisplay();
  173. SwitchToBuyMode();
  174. RefreshItemList();
  175. Debug.Log("Shop opening sequence completed");
  176. }
  177. private void CreateFallbackUI()
  178. {
  179. if (root == null) return;
  180. // Create a simple fallback UI directly in code
  181. var fallbackContainer = new VisualElement();
  182. fallbackContainer.style.position = Position.Absolute;
  183. fallbackContainer.style.width = Length.Percent(100);
  184. fallbackContainer.style.height = Length.Percent(100);
  185. fallbackContainer.style.backgroundColor = new Color(0, 0, 0, 0.8f);
  186. fallbackContainer.style.justifyContent = Justify.Center;
  187. fallbackContainer.style.alignItems = Align.Center;
  188. var shopWindow = new VisualElement();
  189. shopWindow.style.width = 400;
  190. shopWindow.style.height = 300;
  191. shopWindow.style.backgroundColor = new Color(0.9f, 0.85f, 0.7f);
  192. shopWindow.style.borderLeftWidth = 3;
  193. shopWindow.style.borderRightWidth = 3;
  194. shopWindow.style.borderTopWidth = 3;
  195. shopWindow.style.borderBottomWidth = 3;
  196. shopWindow.style.borderLeftColor = new Color(0.55f, 0.27f, 0.07f);
  197. shopWindow.style.borderRightColor = new Color(0.55f, 0.27f, 0.07f);
  198. shopWindow.style.borderTopColor = new Color(0.55f, 0.27f, 0.07f);
  199. shopWindow.style.borderBottomColor = new Color(0.55f, 0.27f, 0.07f);
  200. shopWindow.style.borderTopLeftRadius = 10;
  201. shopWindow.style.borderTopRightRadius = 10;
  202. shopWindow.style.borderBottomLeftRadius = 10;
  203. shopWindow.style.borderBottomRightRadius = 10;
  204. shopWindow.style.paddingLeft = 20;
  205. shopWindow.style.paddingRight = 20;
  206. shopWindow.style.paddingTop = 20;
  207. shopWindow.style.paddingBottom = 20;
  208. var title = new Label($"{currentShop?.buildingName ?? "Shop"} - FALLBACK UI");
  209. title.style.fontSize = 20;
  210. title.style.color = new Color(0.55f, 0.27f, 0.07f);
  211. title.style.unityFontStyleAndWeight = FontStyle.Bold;
  212. title.style.marginBottom = 20;
  213. var message = new Label("The full shop UI failed to load.\nThis is a simplified fallback interface.\nCheck console for details.");
  214. message.style.fontSize = 14;
  215. message.style.color = new Color(0.3f, 0.3f, 0.3f);
  216. message.style.whiteSpace = WhiteSpace.Normal;
  217. message.style.marginBottom = 20;
  218. var closeButton = new Button(() =>
  219. {
  220. root.Remove(fallbackContainer);
  221. Destroy(gameObject);
  222. });
  223. closeButton.text = "Close";
  224. closeButton.style.backgroundColor = new Color(0.8f, 0.2f, 0.2f);
  225. closeButton.style.color = Color.white;
  226. closeButton.style.borderLeftWidth = 0;
  227. closeButton.style.borderRightWidth = 0;
  228. closeButton.style.borderTopWidth = 0;
  229. closeButton.style.borderBottomWidth = 0;
  230. closeButton.style.borderTopLeftRadius = 5;
  231. closeButton.style.borderTopRightRadius = 5;
  232. closeButton.style.borderBottomLeftRadius = 5;
  233. closeButton.style.borderBottomRightRadius = 5;
  234. closeButton.style.width = 80;
  235. closeButton.style.height = 30;
  236. shopWindow.Add(title);
  237. shopWindow.Add(message);
  238. shopWindow.Add(closeButton);
  239. fallbackContainer.Add(shopWindow);
  240. root.Add(fallbackContainer);
  241. Debug.Log("Fallback UI created and displayed");
  242. }
  243. private void CloseShop()
  244. {
  245. if (shopContainer != null)
  246. {
  247. shopContainer.style.display = DisplayStyle.None;
  248. }
  249. // Destroy the entire shop UI GameObject
  250. Destroy(gameObject);
  251. Debug.Log("Shop UI closed and destroyed");
  252. }
  253. private void PopulateCustomerDropdown()
  254. {
  255. if (customerDropdown == null || GameStateManager.Instance?.savedTeam == null) return;
  256. var choices = new List<string>();
  257. foreach (var member in GameStateManager.Instance.savedTeam)
  258. {
  259. if (member != null)
  260. {
  261. choices.Add(member.name); // Using correct property name
  262. }
  263. }
  264. customerDropdown.choices = choices;
  265. if (choices.Count > 0)
  266. {
  267. customerDropdown.value = choices[0];
  268. if (selectedCustomer == null)
  269. {
  270. foreach (var member in GameStateManager.Instance.savedTeam)
  271. {
  272. if (member != null)
  273. {
  274. selectedCustomer = member;
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. private void OnCustomerChanged(ChangeEvent<string> evt)
  282. {
  283. if (GameStateManager.Instance?.savedTeam == null) return;
  284. foreach (var member in GameStateManager.Instance.savedTeam)
  285. {
  286. if (member != null && member.name == evt.newValue) // Using correct property name
  287. {
  288. selectedCustomer = member;
  289. UpdateCustomerDisplay();
  290. RefreshItemList();
  291. break;
  292. }
  293. }
  294. }
  295. private void UpdateShopDisplay()
  296. {
  297. Debug.Log($"=== UpdateShopDisplay DEBUG ===");
  298. Debug.Log($"Current shop: {currentShop?.buildingName ?? "NULL"} ({currentShop?.shopType})");
  299. Debug.Log($"shopNameLabel: {shopNameLabel != null}");
  300. Debug.Log($"shopkeeperLabel: {shopkeeperLabel != null}");
  301. if (currentShop == null) return;
  302. if (shopNameLabel != null)
  303. {
  304. shopNameLabel.text = currentShop.buildingName;
  305. Debug.Log($"Set shop name label to: {currentShop.buildingName}");
  306. }
  307. if (shopkeeperLabel != null)
  308. {
  309. shopkeeperLabel.text = $"Shopkeeper: {currentShop.shopkeeperName}";
  310. Debug.Log($"Set shopkeeper label to: {currentShop.shopkeeperName}");
  311. }
  312. }
  313. private void UpdateCustomerDisplay()
  314. {
  315. if (selectedCustomer == null) return;
  316. UpdateMoneyDisplay();
  317. }
  318. private void UpdateMoneyDisplay()
  319. {
  320. if (moneyLabel == null || selectedCustomer == null) return;
  321. moneyLabel.text = $"Money: {selectedCustomer.gold}g {selectedCustomer.silver}s {selectedCustomer.copper}c";
  322. }
  323. private void RefreshItemList()
  324. {
  325. if (itemList == null || currentShop == null) return;
  326. itemList.Clear();
  327. if (isSellMode)
  328. {
  329. // Show player inventory for selling
  330. PopulateSellItems();
  331. }
  332. else
  333. {
  334. // Show shop inventory for buying
  335. PopulateBuyItems();
  336. }
  337. }
  338. private void PopulateBuyItems()
  339. {
  340. // Get shop inventory using the correct method
  341. var shopInventory = currentShop.GetInventory();
  342. foreach (var shopItem in shopInventory)
  343. {
  344. if (shopItem == null || shopItem.item == null) continue;
  345. // Apply filters
  346. string searchText = searchField?.value?.ToLower() ?? "";
  347. string selectedCategory = categoryFilter?.value ?? "All";
  348. if (!string.IsNullOrEmpty(searchText) && !shopItem.item.itemName.ToLower().Contains(searchText))
  349. continue;
  350. if (selectedCategory != "All" && !MatchesCategory(shopItem.item, selectedCategory))
  351. continue;
  352. CreateShopItemElement(shopItem);
  353. }
  354. }
  355. private void PopulateSellItems()
  356. {
  357. if (selectedCustomer == null) return;
  358. // Get player's inventory items using correct property names
  359. var playerItems = GetPlayerInventoryItems();
  360. foreach (var item in playerItems)
  361. {
  362. if (item == null) continue;
  363. // Apply filters
  364. string searchText = searchField?.value?.ToLower() ?? "";
  365. string selectedCategory = categoryFilter?.value ?? "All";
  366. if (!string.IsNullOrEmpty(searchText) && !item.itemName.ToLower().Contains(searchText))
  367. continue;
  368. if (selectedCategory != "All" && !MatchesCategory(item, selectedCategory))
  369. continue;
  370. CreateSellItemElement(item);
  371. }
  372. }
  373. private List<Item> GetPlayerInventoryItems()
  374. {
  375. var items = new List<Item>();
  376. if (selectedCustomer == null) return items;
  377. // Note: TeamCharacter stores item names as strings, but we need actual Item objects
  378. // This is a limitation of the current system - we would need an ItemDatabase
  379. // to convert item names back to Item objects for proper selling functionality
  380. // For now, return empty list with a debug message
  381. if (selectedCustomer.weapons != null && selectedCustomer.weapons.Count > 0)
  382. {
  383. Debug.LogWarning("Player has weapons but cannot display them - need ItemDatabase to convert string names to Item objects");
  384. }
  385. if (selectedCustomer.armor != null && selectedCustomer.armor.Count > 0)
  386. {
  387. Debug.LogWarning("Player has armor but cannot display them - need ItemDatabase to convert string names to Item objects");
  388. }
  389. if (selectedCustomer.miscItems != null && selectedCustomer.miscItems.Count > 0)
  390. {
  391. Debug.LogWarning("Player has misc items but cannot display them - need ItemDatabase to convert string names to Item objects");
  392. }
  393. return items;
  394. }
  395. private bool MatchesCategory(Item item, string category)
  396. {
  397. switch (category)
  398. {
  399. case "Weapons": return item is WeaponItem;
  400. case "Armor": return item is ArmorItem;
  401. case "Potions": return item is MiscellaneousItem && item.itemType == ItemType.Consumable;
  402. case "Misc": return item is MiscellaneousItem;
  403. default: return true;
  404. }
  405. }
  406. private void CreateShopItemElement(ShopInventoryItem shopItem)
  407. {
  408. var itemElement = new VisualElement();
  409. itemElement.AddToClassList("shop-item");
  410. // Use flexible row layout
  411. itemElement.style.flexDirection = FlexDirection.Row;
  412. itemElement.style.alignItems = Align.Center;
  413. itemElement.style.justifyContent = Justify.SpaceBetween;
  414. itemElement.style.paddingTop = 5;
  415. itemElement.style.paddingBottom = 5;
  416. itemElement.style.paddingLeft = 10;
  417. itemElement.style.paddingRight = 10;
  418. itemElement.style.marginBottom = 3;
  419. itemElement.style.backgroundColor = new Color(0.95f, 0.95f, 0.95f, 1f);
  420. itemElement.style.borderBottomWidth = 1;
  421. itemElement.style.borderBottomColor = new Color(0.8f, 0.8f, 0.8f, 1f);
  422. // Left side - Item info container
  423. var infoContainer = new VisualElement();
  424. infoContainer.style.flexDirection = FlexDirection.Column;
  425. infoContainer.style.flexGrow = 1;
  426. infoContainer.style.marginRight = 10;
  427. var nameLabel = new Label(shopItem.item.itemName);
  428. nameLabel.AddToClassList("item-name");
  429. nameLabel.style.fontSize = 14;
  430. nameLabel.style.color = new Color(0.2f, 0.2f, 0.2f, 1f);
  431. nameLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
  432. var descLabel = new Label(shopItem.item.description);
  433. descLabel.AddToClassList("item-description");
  434. descLabel.style.fontSize = 12;
  435. descLabel.style.color = new Color(0.4f, 0.4f, 0.4f, 1f);
  436. descLabel.style.whiteSpace = WhiteSpace.Normal;
  437. infoContainer.Add(nameLabel);
  438. infoContainer.Add(descLabel);
  439. // Right side - Price and button container
  440. var actionContainer = new VisualElement();
  441. actionContainer.style.flexDirection = FlexDirection.Row;
  442. actionContainer.style.alignItems = Align.Center;
  443. actionContainer.style.minWidth = 150;
  444. var priceLabel = new Label(shopItem.GetPriceString());
  445. priceLabel.AddToClassList("item-price");
  446. priceLabel.style.fontSize = 12;
  447. priceLabel.style.color = new Color(0.0f, 0.5f, 0.0f, 1f);
  448. priceLabel.style.marginRight = 10;
  449. priceLabel.style.minWidth = 60;
  450. var buyButton = new Button(() => BuyItem(shopItem))
  451. {
  452. text = "Buy"
  453. };
  454. buyButton.AddToClassList("buy-button");
  455. buyButton.style.width = 60;
  456. buyButton.style.height = 25;
  457. buyButton.style.fontSize = 12;
  458. // Check if customer can afford
  459. if (selectedCustomer != null && !shopItem.CanAfford(selectedCustomer))
  460. {
  461. buyButton.SetEnabled(false);
  462. priceLabel.style.color = new Color(0.8f, 0.2f, 0.2f, 1f);
  463. }
  464. actionContainer.Add(priceLabel);
  465. actionContainer.Add(buyButton);
  466. itemElement.Add(infoContainer);
  467. itemElement.Add(actionContainer);
  468. itemList.Add(itemElement);
  469. }
  470. private void CreateSellItemElement(Item item)
  471. {
  472. var itemElement = new VisualElement();
  473. itemElement.AddToClassList("sell-item");
  474. // Use flexible row layout
  475. itemElement.style.flexDirection = FlexDirection.Row;
  476. itemElement.style.alignItems = Align.Center;
  477. itemElement.style.justifyContent = Justify.SpaceBetween;
  478. itemElement.style.paddingTop = 5;
  479. itemElement.style.paddingBottom = 5;
  480. itemElement.style.paddingLeft = 10;
  481. itemElement.style.paddingRight = 10;
  482. itemElement.style.marginBottom = 3;
  483. itemElement.style.backgroundColor = new Color(0.95f, 0.95f, 0.95f, 1f);
  484. itemElement.style.borderBottomWidth = 1;
  485. itemElement.style.borderBottomColor = new Color(0.8f, 0.8f, 0.8f, 1f);
  486. // Left side - Item info container
  487. var infoContainer = new VisualElement();
  488. infoContainer.style.flexDirection = FlexDirection.Column;
  489. infoContainer.style.flexGrow = 1;
  490. infoContainer.style.marginRight = 10;
  491. var nameLabel = new Label(item.itemName);
  492. nameLabel.AddToClassList("item-name");
  493. nameLabel.style.fontSize = 14;
  494. nameLabel.style.color = new Color(0.2f, 0.2f, 0.2f, 1f);
  495. nameLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
  496. var descLabel = new Label(item.description);
  497. descLabel.AddToClassList("item-description");
  498. descLabel.style.fontSize = 12;
  499. descLabel.style.color = new Color(0.4f, 0.4f, 0.4f, 1f);
  500. descLabel.style.whiteSpace = WhiteSpace.Normal;
  501. infoContainer.Add(nameLabel);
  502. infoContainer.Add(descLabel);
  503. // Right side - Price and button container
  504. var actionContainer = new VisualElement();
  505. actionContainer.style.flexDirection = FlexDirection.Row;
  506. actionContainer.style.alignItems = Align.Center;
  507. actionContainer.style.minWidth = 150;
  508. var sellPrice = currentShop.GetSellPrice(item);
  509. var sellPriceGold = sellPrice / 100;
  510. var sellPriceSilver = (sellPrice % 100) / 10;
  511. var sellPriceCopper = sellPrice % 10;
  512. var priceLabel = new Label($"Sell: {sellPriceGold}g {sellPriceSilver}s {sellPriceCopper}c");
  513. priceLabel.AddToClassList("item-price");
  514. priceLabel.style.fontSize = 12;
  515. priceLabel.style.color = new Color(0.0f, 0.5f, 0.0f, 1f);
  516. priceLabel.style.marginRight = 10;
  517. priceLabel.style.minWidth = 80;
  518. var sellButton = new Button(() => SellItem(item))
  519. {
  520. text = "Sell"
  521. };
  522. sellButton.AddToClassList("sell-button");
  523. sellButton.style.width = 60;
  524. sellButton.style.height = 25;
  525. sellButton.style.fontSize = 12;
  526. // Check if shop accepts this item
  527. if (!currentShop.CanBuyItem(item))
  528. {
  529. sellButton.SetEnabled(false);
  530. priceLabel.text = "Not accepted";
  531. priceLabel.style.color = new Color(0.8f, 0.2f, 0.2f, 1f);
  532. }
  533. actionContainer.Add(priceLabel);
  534. actionContainer.Add(sellButton);
  535. itemElement.Add(infoContainer);
  536. itemElement.Add(actionContainer);
  537. itemList.Add(itemElement);
  538. }
  539. private void BuyItem(ShopInventoryItem shopItem)
  540. {
  541. if (selectedCustomer == null || shopItem == null) return;
  542. // Use ShopInventoryItem's Purchase method which handles money transaction
  543. if (shopItem.CanAfford(selectedCustomer))
  544. {
  545. shopItem.Purchase(selectedCustomer);
  546. // Add item to player's inventory (this needs proper implementation)
  547. // For now, we'll add to the appropriate string list
  548. AddItemToPlayerInventory(shopItem.item);
  549. // Refresh UI
  550. RefreshItemList();
  551. UpdateMoneyDisplay();
  552. Debug.Log($"Bought {shopItem.item.itemName}");
  553. }
  554. else
  555. {
  556. Debug.Log("Not enough money!");
  557. }
  558. }
  559. private void AddItemToPlayerInventory(Item item)
  560. {
  561. if (selectedCustomer == null || item == null) return;
  562. // Add item name to appropriate list based on item type
  563. switch (item.itemType)
  564. {
  565. case ItemType.Weapon:
  566. if (selectedCustomer.weapons == null)
  567. selectedCustomer.weapons = new List<string>();
  568. selectedCustomer.weapons.Add(item.itemName);
  569. break;
  570. case ItemType.Armor:
  571. if (selectedCustomer.armor == null)
  572. selectedCustomer.armor = new List<string>();
  573. selectedCustomer.armor.Add(item.itemName);
  574. break;
  575. case ItemType.Consumable:
  576. case ItemType.Miscellaneous:
  577. case ItemType.Tool:
  578. case ItemType.Accessory:
  579. if (selectedCustomer.miscItems == null)
  580. selectedCustomer.miscItems = new List<string>();
  581. selectedCustomer.miscItems.Add(item.itemName);
  582. break;
  583. }
  584. }
  585. private void SellItem(Item item)
  586. {
  587. if (selectedCustomer == null || item == null) return;
  588. var sellPrice = currentShop.GetSellPrice(item);
  589. if (sellPrice > 0)
  590. {
  591. // Add money to player using the shop's method
  592. currentShop.SellItemToShop(item, selectedCustomer);
  593. // Remove item from player inventory
  594. RemoveItemFromPlayerInventory(item);
  595. // Refresh UI
  596. RefreshItemList();
  597. UpdateMoneyDisplay();
  598. Debug.Log($"Sold {item.itemName} for {sellPrice / 100}g");
  599. }
  600. else
  601. {
  602. Debug.Log("Shop doesn't accept this item!");
  603. }
  604. }
  605. private void RemoveItemFromPlayerInventory(Item item)
  606. {
  607. if (selectedCustomer == null || item == null) return;
  608. // Remove item name from appropriate list
  609. switch (item.itemType)
  610. {
  611. case ItemType.Weapon:
  612. selectedCustomer.weapons?.Remove(item.itemName);
  613. break;
  614. case ItemType.Armor:
  615. selectedCustomer.armor?.Remove(item.itemName);
  616. break;
  617. case ItemType.Consumable:
  618. case ItemType.Miscellaneous:
  619. case ItemType.Tool:
  620. case ItemType.Accessory:
  621. selectedCustomer.miscItems?.Remove(item.itemName);
  622. break;
  623. }
  624. }
  625. private void SwitchToBuyMode()
  626. {
  627. isSellMode = false;
  628. if (buyTab != null)
  629. {
  630. buyTab.AddToClassList("active-tab");
  631. }
  632. if (sellTab != null)
  633. {
  634. sellTab.RemoveFromClassList("active-tab");
  635. }
  636. RefreshItemList();
  637. }
  638. private void SwitchToSellMode()
  639. {
  640. isSellMode = true;
  641. if (sellTab != null)
  642. {
  643. sellTab.AddToClassList("active-tab");
  644. }
  645. if (buyTab != null)
  646. {
  647. buyTab.RemoveFromClassList("active-tab");
  648. }
  649. RefreshItemList();
  650. }
  651. }