123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- using System.Collections.Generic;
- using System.Linq;
- using Assets.HeroEditor4D.Common.Scripts.Common;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Elements;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Assets.HeroEditor4D.InventorySystem.Scripts
- {
- /// <summary>
- /// High-level inventory interface.
- /// </summary>
- public class WarehouseBase : ItemWorkspace
- {
- public ScrollInventory WarehouseInventory;
- public ScrollInventory PlayerInventory;
- public InputField AmountInput;
- public Button PutButton;
- public Button TakeButton;
- public AudioClip MoveSound;
- public AudioSource AudioSource;
- public bool ExampleInitialize;
- public string CurrencyId = "Gold";
- public int Amount;
-
- public void Awake()
- {
- // You must to set an active collection (as there may be several different collections in Resources).
- ItemCollection.Active = ItemCollection;
- }
- public void Start()
- {
- if (ExampleInitialize)
- {
- TestInitialize();
- }
- }
- /// <summary>
- /// Initialize owned items and trader items (just for example).
- /// </summary>
- public void TestInitialize()
- {
- var warehouse = new List<Item>();
- var inventory = ItemCollection.Active.Items.Select(i => new Item(i.Id, 2)).ToList();
- RegisterCallbacks();
- WarehouseInventory.Initialize(ref warehouse);
- PlayerInventory.Initialize(ref inventory);
- if (!WarehouseInventory.SelectAny() && !PlayerInventory.SelectAny())
- {
- ItemInfo.Reset();
- }
- }
- public void Initialize(ref List<Item> playerItems, ref List<Item> storageItems)
- {
- RegisterCallbacks();
- PlayerInventory.Initialize(ref playerItems);
- WarehouseInventory.Initialize(ref storageItems);
- if (!PlayerInventory.SelectAny() && !WarehouseInventory.SelectAny())
- {
- ItemInfo.Reset();
- }
- }
- public void RegisterCallbacks()
- {
- InventoryItem.OnLeftClick = SelectItem;
- InventoryItem.OnRightClick = OnDoubleClick;
- }
- private void OnDoubleClick(Item item)
- {
- SelectItem(item);
- if (PlayerInventory.Items.Contains(item))
- {
- Take();
- }
- else if (CanMoveSelectedItem())
- {
- Put();
- }
- }
- public void SelectItem(Item item)
- {
- SelectedItem = item;
- SetAmount(1);
- ItemInfo.Initialize(SelectedItem, SelectedItem.Params.Price * Amount);
- Refresh();
- }
- public void Put()
- {
- if (!CanMoveSelectedItem()) return;
- MoveItem(SelectedItem, PlayerInventory, WarehouseInventory, Amount);
- SelectItem(SelectedItem);
- AudioSource.PlayOneShot(MoveSound, SfxVolume);
- }
- public void Take()
- {
- if (!CanMoveSelectedItem()) return;
- MoveItem(SelectedItem, WarehouseInventory, PlayerInventory, Amount);
- SelectItem(SelectedItem);
- AudioSource.PlayOneShot(MoveSound, SfxVolume);
- }
- public override void Refresh()
- {
- if (SelectedItem == null)
- {
- ItemInfo.Reset();
- PutButton.SetActive(false);
- TakeButton.SetActive(false);
- }
- else
- {
- var stored = WarehouseInventory.Items.Contains(SelectedItem);
- PutButton.SetActive(!stored && CanMoveSelectedItem());
- TakeButton.SetActive(stored && CanMoveSelectedItem());
- }
- }
- public void SetMinAmount()
- {
- SetAmount(1);
- }
- public void IncAmount(int value)
- {
- SetAmount(Amount + value);
- }
- public void SetMaxAmount()
- {
- SetAmount(SelectedItem.Count);
- }
- public void OnAmountChanged(string value)
- {
- if (value.IsEmpty()) return;
- SetAmount(int.Parse(value));
- }
- public void OnAmountEndEdit(string value)
- {
- if (value.IsEmpty())
- {
- SetAmount(1);
- }
- }
- public void Drop()
- {
- foreach (var item in PlayerInventory.Items.ToList())
- {
- if (item.Params.Type != ItemType.Currency && !item.Params.Tags.Contains(ItemTag.Quest))
- {
- #if TAP_HEROES
- if (item.Params.Class == ItemClass.Gunpowder) continue;
- #endif
- MoveItem(item, PlayerInventory, WarehouseInventory, item.Count);
- }
- }
- AudioSource.PlayOneShot(MoveSound, SfxVolume);
- }
- private void SetAmount(int amount)
- {
- Amount = Mathf.Max(1, Mathf.Min(SelectedItem.Count, amount));
- AmountInput?.SetTextWithoutNotify(Amount.ToString());
- ItemInfo.UpdatePrice(SelectedItem, SelectedItem.Params.Price * Amount, false);
- }
- protected virtual bool CanMoveSelectedItem()
- {
- return true;
- }
- }
- }
|