123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
- using Assets.HeroEditor4D.InventorySystem.Scripts.Helpers;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Assets.HeroEditor4D.InventorySystem.Scripts.Elements
- {
- /// <summary>
- /// Represents hero (player) equipment. Based on equipment slots.
- /// </summary>
- public class Equipment : ItemContainer
- {
- public Action OnRefresh;
- /// <summary>
- /// Defines what kinds of items can be equipped.
- /// </summary>
- public List<ItemSlot> Slots;
- /// <summary>
- /// Equipped items will be instantiated in front of equipment slots.
- /// </summary>
- public GameObject ItemPrefab;
- /// <summary>
- /// Character preview.
- /// </summary>
- public Character Preview;
- public Transform Scheme;
- public int BagSize;
- public readonly List<InventoryItem> InventoryItems = new List<InventoryItem>();
- public void OnValidate()
- {
- if (Application.isPlaying) return;
- Slots = GetComponentsInChildren<ItemSlot>(true).ToList();
- //if (Character == null)
- //{
- // Character = FindObjectOfType<Character>();
- //}
- }
- public void Start()
- {
- //Character.Animator.SetBool("Ready", false);
- }
- public void SetBagSize(int size)
- {
- BagSize = size;
- var supplies = GetComponentsInChildren<ItemSlot>(true).Where(i => i.Types.Contains(ItemType.Supply)).ToList();
- for (var i = 0; i < supplies.Count; i++)
- {
- supplies[i].Locked = i >= size;
- }
- }
- public bool SelectAny()
- {
- if (InventoryItems.Count > 0)
- {
- InventoryItems[0].Select(true);
- return true;
- }
- return false;
- }
- public override void Refresh(Item selected)
- {
- var items = Slots.Select(FindItem).Where(i => i != null).ToList();
- var toggleGroup = GetComponentInParent<ToggleGroup>(includeInactive: true);
- Reset();
- foreach (var slot in Slots)
- {
- var item = FindItem(slot);
- slot.gameObject.SetActive(item == null);
- if (item == null) continue;
- var inventoryItem = Instantiate(ItemPrefab, slot.transform.parent).GetComponent<InventoryItem>();
- inventoryItem.Initialize(item, toggleGroup);
- inventoryItem.Count.text = null;
- inventoryItem.transform.position = slot.transform.position;
- inventoryItem.transform.SetSiblingIndex(slot.transform.GetSiblingIndex());
-
- if (AutoSelect) inventoryItem.Select(item == selected);
- InventoryItems.Add(inventoryItem);
- }
- if (Preview)
- {
- CharacterInventorySetup.Setup(Preview, items);
- Preview.Initialize();
- }
- OnRefresh?.Invoke();
- }
- private void Reset()
- {
- foreach (var inventoryItem in InventoryItems)
- {
- Destroy(inventoryItem.gameObject);
- }
- InventoryItems.Clear();
- }
- private Item FindItem(ItemSlot slot)
- {
- if (slot.Types.Contains(ItemType.Shield))
- {
- var copy = Items.SingleOrDefault(i => i.Params.Type == ItemType.Weapon && (i.IsTwoHanded || i.IsFirearm));
- if (copy != null)
- {
- return copy;
- }
- }
- var index = Slots.Where(i => i.Types.SequenceEqual(slot.Types)).ToList().IndexOf(slot);
- var items = Items.Where(slot.Supports).ToList();
- return index < items.Count ? items[index] : null;
- }
- }
- }
|