BarterControllerUi.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.SFX;
  7. namespace KairoEngine.Inventory
  8. {
  9. public class BarterControllerUi : MonoBehaviour
  10. {
  11. public static BarterControllerUi instance;
  12. public GameObject buyItemSlotPrefab;
  13. public ItemContainerUi buyUi;
  14. public ItemContainerUi sellUi;
  15. public float panelDistance = 4.5f;
  16. public SFXClip sfxAddItem;
  17. public SFXClip sfxRemoveItem;
  18. public SFXClip sfxConfirm;
  19. public SFXClip sfxCancel;
  20. public SFXClip sfxNegative;
  21. public SFXClip sfxOpenWindow;
  22. private bool initialized = false;
  23. private void Awake()
  24. {
  25. if(instance == null) instance = this;
  26. else Destroy(this);
  27. }
  28. private void Update ()
  29. {
  30. if(buyUi.itemContainerPanel.activeSelf && buyUi.targetItemContainer != null)
  31. {
  32. if(!initialized) Initialize();
  33. UpdatePanelPosition(buyUi.targetItemContainer.transform, buyUi.itemContainerPanel.transform);
  34. UpdatePurchaseValue();
  35. }
  36. if(sellUi.itemContainerPanel.activeSelf && sellUi.targetItemContainer != null)
  37. {
  38. if(!initialized) Initialize();
  39. UpdatePanelPosition(sellUi.targetItemContainer.transform, sellUi.itemContainerPanel.transform);
  40. UpdateSaleValue();
  41. }
  42. }
  43. private void Initialize()
  44. {
  45. initialized = true;
  46. SoundController.EmmitSound(sfxOpenWindow, this.transform.position);
  47. }
  48. private void UpdatePanelPosition(Transform target, Transform targetPanel)
  49. {
  50. RectTransform panelTransform = targetPanel.GetComponent<RectTransform>();
  51. Vector3 pos = new Vector3(target.position.x, target.position.y + panelDistance, target.position.z);
  52. Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
  53. if(Vector3.Distance(panelTransform.position, screenPos) > 0.1f)
  54. {
  55. screenPos.z = 100;
  56. panelTransform.position = screenPos;
  57. }
  58. }
  59. private void UpdateSaleValue()
  60. {
  61. int total = 0;
  62. foreach (var item in sellUi.targetItemContainer.inventory)
  63. {
  64. total += item.item.value * item.quantity;
  65. }
  66. string title = "Sell Items ($" + total + ")";
  67. sellUi.SetWindowTitle(title);
  68. }
  69. private void UpdatePurchaseValue()
  70. {
  71. int total = 0;
  72. foreach (var itemSlot in buyUi.itemSlots)
  73. {
  74. if(itemSlot.itemRef != null)
  75. {
  76. total += itemSlot.itemRef.item.value * itemSlot.quantityToBuy;
  77. }
  78. }
  79. string title = "Buy Items ($" + total + ")";
  80. buyUi.SetWindowTitle(title);
  81. }
  82. public void ConcludeSale()
  83. {
  84. int total = 0;
  85. foreach (var item in sellUi.targetItemContainer.inventory)
  86. {
  87. total += item.item.value * item.quantity;
  88. }
  89. sellUi.targetItemContainer.inventory.Clear();
  90. ItemBase itemBase = ItemLibrary.GetItemByName("Old Dollars");
  91. ItemRef itemRef = new ItemRef(itemBase, total, 1000);
  92. ItemContainer playerInventory = null; //GameController.instance.heroCharacter.GetComponent<ItemContainer>();
  93. if(playerInventory == null)
  94. {
  95. Debug.LogError("playerInventory is missing!");
  96. return;
  97. }
  98. playerInventory.AddItem(itemRef);
  99. ICommand closeItemContainerCommand = new CloseItemContainerCommand(sellUi.targetItemContainer.title);
  100. CommandInvoker.AddCommand(closeItemContainerCommand);
  101. EmmitEvent(sellUi.targetItemContainer, total);
  102. initialized = false;
  103. SoundController.EmmitSound(sfxConfirm, this.transform.position);
  104. }
  105. public void ConcludePurchase()
  106. {
  107. ItemContainer playerInventory = EventManager.request.GetItemContainer("PlayerItemContainer").value;
  108. if(playerInventory == null)
  109. {
  110. Debug.LogError("playerInventory is missing!");
  111. return;
  112. }
  113. List<ItemRef> tradedItems = new List<ItemRef>();
  114. int total = 0;
  115. for (int i = 0; i < buyUi.itemSlots.Count; i++)
  116. {
  117. ItemSlotUi itemSlot = buyUi.itemSlots[i];
  118. if(itemSlot.itemRef != null)
  119. {
  120. if(itemSlot.quantityToBuy > 0)
  121. {
  122. total += itemSlot.itemRef.item.value * itemSlot.quantityToBuy;
  123. ItemRef playerItemRef = new ItemRef(itemSlot.itemRef.item, itemSlot.quantityToBuy, 1000);
  124. ItemRef currentItemRef = new ItemRef(itemSlot.itemRef.item, itemSlot.quantityToBuy, 1000);
  125. if(currentItemRef.item.category == ItemType.firearm)
  126. {
  127. ItemBaseFirearm firearmItem = (ItemBaseFirearm)playerItemRef.item;
  128. ItemFirearmRef firearmRef = new ItemFirearmRef(firearmItem, playerItemRef.quantity, firearmItem.ammoType, firearmItem.ammoCapacity);
  129. playerItemRef = firearmRef;
  130. }
  131. tradedItems.Add(currentItemRef);
  132. playerInventory.AddItem(playerItemRef);
  133. GenericEvents.Trigger("Player buy item", $"{playerItemRef.item.title}_{playerItemRef.item.value}", playerItemRef.quantity);
  134. }
  135. }
  136. }
  137. ItemBase itemBase = ItemLibrary.GetItemByName("Old Dollars");
  138. ItemRef itemRef = new ItemRef(itemBase, total, 1000);
  139. playerInventory.RemoveItem(itemRef);
  140. foreach (var tradedItem in tradedItems)
  141. {
  142. buyUi.targetItemContainer.RemoveItem(tradedItem);
  143. }
  144. ICommand closeItemContainerCommand = new CloseItemContainerCommand(buyUi.containerTitle);
  145. CommandInvoker.AddCommand(closeItemContainerCommand);
  146. EmmitEvent(buyUi.targetItemContainer, total);
  147. initialized = false;
  148. SoundController.EmmitSound(sfxConfirm, this.transform.position);
  149. }
  150. public void CancelSale()
  151. {
  152. ItemContainer playerInventory = null; //GameController.instance.heroCharacter.GetComponent<ItemContainer>();
  153. if(playerInventory == null)
  154. {
  155. Debug.LogError("playerInventory is missing!");
  156. return;
  157. }
  158. foreach (var item in sellUi.targetItemContainer.inventory)
  159. {
  160. playerInventory.AddItem(item);
  161. }
  162. sellUi.targetItemContainer.inventory.Clear();
  163. ICommand closeItemContainerCommand = new CloseItemContainerCommand(sellUi.targetItemContainer.title);
  164. CommandInvoker.AddCommand(closeItemContainerCommand);
  165. EmmitEvent(sellUi.targetItemContainer, 0);
  166. initialized = false;
  167. SoundController.EmmitSound(sfxCancel, this.transform.position);
  168. }
  169. public void CancelPurchase()
  170. {
  171. ICommand closeItemContainerCommand = new CloseItemContainerCommand(buyUi.containerTitle);
  172. CommandInvoker.AddCommand(closeItemContainerCommand);
  173. EmmitEvent(buyUi.targetItemContainer, 0);
  174. initialized = false;
  175. SoundController.EmmitSound(sfxCancel, this.transform.position);
  176. }
  177. private void EmmitEvent(ItemContainer itemContainer, int value)
  178. {
  179. CharacterController character = itemContainer.GetComponent<CharacterController>();
  180. if(character == null) return;
  181. GenericEvents.Trigger("TradeFinished");
  182. }
  183. }
  184. }