123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.SFX;
- namespace KairoEngine.Inventory
- {
- public class BarterControllerUi : MonoBehaviour
- {
- public static BarterControllerUi instance;
- public GameObject buyItemSlotPrefab;
- public ItemContainerUi buyUi;
- public ItemContainerUi sellUi;
- public float panelDistance = 4.5f;
- public SFXClip sfxAddItem;
- public SFXClip sfxRemoveItem;
- public SFXClip sfxConfirm;
- public SFXClip sfxCancel;
- public SFXClip sfxNegative;
- public SFXClip sfxOpenWindow;
- private bool initialized = false;
-
- private void Awake()
- {
- if(instance == null) instance = this;
- else Destroy(this);
- }
- private void Update ()
- {
- if(buyUi.itemContainerPanel.activeSelf && buyUi.targetItemContainer != null)
- {
- if(!initialized) Initialize();
- UpdatePanelPosition(buyUi.targetItemContainer.transform, buyUi.itemContainerPanel.transform);
- UpdatePurchaseValue();
-
- }
- if(sellUi.itemContainerPanel.activeSelf && sellUi.targetItemContainer != null)
- {
- if(!initialized) Initialize();
- UpdatePanelPosition(sellUi.targetItemContainer.transform, sellUi.itemContainerPanel.transform);
- UpdateSaleValue();
- }
- }
- private void Initialize()
- {
- initialized = true;
- SoundController.EmmitSound(sfxOpenWindow, this.transform.position);
- }
- private void UpdatePanelPosition(Transform target, Transform targetPanel)
- {
- RectTransform panelTransform = targetPanel.GetComponent<RectTransform>();
- Vector3 pos = new Vector3(target.position.x, target.position.y + panelDistance, target.position.z);
- Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
- if(Vector3.Distance(panelTransform.position, screenPos) > 0.1f)
- {
- screenPos.z = 100;
- panelTransform.position = screenPos;
- }
-
- }
- private void UpdateSaleValue()
- {
- int total = 0;
- foreach (var item in sellUi.targetItemContainer.inventory)
- {
- total += item.item.value * item.quantity;
- }
- string title = "Sell Items ($" + total + ")";
- sellUi.SetWindowTitle(title);
- }
- private void UpdatePurchaseValue()
- {
- int total = 0;
- foreach (var itemSlot in buyUi.itemSlots)
- {
- if(itemSlot.itemRef != null)
- {
- total += itemSlot.itemRef.item.value * itemSlot.quantityToBuy;
- }
-
- }
- string title = "Buy Items ($" + total + ")";
- buyUi.SetWindowTitle(title);
- }
- public void ConcludeSale()
- {
- int total = 0;
- foreach (var item in sellUi.targetItemContainer.inventory)
- {
- total += item.item.value * item.quantity;
- }
- sellUi.targetItemContainer.inventory.Clear();
- ItemBase itemBase = ItemLibrary.GetItemByName("Old Dollars");
- ItemRef itemRef = new ItemRef(itemBase, total, 1000);
- ItemContainer playerInventory = null; //GameController.instance.heroCharacter.GetComponent<ItemContainer>();
- if(playerInventory == null)
- {
- Debug.LogError("playerInventory is missing!");
- return;
- }
- playerInventory.AddItem(itemRef);
- ICommand closeItemContainerCommand = new CloseItemContainerCommand(sellUi.targetItemContainer.title);
- CommandInvoker.AddCommand(closeItemContainerCommand);
- EmmitEvent(sellUi.targetItemContainer, total);
- initialized = false;
- SoundController.EmmitSound(sfxConfirm, this.transform.position);
- }
- public void ConcludePurchase()
- {
- ItemContainer playerInventory = EventManager.request.GetItemContainer("PlayerItemContainer").value;
- if(playerInventory == null)
- {
- Debug.LogError("playerInventory is missing!");
- return;
- }
- List<ItemRef> tradedItems = new List<ItemRef>();
- int total = 0;
- for (int i = 0; i < buyUi.itemSlots.Count; i++)
- {
- ItemSlotUi itemSlot = buyUi.itemSlots[i];
- if(itemSlot.itemRef != null)
- {
- if(itemSlot.quantityToBuy > 0)
- {
- total += itemSlot.itemRef.item.value * itemSlot.quantityToBuy;
- ItemRef playerItemRef = new ItemRef(itemSlot.itemRef.item, itemSlot.quantityToBuy, 1000);
- ItemRef currentItemRef = new ItemRef(itemSlot.itemRef.item, itemSlot.quantityToBuy, 1000);
- if(currentItemRef.item.category == ItemType.firearm)
- {
- ItemBaseFirearm firearmItem = (ItemBaseFirearm)playerItemRef.item;
- ItemFirearmRef firearmRef = new ItemFirearmRef(firearmItem, playerItemRef.quantity, firearmItem.ammoType, firearmItem.ammoCapacity);
- playerItemRef = firearmRef;
- }
- tradedItems.Add(currentItemRef);
- playerInventory.AddItem(playerItemRef);
- GenericEvents.Trigger("Player buy item", $"{playerItemRef.item.title}_{playerItemRef.item.value}", playerItemRef.quantity);
- }
- }
- }
- ItemBase itemBase = ItemLibrary.GetItemByName("Old Dollars");
- ItemRef itemRef = new ItemRef(itemBase, total, 1000);
- playerInventory.RemoveItem(itemRef);
- foreach (var tradedItem in tradedItems)
- {
- buyUi.targetItemContainer.RemoveItem(tradedItem);
- }
- ICommand closeItemContainerCommand = new CloseItemContainerCommand(buyUi.containerTitle);
- CommandInvoker.AddCommand(closeItemContainerCommand);
- EmmitEvent(buyUi.targetItemContainer, total);
- initialized = false;
- SoundController.EmmitSound(sfxConfirm, this.transform.position);
- }
- public void CancelSale()
- {
- ItemContainer playerInventory = null; //GameController.instance.heroCharacter.GetComponent<ItemContainer>();
- if(playerInventory == null)
- {
- Debug.LogError("playerInventory is missing!");
- return;
- }
- foreach (var item in sellUi.targetItemContainer.inventory)
- {
- playerInventory.AddItem(item);
- }
- sellUi.targetItemContainer.inventory.Clear();
- ICommand closeItemContainerCommand = new CloseItemContainerCommand(sellUi.targetItemContainer.title);
- CommandInvoker.AddCommand(closeItemContainerCommand);
- EmmitEvent(sellUi.targetItemContainer, 0);
- initialized = false;
- SoundController.EmmitSound(sfxCancel, this.transform.position);
- }
- public void CancelPurchase()
- {
- ICommand closeItemContainerCommand = new CloseItemContainerCommand(buyUi.containerTitle);
- CommandInvoker.AddCommand(closeItemContainerCommand);
- EmmitEvent(buyUi.targetItemContainer, 0);
- initialized = false;
- SoundController.EmmitSound(sfxCancel, this.transform.position);
- }
- private void EmmitEvent(ItemContainer itemContainer, int value)
- {
- CharacterController character = itemContainer.GetComponent<CharacterController>();
- if(character == null) return;
- GenericEvents.Trigger("TradeFinished");
- }
- }
- }
|